Class: PrettyApi::ActiveRecord::Associations

Inherits:
Object
  • Object
show all
Defined in:
lib/pretty_api/active_record/associations.rb

Class Method Summary collapse

Class Method Details

.attribute_association(model, attribute) ⇒ Object



29
30
31
# File 'lib/pretty_api/active_record/associations.rb', line 29

def self.attribute_association(model, attribute)
  model.reflect_on_association(attribute).chain.last
end

.attribute_destroy_allowed?(model, attribute) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/pretty_api/active_record/associations.rb', line 25

def self.attribute_destroy_allowed?(model, attribute)
  model.nested_attributes_options[attribute.to_sym][:allow_destroy] == true
end

.nested_attributes_tree(model, result = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pretty_api/active_record/associations.rb', line 4

def self.nested_attributes_tree(model, result = {})
  model.nested_attributes_options.each_key do |association_name|
    result[model] ||= {}

    association = attribute_association(model, association_name)
    association_class = association.klass

    result[model][association_name] = {
      model: association_class,
      type: association.macro,
      allow_destroy: attribute_destroy_allowed?(model, association_name)
    }

    next if result.key?(association_class)

    result[association_class] = {}
    nested_attributes_tree(association_class, result)
  end
  result
end