Module: CustomFields::Types::ManyToMany::Target::ClassMethods
- Defined in:
- lib/custom_fields/types/many_to_many.rb
Instance Method Summary collapse
-
#apply_many_to_many_custom_field(klass, rule) ⇒ Object
Adds a many_to_many relationship between 2 mongoid models.
Instance Method Details
#apply_many_to_many_custom_field(klass, rule) ⇒ Object
Adds a many_to_many relationship between 2 mongoid models
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/custom_fields/types/many_to_many.rb', line 36 def apply_many_to_many_custom_field(klass, rule) # puts "#{klass.inspect}.many_to_many #{rule['name'].inspect}, class_name: #{rule['class_name'].inspect} / #{rule['order_by']}" # DEBUG klass.has_and_belongs_to_many rule['name'], class_name: rule['class_name'], inverse_of: rule['inverse_of'], order: rule['order_by'] do # def at_least_one_element? # (base.send(metadata.key.to_sym).try(:size) || 0) > 0 # end def filtered(conditions = {}, order_by = nil) list = conditions.empty? ? self : self.where(conditions) if order_by list.order_by(order_by) else # use the natural order given by the initial array (ex: project_ids). # Warning: it returns an array and not a criteria object meaning it breaks the chain ids = base.send(.key.to_sym) list.entries.sort { |a, b| ids.index(a.id) <=> ids.index(b.id) } end end alias :ordered :filtered # backward compatibility + semantic purpose end if rule['required'] klass.validates_collection_size_of rule['name'], minimum: 1, message: :at_least_one_element end end |