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
Instance Method Summary collapse
- #add_to_asset_box(box, *items) ⇒ Object
- #add_to_asset_box!(box, *items) ⇒ Object
- #asset ⇒ Object
- #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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/models/concerns/acts_as_asset_box.rb', line 96 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
127 128 129 |
# File 'app/models/concerns/acts_as_asset_box.rb', line 127 def add_to_asset_box!(box, *items) add_to_asset_box(box, items) && save! end |
#asset ⇒ Object
92 93 94 |
# File 'app/models/concerns/acts_as_asset_box.rb', line 92 def asset assets(:asset) end |
#assets(box = nil) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'app/models/concerns/acts_as_asset_box.rb', line 81 def assets(box = nil) box = (box.present? ? box.to_s : 'assets') boxes = box.pluralize if box == boxes .map { || .asset if .box == boxes }.compact else .to_a.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
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'app/models/concerns/acts_as_asset_box.rb', line 134 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
156 157 158 |
# File 'app/models/concerns/acts_as_asset_box.rb', line 156 def replace_in_asset_box!(box, original, overwrite) replace_in_asset_box(box, original, overwrite) && save! end |