Files

Class Index [+]

Quicksearch

UbiquoMedia::MediaSelector::ActiveRecord::ClassMethods

Constants

DEFAULT_OPTIONS
(Not documented)

Public Instance Methods

media_attachment(field, options = {}) click to toggle source

Class method for ActiveRecord. This creates relationship to Assets only calling this method. You must pass the desired field value and custom options. custom options:

  :size   => the max size of assets assigned. Can be an integer or :many if no limit. Default: 1
  :types  => array with allowed asset types keys. Can be a single key or an Array. if includes :ALL will accept all types. Default: ALL
  :styles => paperclip styles

EXAMPLES:

  media_attachment :simple
  media_attachment :multiple, :size => :many
  media_attachment :sized, :size => 2
  media_attachment :all_types, :types => :ALL
  media_attachment :some_types, :types => %w{audio video}
     # File vendor/plugins/ubiquo_media/lib/ubiquo_media/media_selector/active_record.rb, line 80
 80:         def media_attachment(field, options = {})
 81:           options.reverse_merge!(DEFAULT_OPTIONS)
 82: 
 83:           self.has_many(:asset_relations, {
 84:                           :as => :related_object,
 85:                           :class_name => "::AssetRelation",
 86:                           :dependent => :destroy,
 87:                           :order => "asset_relations.position ASC"
 88:                         }) unless self.respond_to?(:asset_relations)
 89: 
 90:           proc = Proc.new do
 91:             define_method('<<') do |assets|
 92:               assets = case assets
 93:                        when Array
 94:                          assets
 95:                        else
 96:                          [assets]
 97:                        end
 98:               assets.each do |asset|
 99:                 next if self.is_full?
100:                 name = nil
101:                 asset = case asset
102:                         when String, Fixnum
103:                           Asset.gfind(asset.to_i)
104:                         when Hash
105:                           name = asset["name"]
106:                           Asset.gfind(asset["id"].to_i)
107:                         when Asset
108:                           Asset.gfind(asset)
109:                         else
110:                           raise "Not acceptable type"
111:                         end
112:                 next if asset.nil?
113:                 next unless self.accepts?(asset)
114:                 AssetRelation.scoped_creation(field, (name || asset.name)) do
115:                   self.concat(asset)
116:                 end
117:               end
118:             end
119:             define_method('is_full?') do
120:               return false if self.options[:size].to_sym == :many
121:               self.size >= self.options[:size]
122:             end
123:             define_method('accepts?') do |asset|
124:               refresh_types if options[:asset_types].nil?
125:               options[:asset_types].include?(asset.asset_type)
126:             end
127:             define_method('options') do
128:               refresh_types if options[:asset_types].nil?
129:               options
130:             end
131:             define_method('refresh_types') do
132:               options[:types] = [options[:types]].flatten.map(&:to_sym)
133:               if(options[:types].include?(:ALL))
134:                 options[:asset_types] = AssetType.find(:all)
135:               else
136:                 options[:asset_types] = options[:types].map{|o|AssetType.gfind(o)}
137:               end
138:             end
139:             
140:             # Automatically set the required attr_name when creating through the through
141:             define_method 'construct_owner_attributes' do |reflection|
142:               super.merge(:field_name => field.to_s)
143:             end
144:           end
145: 
146: 
147:           self.has_many(field, {
148:                           :through => :asset_relations,
149:                           :class_name => "::Asset",
150:                           :source => :asset,
151:                           :conditions => ["asset_relations.field_name = ?", field.to_s],
152:                           :order => "asset_relations.position ASC"
153:                         },&proc)
154: 
155:           define_method("name_for_asset") do |asset_field, asset|
156:             return "" if asset_field.to_s.blank? || asset.nil?
157:             AssetRelation.name_for_asset(asset_field, asset, self)
158:           end
159:           
160:           define_method("#{field}_ids=") do |values|
161:             # Sometimes +values+ comes grouped in a hash due a prototype issue
162:             values = values.values if values.is_a? Hash
163:             instance_variable_set("@#{field}_values_ids", values.reject(&:blank?))
164:           end
165: 
166:           define_method("#{field}_ids") do
167:             ids=instance_variable_get("@#{field}_values_ids")
168:             ids || send(field).map(&:id)
169:           end
170: 
171:           after_save "#{field}_after_save"
172: 
173:           define_method("#{field}_after_save") do
174:             if new_assets = instance_variable_get("@#{field}_values_ids")
175:               old_assets = send(field).dup
176:               new_assets.each_with_index { |a, i| (a["position"] ||= i + 1) if a.is_a?(Hash) }
177:               old_assets.each do |old_asset|
178:                 if new_asset = new_assets.detect{|asset_info| asset_info["id"].to_i == old_asset.id}
179:                   relation = self.asset_relations.first(:conditions => {:asset_id => old_asset.id, :field_name => field.to_s})
180:                   relation.update_attributes({ :name => new_asset["name"], :position => new_asset["position"] })
181:                 else
182:                   send(field).delete(old_asset)
183:                 end
184:               end
185:               # ensure they are ordered by position if they have the field
186:               new_assets = new_assets.sort_by{|a| (a.is_a?(Hash) && a['position']) || '' }
187:               send(field) << new_assets.reject{|asset| old_assets.map(&:id).include?(asset["id"].to_i)}
188:               instance_variable_set "@#{field}_values_ids", nil
189:             end
190:             true
191:           end
192:           
193:           uhook_media_attachment field, options
194:         end

Disabled; run with $DEBUG to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.