Sha256: e5329e7226ce126c354b05bcc8884903fa5e4dab5f022f356d7d6f3aa1366a15

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

require 'active_support/concern'

module Journey::Resource::AttributeLoading
  extend ActiveSupport::Concern

  included do
    def load(attributes, remove_root = false, persisted = false)
      # 'data' is a reserved key in ActiveResource,
      # but Journey uses it for the Oplog
      if content = attributes.delete('data')
        attributes['content'] = content
      end

      super(attributes, remove_root, persisted).tap do

        # set the cached value of any updated associations (e.g. parent_id)
        # to nil so that they can be re-fetched
        attributes.keys.map(&:to_s).select{ |key| key =~ /_id$/ }.each do |association_key|
          association = association_key.gsub /_id$/, ''
          instance_variable_set("@#{association}", nil)
        end

        # allow enum_sets to be loaded by key (rather than index)
        # by auto-converting them on initialization
        if enum_sets = self.class.instance_variable_get(:@enum_sets)
          enum_sets.each do |enum_attr|
            send("#{enum_attr}=", send(enum_attr))
          end
        end
      end
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
embark-journey-0.1.7 lib/journey/resource/attribute_loading.rb
embark-journey-0.1.6 lib/journey/resource/attribute_loading.rb