Module: Humpyard::ActiveRecord::Acts::Asset

Defined in:
lib/humpyard/active_record/acts/asset.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/humpyard/active_record/acts/asset.rb', line 6

def self.included(base)
  base.has_one :asset, :as => :content_data, :class_name => 'Humpyard::Asset', :autosave => true
  base.validate :asset_must_be_valid
  
  begin
    all_attributes = Humpyard::Asset.column_names.map{|a| a.to_s}
  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 => :asset
    if attrib.match /_id$/
      attrib = attrib.gsub /(_id)$/, ''
      base.delegate "#{attrib}", "#{attrib}=", "#{attrib}?", :to => :asset
    end
  end
  
  Humpyard::Asset.attr_accessible.each do |attr|
    base.attr_accessible attr
  end
  
  base.extend ClassMethods
  
  base.alias_method_chain :asset, :autobuild 
  base.alias_method_chain :column_for_attribute, :asset_column_for_attribute
  
end

Instance Method Details

#asset_with_autobuildObject



37
38
39
# File 'lib/humpyard/active_record/acts/asset.rb', line 37

def asset_with_autobuild
  asset_without_autobuild || build_asset
end

#column_for_attribute_with_asset_column_for_attribute(attr) ⇒ Object



41
42
43
# File 'lib/humpyard/active_record/acts/asset.rb', line 41

def column_for_attribute_with_asset_column_for_attribute(attr)
  ret = column_for_attribute_without_asset_column_for_attribute(attr) || Humpyard::Asset.new.column_for_attribute(attr)       
end