Sha256: 4edf9366834dc071f3fe31d39f89196ff7672f790d7bdb7b03b2374c49d358c6

Contents?: true

Size: 1.48 KB

Versions: 8

Compression:

Stored size: 1.48 KB

Contents

module Effective
  module Resources
    module Instance
      attr_reader :instance

      # This is written for use by effective_logging and effective_trash

      def instance_attributes
        return {} unless instance.present?

        attributes = { attributes: instance.attributes }

        # Collect to_s representations of all belongs_to associations
        belong_tos.each do |association|
          attributes[association.name] = instance.send(association.name).to_s
        end

        has_ones.each do |association|
          attributes[association.name] = instance.send(association.name).to_s
        end

        nested_resources.each do |association|
          attributes[association.name] = {}

          Array(instance.send(association.name)).each_with_index do |child, index|
            resource = Effective::Resource.new(child)
            attributes[association.name][index] = resource.instance_attributes
          end
        end

        attributes.delete_if { |_, value| value.blank? }
      end

      def instance_changes
        return {} unless instance.present?

        changes = instance.changes

        # Log to_s changes on all belongs_to associations
        belong_tos.each do |association|
          if (change = changes.delete(association.foreign_key)).present?
            changes[association.name] = [(association.klass.find_by_id(change.first) if changes.first), instance.send(association.name)]
          end
        end

        changes
      end

    end
  end
end




Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
effective_resources-0.3.4 app/models/effective/resources/instance.rb
effective_resources-0.3.3 app/models/effective/resources/instance.rb
effective_resources-0.3.2 app/models/effective/resources/instance.rb
effective_resources-0.3.1 app/models/effective/resources/instance.rb
effective_resources-0.3.0 app/models/effective/resources/instance.rb
effective_resources-0.2.4 app/models/effective/resources/instance.rb
effective_resources-0.2.3 app/models/effective/resources/instance.rb
effective_resources-0.2.2 app/models/effective/resources/instance.rb