Class: Flattener::Resource
- Inherits:
-
Object
- Object
- Flattener::Resource
show all
- Includes:
- ActiveModel::Model, ResourceGenerator, Utils
- Defined in:
- lib/has_helpers/flattener/resource.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Utils
#combine_names, #underscore_type
#generate_resources
Instance Attribute Details
#association ⇒ Object
Returns the value of attribute association.
10
11
12
|
# File 'lib/has_helpers/flattener/resource.rb', line 10
def association
@association
end
|
#base_flattener=(value) ⇒ Object
Sets the attribute base_flattener
11
12
13
|
# File 'lib/has_helpers/flattener/resource.rb', line 11
def base_flattener=(value)
@base_flattener = value
end
|
#flattener ⇒ Object
51
52
53
|
# File 'lib/has_helpers/flattener/resource.rb', line 51
def flattener
@flattener ||= association.flattener
end
|
#index ⇒ Object
Returns the value of attribute index.
10
11
12
|
# File 'lib/has_helpers/flattener/resource.rb', line 10
def index
@index
end
|
#parent ⇒ Object
Returns the value of attribute parent.
10
11
12
|
# File 'lib/has_helpers/flattener/resource.rb', line 10
def parent
@parent
end
|
#type ⇒ Object
Returns the value of attribute type.
10
11
12
|
# File 'lib/has_helpers/flattener/resource.rb', line 10
def type
@type
end
|
Instance Method Details
#associations ⇒ Object
37
38
39
40
|
# File 'lib/has_helpers/flattener/resource.rb', line 37
def associations
@associations ||= flattener.associations.
reject { |a| association.present? && a.reject_if.call(self) }
end
|
#attributes ⇒ Object
27
28
29
30
31
|
# File 'lib/has_helpers/flattener/resource.rb', line 27
def attributes
@attributes ||= flattener.columns.
reject { |column| exclude_column?(column) }.
map { |column| Flattener::Attribute.new(resource: self, column: column) }
end
|
#flatten(depth = 1) ⇒ Object
46
47
48
49
|
# File 'lib/has_helpers/flattener/resource.rb', line 46
def flatten(depth = 1)
return [*attributes, *generate_resources(associations.select(&:always_include)).flat_map { |r| r.flatten(0) }] if depth.zero?
[*attributes, *resources.flat_map { |r| r.flatten(depth - 1) }]
end
|
#hide_owner_keys? ⇒ Boolean
59
60
61
62
63
|
# File 'lib/has_helpers/flattener/resource.rb', line 59
def hide_owner_keys?
if klass.respond_to?(:owner_association_reflections) && parent.present?
klass.owner_association_reflections.map(&:klass).include? parent.klass
end
end
|
#id_prefix ⇒ Object
22
23
24
25
|
# File 'lib/has_helpers/flattener/resource.rb', line 22
def id_prefix
return "[#{index}]" if index
"[#{type_name}]" if type
end
|
#is_constant? ⇒ Boolean
65
66
67
|
# File 'lib/has_helpers/flattener/resource.rb', line 65
def is_constant?
klass.include?(HasHelpers::Constant)
end
|
#klass ⇒ Object
55
56
57
|
# File 'lib/has_helpers/flattener/resource.rb', line 55
def klass
flattener.class.model_klass
end
|
#merge_into_parent ⇒ Object
42
43
44
|
# File 'lib/has_helpers/flattener/resource.rb', line 42
def merge_into_parent
association && association.merge_into_parent
end
|
#name ⇒ Object
13
14
15
|
# File 'lib/has_helpers/flattener/resource.rb', line 13
def name
combine_names(parent.name, resource_name) if association.present?
end
|
#owner_keys ⇒ Object
69
70
71
|
# File 'lib/has_helpers/flattener/resource.rb', line 69
def owner_keys
klass.owner_association_reflections.map(&:foreign_key)
end
|
#prefix ⇒ Object
17
18
19
20
|
# File 'lib/has_helpers/flattener/resource.rb', line 17
def prefix
return klass.name.underscore if parent.nil?
parent && "#{parent.prefix}[#{association.ar_association_name}_attributes]#{id_prefix}"
end
|
#resources ⇒ Object
33
34
35
|
# File 'lib/has_helpers/flattener/resource.rb', line 33
def resources
@resources ||= generate_resources
end
|