Class: Flattener::Resource

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

Methods included from ResourceGenerator

#generate_resources

Instance Attribute Details

#associationObject

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 (writeonly)

Sets the attribute base_flattener

Parameters:

  • value

    the value to set the attribute base_flattener to.



11
12
13
# File 'lib/has_helpers/flattener/resource.rb', line 11

def base_flattener=(value)
  @base_flattener = value
end

#flattenerObject



51
52
53
# File 'lib/has_helpers/flattener/resource.rb', line 51

def flattener
  @flattener ||= association.flattener
end

#indexObject

Returns the value of attribute index.



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

def index
  @index
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#typeObject

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

#associationsObject



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

#attributesObject



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

Returns:

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



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

Returns:

  • (Boolean)


65
66
67
# File 'lib/has_helpers/flattener/resource.rb', line 65

def is_constant?
  klass.include?(HasHelpers::Constant)
end

#klassObject



55
56
57
# File 'lib/has_helpers/flattener/resource.rb', line 55

def klass
  flattener.class.model_klass
end

#merge_into_parentObject



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

def merge_into_parent
  association && association.merge_into_parent
end

#nameObject



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_keysObject



69
70
71
# File 'lib/has_helpers/flattener/resource.rb', line 69

def owner_keys
  klass.owner_association_reflections.map(&:foreign_key)
end

#prefixObject



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

#resourcesObject



33
34
35
# File 'lib/has_helpers/flattener/resource.rb', line 33

def resources
  @resources ||= generate_resources
end