Module: CustomFields::Types::BelongsTo::Target::ClassMethods
- Defined in:
- lib/custom_fields/types/belongs_to.rb
Instance Method Summary collapse
-
#apply_belongs_to_custom_field(klass, rule) ⇒ Object
Adds a belongs_to relationship between 2 models.
Instance Method Details
#apply_belongs_to_custom_field(klass, rule) ⇒ Object
Adds a belongs_to relationship between 2 models
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/custom_fields/types/belongs_to.rb', line 29 def apply_belongs_to_custom_field(klass, rule) # puts "#{klass.inspect}.belongs_to #{rule['name'].inspect}, class_name: #{rule['class_name'].inspect}" # DEBUG position_name = "position_in_#{rule['name'].underscore}" # puts "#{klass.inspect}.field :#{position_name}" # DEBUG klass.field position_name, type: ::Integer, default: 0 = { class_name: rule['class_name'], optional: true } [:inverse_of] = rule['inverse_of'] unless rule['inverse_of'].blank? klass.belongs_to rule['name'].to_sym, klass.validates_presence_of rule['name'].to_sym if rule['required'] klass.before_create do |object| position = (object.class.max(position_name.to_sym) || 0) + 1 object.send(:"#{position_name}=", position) end end |