Module: Trax::Model::STI::Attributes::ClassMethods
- Defined in:
- lib/trax/model/sti/attributes.rb
Instance Method Summary collapse
Instance Method Details
#sti_attribute(*args) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/trax/model/sti/attributes.rb', line 52 def sti_attribute(*args) = args. args.each do |attribute_name| return unless self._attribute_set_class_name.constantize.table_exists? raise ::Trax::Model::Errors::STIAttributeNotFound unless self._attribute_set_class_name.constantize.column_names.include?("#{attribute_name}") self._sti_attributes << attribute_name self.delegate(attribute_name, :to => :attribute_set) self.delegate("#{attribute_name}=", :to => :attribute_set) unless .key?(:writer) && [:writer] == false end end |
#validates_uniqueness_of!(*args) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/trax/model/sti/attributes.rb', line 66 def validates_uniqueness_of!(*args) = args. args.each do |arg| validation_method_name = :"validate_#{arg.to_s}" self.send(:define_method, :"validate_#{arg.to_s}") do |*_args| where_scope_hash = {} field_value = self.__send__(arg) where_scope_hash[arg] = field_value where_scope = self._attribute_set_class_name.constantize.where(where_scope_hash).all [:scope].each do |field| scoped_field_value = self.__send__(field) where_scope.merge(self.class.where({field => scoped_field_value})) end if .has_key?(:scope) errors.add(arg, "has already been taken") if where_scope.limit(1).any? end self.validate(validation_method_name) end end |