module Humpyard::ActiveRecord::Acts::Element

Public Class Methods

included(base) click to toggle source
# File lib/humpyard/active_record/acts/element.rb, line 5
def self.included(base)
  base.has_one :element, :as => :content_data, :class_name => 'Humpyard::Element', :autosave => true
  base.validate :element_must_be_valid
  base.alias_method_chain :element, :autobuild 
  base.alias_method_chain :column_for_attribute, :element_column_for_attribute
  
  begin
    all_attributes = Humpyard::Element.column_names
  rescue
    # Table not migrated
    all_attributes = []
  end
  ignored_attributes = ['id', 'created_at', 'updated_at', 'content_data_id', 'content_data_type']
  attributes_to_delegate = all_attributes - ignored_attributes
  attributes_to_delegate.each do |attrib|
    base.delegate "#{attrib}", "#{attrib}=", "#{attrib}?", :to => :element
    if attrib.match /_id$/
      attrib = attrib.gsub /(_id)$/, ''
      base.delegate "#{attrib}", "#{attrib}=", "#{attrib}?", :to => :element
    end
  end
  
  Humpyard::Element.attr_accessible.each do |attr|
    base.attr_accessible attr
  end
     
  base.extend ClassMethods

end

Public Instance Methods

column_for_attribute_with_element_column_for_attribute(attr) click to toggle source
# File lib/humpyard/active_record/acts/element.rb, line 39
def column_for_attribute_with_element_column_for_attribute(attr)
  ret = column_for_attribute_without_element_column_for_attribute(attr) || element.column_for_attribute(attr)
end
element_with_autobuild() click to toggle source
# File lib/humpyard/active_record/acts/element.rb, line 35
def element_with_autobuild
  element_without_autobuild || build_element
end
is_humpyard_container_element?() click to toggle source
# File lib/humpyard/active_record/acts/element.rb, line 47
def is_humpyard_container_element?
  self.class.is_humpyard_container_element?
end
is_humpyard_element?() click to toggle source
# File lib/humpyard/active_record/acts/element.rb, line 43
def is_humpyard_element?
  self.class.is_humpyard_element?
end

Protected Instance Methods

element_must_be_valid() click to toggle source
# File lib/humpyard/active_record/acts/element.rb, line 72
def element_must_be_valid
  unless element.valid?
    element.errors.each do |attr, message|
      errors.add(attr, message)
    end
  end
end