Sha256: 125625bd5cf16907f07c942e09f9097868fe0ce92a6a35e0723173ea1d5b5bcc

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 KB

Contents

module PrettyApi
  module ActiveRecord
    class Associations
      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

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pretty-api-0.5.1 lib/pretty_api/active_record/associations.rb
pretty-api-0.5.0 lib/pretty_api/active_record/associations.rb
pretty-api-0.4.1 lib/pretty_api/active_record/associations.rb
pretty-api-0.4.0 lib/pretty_api/active_record/associations.rb
pretty-api-0.3.1 lib/pretty_api/active_record/associations.rb