Module: CustomFields::Types::HasMany::Target::ClassMethods

Defined in:
lib/custom_fields/types/has_many.rb

Instance Method Summary collapse

Instance Method Details

#apply_has_many_custom_field(klass, rule) ⇒ Object

Adds a has_many relationship between 2 mongoid models

Parameters:

  • klass (Class)

    The class to modify

  • rule (Hash)

    It contains the name of the relation and if it is required or not



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/custom_fields/types/has_many.rb', line 28

def apply_has_many_custom_field(klass, rule)
  # puts "#{klass.inspect}.has_many #{rule['name'].inspect}, class_name: #{rule['class_name'].inspect}, inverse_of: #{rule['inverse_of']}, order_by: #{rule['order_by'].inspect}" # DEBUG
  position_name = "position_in_#{rule['inverse_of']}"

  _order_by   = rule['order_by'] || position_name.to_sym.asc
  _inverse_of = rule['inverse_of'].blank? ? nil : rule['inverse_of'] # an empty String can cause weird behaviours

  klass.has_many rule['name'], class_name: rule['class_name'], inverse_of: _inverse_of, order: _order_by,
                               validate: false do
    def filtered(conditions = {}, order_by = nil)
      list = conditions.empty? ? unscoped : where(conditions)

      list.order_by(order_by || association.options[:order])
    end
    alias_method :ordered, :filtered # backward compatibility + semantic purpose
  end

  klass.accepts_nested_attributes_for rule['name'], allow_destroy: true

  return unless rule['required']

  klass.validates_collection_size_of rule['name'], minimum: 1, message: :at_least_one_element, on: :update
end