module Redcrumbs::SerializableAssociation

Public Class Methods

included(base) click to toggle source
# File lib/redcrumbs/serializable_association.rb, line 6
def self.included(base)
  base.extend(ClassMethods)

  base.class_eval do
    include DataMapper::Resource unless self < DataMapper::Resource
  end
end

Public Instance Methods

load_associated(name) click to toggle source

Load the association from the database.

# File lib/redcrumbs/serializable_association.rb, line 86
def load_associated(name)
  return nil unless association_id = send("#{name}_id")

  class_name = send("#{name}_type") || config_class_name_for(name)
  klass = class_name.classify.constantize

  primary_key = config_primary_key_for(name) || klass.primary_key

  klass.where(primary_key => association_id).first
end

Private Instance Methods

assign_id_for(name, associated) click to toggle source

Assign the association id based on default primary key

# File lib/redcrumbs/serializable_association.rb, line 101
def assign_id_for(name, associated)
  id = if associated
    primary_key = config_primary_key_for(name) or associated.class.primary_key
    associated[primary_key]
  end

  send("#{name}_id=", id)
end
assign_serialized_attributes(name, associated) click to toggle source

Serialize and assign the association

# File lib/redcrumbs/serializable_association.rb, line 122
def assign_serialized_attributes(name, associated)
  serialized = associated ? serialize(name, associated) : {}

  send("stored_#{name}=", serialized)
end
assign_type_for(name, associated) click to toggle source

Assign the association type based on default primary key

# File lib/redcrumbs/serializable_association.rb, line 113
def assign_type_for(name, associated)
  type = associated ? associated.class.name : nil

  send("#{name}_type=", type)
end
clean_properties(klass, properties) click to toggle source

Return a properties hash that corresponds to the given class’s column names.

# File lib/redcrumbs/serializable_association.rb, line 179
def clean_properties(klass, properties)
  properties.select {|k,v| klass.column_names.include?(k.to_s)}
end
config_class_name_for(name) click to toggle source

Get the class name from the config options, e.g. Redcrumbs.creator_class_sym

# File lib/redcrumbs/serializable_association.rb, line 132
def config_class_name_for(name)
  Redcrumbs.send("#{name}_class_sym").to_s
end
config_primary_key_for(name) click to toggle source

Get the expected primary key for the association from the config options.

# File lib/redcrumbs/serializable_association.rb, line 140
def config_primary_key_for(name)
  Redcrumbs.send("#{name}_primary_key")
rescue NoMethodError
  nil
end
deserialize(name) click to toggle source

Returns a new instance of the associated object based on the serialized attributes only.

# File lib/redcrumbs/serializable_association.rb, line 163
def deserialize(name)
  properties = send("stored_#{name}")
  associated_id = send("#{name}_id")

  return nil unless properties.present? and associated_id

  class_name = send("#{name}_type")
  class_name ||= config_class_name_for(name) unless name == :subject

  instantiate_with_id(class_name, properties, associated_id)
end
instantiate_with_id(class_name, properties, associated_id) click to toggle source
# File lib/redcrumbs/serializable_association.rb, line 184
def instantiate_with_id(class_name, properties, associated_id)
  klass = class_name.classify.constantize
  properties = clean_properties(klass, properties)

  associated = klass.new(properties, :without_protection => true)
  associated.id = associated_id
  associated
end
serialize(name, associated) click to toggle source

Serializes a given object by looking for its configuration options or calling serialization method.

# File lib/redcrumbs/serializable_association.rb, line 149
def serialize(name, associated)
  if name == :subject
    associated.serialized_as_redcrumbs_subject
  else
    keys = Redcrumbs.send("store_#{name}_attributes").dup

    associated.attributes.select {|k,v| keys.include?(k.to_sym)}
  end
end