Module: Qcontent::Assets::Macros

Defined in:
lib/qcontent/assets.rb

Instance Method Summary collapse

Instance Method Details

#has_assets(group_name, options = {}) ⇒ Object



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
36
37
38
# File 'lib/qcontent/assets.rb', line 10

def has_assets(group_name, options = {})
  
  has_many :content_assets, :as => :content, :dependent => :delete_all
  has_many "content_#{group_name}", :class_name => 'ContentAsset', 
                                    :order => 'content_assets.position ASC',
                                    :as => :content,
                                    :conditions => "content_assets.asset_group = '#{group_name}'"
  has_many group_name, :through => "content_#{group_name}", :source => :asset
  include InstanceMethods
  
  alias_method "#{group_name}_association=", "#{group_name}="
  
  after_save :save_assets
  
  self.class_eval <<-EOT
    def #{group_name}=(hash_of_assets)
      return unless hash_of_assets
      if hash_of_assets.is_a?(Array) && hash_of_assets.first.is_a?(Asset)
        self.#{group_name}_association = hash_of_assets
      else
        collect_assets_to_save(:#{group_name}, hash_of_assets)
      end
    end
    
    def #{group_name.to_s.singularize}
      self.#{group_name}.first
    end
  EOT
end