class Template

Public Class Methods

deserialize_attribute(value) click to toggle source
# File lib/template.rb, line 12
def self.deserialize_attribute(value)
  return ActiveSupport::JSON.decode(value)['json']
end
serialize_attribute(value) click to toggle source
# File lib/template.rb, line 8
def self.serialize_attribute(value)
  return ActiveSupport::JSON.encode({:json => value})
end

Public Instance Methods

all_attributes() click to toggle source
# File lib/template.rb, line 78
def all_attributes
  return default_attributes.dup.merge(processed_attributes)
end
default_attributes() click to toggle source
# File lib/template.rb, line 61
def default_attributes
  return @default_attributes || {}
end
default_attributes=(defaults) click to toggle source
# File lib/template.rb, line 65
def default_attributes=(defaults)
  @default_attributes = defaults
end
delete_attribute(attribute) click to toggle source
# File lib/template.rb, line 69
def delete_attribute(attribute)
  if processed_attributes.include?(attribute)
    raw_attributes.where(:key => attribute).map{ |a| a.destroy }
    return processed_attributes.delete(attribute)
  else
    return nil
  end
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/template.rb, line 16
def method_missing(method, *args, &block)
  if method.to_s =~ /=$/
    method = method.to_s.gsub(/=$/, '').to_sym
    processed_attributes[method] = args[0]
    return args[1]
  else
    method = method.to_sym
    if processed_attributes.keys.include?(method)
      return processed_attributes[method]
    elsif default_attributes.keys.include?(method)
      return default_attributes[method]
    end
  end

  super
end
processed_attributes() click to toggle source
# File lib/template.rb, line 46
def processed_attributes
  return @processed_attributes if @processed_attributes
  return rebuild_processed_attributes
end
rebuild_processed_attributes() click to toggle source
# File lib/template.rb, line 51
def rebuild_processed_attributes
  @processed_attributes = {}

  raw_attributes.each do |ra|
    @processed_attributes[ra.key.to_sym] = Template.deserialize_attribute(ra.value)
  end

  return @processed_attributes
end
save_attributes() click to toggle source
# File lib/template.rb, line 33
def save_attributes
  return unless @processed_attributes
  raise 'You must first save the template.' unless id

  @processed_attributes.each do |key, val|
    TemplateAttribute.transaction do
      raw_attr = TemplateAttribute.find_or_initialize_by_template_id_and_key(id, key)
      raw_attr.value = Template.serialize_attribute(val)
      raw_attr.save!
    end
  end
end