Class: Flattener::Association

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

.aliasesObject



131
132
133
# File 'lib/has_helpers/flattener/association.rb', line 131

def aliases
  @aliases ||= {}
end

Instance Attribute Details

#alias_nameObject



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_includeObject



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_associationObject

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

#countObject



42
43
44
# File 'lib/has_helpers/flattener/association.rb', line 42

def count
  @count ||= macro == :has_many ? HAS_MANY_COUNT : 0
end

#expand_onObject

Returns the value of attribute expand_on.



9
10
11
# File 'lib/has_helpers/flattener/association.rb', line 9

def expand_on
  @expand_on
end

#expand_on_columnObject



91
92
93
# File 'lib/has_helpers/flattener/association.rb', line 91

def expand_on_column
  @expand_on_column ||= @expand_on && "type"
end

#expand_on_nilObject

Returns the value of attribute expand_on_nil.



9
10
11
# File 'lib/has_helpers/flattener/association.rb', line 9

def expand_on_nil
  @expand_on_nil
end

#flattenerObject



95
96
97
# File 'lib/has_helpers/flattener/association.rb', line 95

def flattener
  @flattener ||= Flattener.for(klass).pivot_or_self.new
end

#forObject

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_keyObject



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

Parameters:

  • value

    the value to set the attribute is_nested_attributes to.



10
11
12
# File 'lib/has_helpers/flattener/association.rb', line 10

def is_nested_attributes=(value)
  @is_nested_attributes = value
end

#joinObject

Returns the value of attribute join.



9
10
11
# File 'lib/has_helpers/flattener/association.rb', line 9

def join
  @join
end

#join_resourceObject

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

Parameters:

  • value

    the value to set the attribute merge to.



10
11
12
# File 'lib/has_helpers/flattener/association.rb', line 10

def merge=(value)
  @merge = value
end

#merge_into_parentObject



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_keyObject

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_ifObject



81
82
83
# File 'lib/has_helpers/flattener/association.rb', line 81

def reject_if
  @reject_if ||= proc { |_r| false }
end

#removedObject



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

Returns:

  • (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

Returns:

  • (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.nested_attributes_options.key?(ar_association.name)
  else
    @is_nested_attributes
  end
end

#is_pivot?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/has_helpers/flattener/association.rb', line 120

def is_pivot?
  flattener.class.is_pivot?
end

#nameObject



32
33
34
# File 'lib/has_helpers/flattener/association.rb', line 32

def name
  @name ||= alias_name || ar_association.name
end

#parent_flattenerObject



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

#removeObject



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

#typeObject



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