Module: CachedRecord::ORM::ActiveRecord::InstanceMethods

Defined in:
lib/cached_record/orm/active_record.rb

Instance Method Summary collapse

Instance Method Details

#cache_attributesObject



53
54
55
# File 'lib/cached_record/orm/active_record.rb', line 53

def cache_attributes
  as_json(cache_json_options.slice(:only).merge(:root => false)).symbolize_keys!.merge cache_foreign_keys
end

#cache_foreign_key(name, reflection, value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/cached_record/orm/active_record.rb', line 62

def cache_foreign_key(name, reflection, value)
  case reflection.macro
  when :belongs_to
    {:"#{reflection.foreign_key}" => value.try(:id)}
  when :has_one
    {:"_#{name.to_s.singularize}_id" => value.try(:id)}
  when :has_many, :has_and_belongs_to_many
    {:"_#{name.to_s.singularize}_ids" => value.collect(&:id)}
  end
end

#cache_foreign_keysObject



56
57
58
59
60
61
# File 'lib/cached_record/orm/active_record.rb', line 56

def cache_foreign_keys
  (cache_json_options[:include] || {}).inject({}) do |json, name|
    reflection = self.class.reflections[name]
    json.merge cache_foreign_key(name, reflection, send(name))
  end
end