Module: ActsAsAssetBox
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/acts_as_asset_box.rb
Overview
ActsAsAssetBox
Returns a set of assets or one asset as defined by the provided ‘boxes’ (a box is just a category) array Works with Formtastic::Inputs::asset_box_input
Define your model with ‘acts_as_asset_box’ if you just need one box of assets You will then be able to call @my_resource.assets and @my_resource.asset
or
acts_as_asset_box :fav_icon, :pictures, :logos
or (if you want validations)
acts_as_asset_box :fav_icon => true, :pictures => false, :videos => 2, :images => 5..10
Define your model with ‘acts_as_asset_box :fav_icon, :pictures, :logos’ for 3 different boxes of assets. This creates three methods, @my_resource.fav_icon, @my_resource.pictures, @my_resource.logos
If you define a singular name, like :fav_icon, then calling @my_resource.fav_icon will return the 1 asset from the fav_icon box If you define a plural name, like :pictures, then calling @my_resource.pictures will return all assets in the pictures box
This works with the lib/acts_as_asset_box_input.rb for editting boxes of assets
Defined Under Namespace
Modules: ActiveRecord, ClassMethods
Constant Summary collapse
- VALIDATION_KEYS =
[:presence, :length, :inclusion, :private, :public]
Instance Method Summary collapse
-
#add_to_asset_box(box, *items) ⇒ Object
This method can be used to manually link an asset to an acts_as_asset_box object in tests or otherwise.
- #add_to_asset_box!(box, *items) ⇒ Object
- #effective_assets(box = nil) ⇒ Object
-
#replace_in_asset_box(box, original, overwrite) ⇒ Object
The idea here is that if you want to replace an Asset with Another one the Attachment should keep the same order, and only the asset should change.
- #replace_in_asset_box!(box, original, overwrite) ⇒ Object
Instance Method Details
#add_to_asset_box(box, *items) ⇒ Object
This method can be used to manually link an asset to an acts_as_asset_box object in tests or otherwise. The object needs to be saved afterward so that the Effective::Attachment is saved (and the association is saved). For example:
class User < ActiveRecord::Base
acts_as_asset_box :avatar
end
asset = Effective::Asset.new(...)
user = User.new
user.add_to_asset_box(:avatar, asset)
user.save
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'app/models/concerns/acts_as_asset_box.rb', line 143 def add_to_asset_box(box, *items) box = (box.present? ? box.to_s : 'assets') boxes = box.pluralize items = [items].flatten.compact if items.present? && items.any? { |obj| !obj.kind_of?(Effective::Asset) } raise ArgumentError.new('add_to_asset_box expects one or more Effective::Assets, or an Array of Effective::Assets') end if box == boxes # If we're adding to a pluralized box, we want to add our attachment onto the end pos = .select { || .box == boxes }.last.try(:position).to_i + 1 else # If we're adding to a singular box, we want our attachments to be on the front pos = .to_a.find { || .box == boxes }.try(:position).to_i # Push all the attachments forward .each { |att| att.position = (att.position + items.length) if att.box == boxes } end # Build the attachments items.each_with_index do |item, x| = self..build(:position => (pos+x), :box => boxes) .attachable = self .asset = item end .to_a.sort_by! { || .position } true end |
#add_to_asset_box!(box, *items) ⇒ Object
174 175 176 |
# File 'app/models/concerns/acts_as_asset_box.rb', line 174 def add_to_asset_box!(box, *items) add_to_asset_box(box, items) && save! end |
#effective_assets(box = nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'app/models/concerns/acts_as_asset_box.rb', line 118 def effective_assets(box = nil) box = (box.present? ? box.to_s : 'assets') boxes = box.pluralize if box == boxes .map { || .asset if .box == boxes }.compact else .sort_by { || .position }.find { || .box == boxes }.try(:asset) end end |
#replace_in_asset_box(box, original, overwrite) ⇒ Object
The idea here is that if you want to replace an Asset with Another one the Attachment should keep the same order, and only the asset should change
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'app/models/concerns/acts_as_asset_box.rb', line 181 def replace_in_asset_box(box, original, overwrite) box = (box.present? ? box.to_s : 'assets') boxes = box.pluralize unless original.present? && original.kind_of?(Effective::Asset) raise ArgumentError.new("second parameter 'original' must be a single Effective::Asset") end unless overwrite.present? && overwrite.kind_of?(Effective::Asset) raise ArgumentError.new("third parameter 'overwrite' must be a single Effective::Asset") end = .to_a.find { || .box == boxes && .asset == original } if .present? .asset = overwrite true else false end end |
#replace_in_asset_box!(box, original, overwrite) ⇒ Object
203 204 205 |
# File 'app/models/concerns/acts_as_asset_box.rb', line 203 def replace_in_asset_box!(box, original, overwrite) replace_in_asset_box(box, original, overwrite) && save! end |