Class: Para::ModelFieldParsers::Relations
- Defined in:
- lib/para/model_field_parsers/relations.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #parse! ⇒ Object
-
#remove_counter_cache_column!(name, reflection) ⇒ Object
Allows removing counter cache columns from fields from has_many relations, when :inverse_of option is set.
Methods inherited from Base
#applicable?, #initialize, register
Constructor Details
This class inherits a constructor from Para::ModelFieldParsers::Base
Instance Method Details
#parse! ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/para/model_field_parsers/relations.rb', line 6 def parse! model.reflections.each do |name, reflection| # We ensure that name is a symbol and not a string for 4.2+ # versions of AR # We may find a better solution to handle strings, since our # `fields_hash` is already a `HashWithIndifferentAccess` name = name.to_sym next if name == :component # Remove foreign key, if existing, from fields fields_hash.delete(reflection.foreign_key.to_s) if reflection.[:polymorphic] == true fields_hash.delete(reflection.foreign_type.to_s) next end if model.[name] if reflection.collection? fields_hash[name] = AttributeField::NestedManyField.new( model, name: name, type: 'has_many', field_type: 'nested_many' ) else fields_hash[name] = AttributeField::NestedOneField.new( model, name: name, type: 'belongs_to', field_type: 'nested_one' ) end else if reflection.collection? remove_counter_cache_column!(name, reflection) fields_hash[name] = AttributeField::HasManyField.new( model, name: name, type: 'has_many', field_type: 'selectize' ) else fields_hash[name] = AttributeField::BelongsToField.new( model, name: name, type: 'belongs_to', field_type: 'selectize' ) end end end end |
#remove_counter_cache_column!(name, reflection) ⇒ Object
Allows removing counter cache columns from fields from has_many relations, when :inverse_of option is set
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/para/model_field_parsers/relations.rb', line 53 def remove_counter_cache_column!(name, reflection) return unless (inverse_relation = reflection.inverse_of) return unless (counter_name = inverse_relation.[:counter_cache]) counter_name = if String === counter_name counter_name else "#{ name }_count" end fields_hash.delete(counter_name) end |