Class: Flattener::Association
- Inherits:
-
Object
- Object
- Flattener::Association
- Includes:
- ActiveModel::Model
- Defined in:
- lib/has_helpers/flattener/association.rb
Constant Summary collapse
- HAS_MANY_COUNT =
2
Class Attribute Summary collapse
Instance Attribute Summary collapse
- #alias_name ⇒ Object
- #always_include ⇒ Object
-
#ar_association ⇒ Object
Returns the value of attribute ar_association.
- #count ⇒ Object
-
#expand_on ⇒ Object
Returns the value of attribute expand_on.
- #expand_on_column ⇒ Object
-
#expand_on_nil ⇒ Object
Returns the value of attribute expand_on_nil.
- #flattener ⇒ Object
-
#for ⇒ Object
FIXME: Refactor to avoid conflicting with the Ruby keyword "for" *.
- #foreign_key ⇒ Object
-
#is_nested_attributes ⇒ Object
writeonly
Sets the attribute is_nested_attributes.
-
#join ⇒ Object
Returns the value of attribute join.
-
#join_resource ⇒ Object
Returns the value of attribute join_resource.
-
#merge ⇒ Object
writeonly
Sets the attribute merge.
- #merge_into_parent ⇒ Object
-
#pivot_foreign_key ⇒ Object
Returns the foreign key on the base table, referencing the pivot table.
- #reject_if ⇒ Object
- #removed ⇒ Object
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(ar_association, **args) ⇒ Association
constructor
A new instance of Association.
- #is_constant? ⇒ Boolean
- #is_nested_attributes? ⇒ Boolean
- #is_pivot? ⇒ Boolean
- #name ⇒ Object
- #parent_flattener ⇒ Object
- #remove ⇒ Object
- #type ⇒ Object
- #update(args) ⇒ Object
Constructor Details
#initialize(ar_association, **args) ⇒ Association
Returns a new instance of Association.
16 17 18 |
# File 'lib/has_helpers/flattener/association.rb', line 16 def initialize(ar_association, **args) super(ar_association: ar_association, **args) end |
Class Attribute Details
.aliases ⇒ Object
131 132 133 |
# File 'lib/has_helpers/flattener/association.rb', line 131 def aliases @aliases ||= {} end |
Instance Attribute Details
#alias_name ⇒ Object
36 37 38 |
# File 'lib/has_helpers/flattener/association.rb', line 36 def alias_name @alias_name ||= self.class.aliases[ar_association_name] end |
#always_include ⇒ Object
72 73 74 75 |
# File 'lib/has_helpers/flattener/association.rb', line 72 def always_include @always_include = (type == :constant && !ar_association.active_record.include?(HasHelpers::Constant)) if @always_include.nil? # always include if a constant, and parents not a constant @always_include end |
#ar_association ⇒ Object
Returns the value of attribute ar_association.
9 10 11 |
# File 'lib/has_helpers/flattener/association.rb', line 9 def ar_association @ar_association end |
#count ⇒ Object
42 43 44 |
# File 'lib/has_helpers/flattener/association.rb', line 42 def count @count ||= macro == :has_many ? HAS_MANY_COUNT : 0 end |
#expand_on ⇒ Object
Returns the value of attribute expand_on.
9 10 11 |
# File 'lib/has_helpers/flattener/association.rb', line 9 def @expand_on end |
#expand_on_column ⇒ Object
91 92 93 |
# File 'lib/has_helpers/flattener/association.rb', line 91 def @expand_on_column ||= @expand_on && "type" end |
#expand_on_nil ⇒ Object
Returns the value of attribute expand_on_nil.
9 10 11 |
# File 'lib/has_helpers/flattener/association.rb', line 9 def @expand_on_nil end |
#flattener ⇒ Object
95 96 97 |
# File 'lib/has_helpers/flattener/association.rb', line 95 def flattener @flattener ||= Flattener.for(klass).pivot_or_self.new end |
#for ⇒ Object
FIXME: Refactor to avoid conflicting with the Ruby keyword "for" *
104 105 106 |
# File 'lib/has_helpers/flattener/association.rb', line 104 def for @for ||= Flattener.default_for(type) end |
#foreign_key ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/has_helpers/flattener/association.rb', line 46 def foreign_key @foreign_key ||= begin columns = (type == :has_many ? flattener.columns : parent_flattener.columns) (columns.detect { |column| column.name.to_s == ar_association.foreign_key.to_s }.try(:alias_name) || ar_association.foreign_key).to_s end end |
#is_nested_attributes=(value) ⇒ Object (writeonly)
Sets the attribute is_nested_attributes
10 11 12 |
# File 'lib/has_helpers/flattener/association.rb', line 10 def is_nested_attributes=(value) @is_nested_attributes = value end |
#join ⇒ Object
Returns the value of attribute join.
9 10 11 |
# File 'lib/has_helpers/flattener/association.rb', line 9 def join @join end |
#join_resource ⇒ Object
Returns the value of attribute join_resource.
9 10 11 |
# File 'lib/has_helpers/flattener/association.rb', line 9 def join_resource @join_resource end |
#merge=(value) ⇒ Object (writeonly)
Sets the attribute merge
10 11 12 |
# File 'lib/has_helpers/flattener/association.rb', line 10 def merge=(value) @merge = value end |
#merge_into_parent ⇒ Object
67 68 69 70 |
# File 'lib/has_helpers/flattener/association.rb', line 67 def merge_into_parent @merge_into_parent = macro == :has_one if @merge_into_parent.nil? @merge_into_parent end |
#pivot_foreign_key ⇒ Object
Returns the foreign key on the base table, referencing the pivot table. In contrast, when an association is used as a pivot the foreign_key will be the column on the pivot table which references back to the base table. The pivot FK does not exist in the transactional database and must therefore be determined using the available klass name.
The default behavior may be overridden by setting an explicit value.
association.pivot_foreign_key = :firm_id
63 64 65 |
# File 'lib/has_helpers/flattener/association.rb', line 63 def pivot_foreign_key @pivot_foreign_key || klass.name.foreign_key end |
#reject_if ⇒ Object
81 82 83 |
# File 'lib/has_helpers/flattener/association.rb', line 81 def reject_if @reject_if ||= proc { |_r| false } end |
#removed ⇒ Object
77 78 79 |
# File 'lib/has_helpers/flattener/association.rb', line 77 def removed @removed ||= false end |
Class Method Details
.alias(name, alias_name) ⇒ Object
136 137 138 |
# File 'lib/has_helpers/flattener/association.rb', line 136 def self.alias(name, alias_name) aliases[name] = alias_name end |
Instance Method Details
#==(other) ⇒ Object
124 125 126 |
# File 'lib/has_helpers/flattener/association.rb', line 124 def ==(other) self.ar_association == other.ar_association end |
#is_constant? ⇒ Boolean
108 109 110 |
# File 'lib/has_helpers/flattener/association.rb', line 108 def is_constant? klass.include?(HasHelpers::Constant) && macro == :belongs_to end |
#is_nested_attributes? ⇒ Boolean
112 113 114 115 116 117 118 |
# File 'lib/has_helpers/flattener/association.rb', line 112 def is_nested_attributes? if @is_nested_attributes.nil? ar_association.active_record..key?(ar_association.name) else @is_nested_attributes end end |
#is_pivot? ⇒ Boolean
120 121 122 |
# File 'lib/has_helpers/flattener/association.rb', line 120 def is_pivot? flattener.class.is_pivot? end |
#name ⇒ Object
32 33 34 |
# File 'lib/has_helpers/flattener/association.rb', line 32 def name @name ||= alias_name || ar_association.name end |
#parent_flattener ⇒ Object
99 100 101 |
# File 'lib/has_helpers/flattener/association.rb', line 99 def parent_flattener @parent_flattener ||= Flattener.for(ar_association.active_record).new end |
#remove ⇒ Object
26 27 28 29 30 |
# File 'lib/has_helpers/flattener/association.rb', line 26 def remove self.for = [] self.always_include = false self.removed = true end |
#type ⇒ Object
85 86 87 88 89 |
# File 'lib/has_helpers/flattener/association.rb', line 85 def type return :constant if is_constant? return :nested_attributes if is_nested_attributes? macro end |
#update(args) ⇒ Object
20 21 22 23 24 |
# File 'lib/has_helpers/flattener/association.rb', line 20 def update(args) args.each do |k, v| send("#{k}=", v) end end |