Class: PrettyApi::ActiveRecord::Associations
- Inherits:
-
Object
- Object
- PrettyApi::ActiveRecord::Associations
- Defined in:
- lib/pretty_api/active_record/associations.rb
Class Method Summary collapse
- .attribute_association(model, attribute) ⇒ Object
- .attribute_destroy_allowed?(model, attribute) ⇒ Boolean
- .nested_attributes_tree(model, result = {}) ⇒ Object
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
25 26 27 |
# File 'lib/pretty_api/active_record/associations.rb', line 25 def self.attribute_destroy_allowed?(model, attribute) model.[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..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 |