Class: Gemgento::Asset
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Gemgento::Asset
- Includes:
- ProductTouches
- Defined in:
- app/models/gemgento/asset.rb
Overview
Class Method Summary collapse
-
.find_by_code(product, code, store = nil) ⇒ Asset?
Find a products asset by the AssetType code.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
-
#image ⇒ Paperclip::Attachment?
Return the image file associated with the Asset.
-
#set_file(file) ⇒ void
Associate an image file with the Asset.
-
#set_types_by_codes(asset_type_codes) ⇒ void
Associate AssetTypes to the Asset.
Class Method Details
.find_by_code(product, code, store = nil) ⇒ Asset?
Find a products asset by the AssetType code
108 109 110 111 112 113 114 |
# File 'app/models/gemgento/asset.rb', line 108 def self.find_by_code(product, code, store = nil) product.assets .joins(:asset_types) .where(store: store || Gemgento::Store.current) .where(gemgento_asset_types: { code: code, product_attribute_set_id: product.product_attribute_set_id }) .first end |
Instance Method Details
#as_json(options = {}) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/gemgento/asset.rb', line 116 def as_json( = {}) result = super result[:styles] = {'original' => self.image.url(:original)} self.image.styles.keys.to_a.each do |style| result[:styles][style] = self.image.url(style.to_sym) end result[:types] = [] self.asset_types.each do |asset_type| result[:types] << asset_type.code unless result[:types].include? asset_type.code end return result end |
#image ⇒ Paperclip::Attachment?
Return the image file associated with the Asset.
64 65 66 67 68 69 70 |
# File 'app/models/gemgento/asset.rb', line 64 def image if self.asset_file.nil? nil else self.asset_file.file end end |
#set_file(file) ⇒ void
This method returns an undefined value.
Associate an image file with the Asset. If the same file is already associated to a related Asset in a different store, then the Asset will be associated with the existing AssetFile.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/models/gemgento/asset.rb', line 30 def set_file(file) matching_file = nil matching_asset = nil self.product.assets.each do |asset| next if asset.asset_file.nil? next if asset.store == self.store && asset.id != self.id # don't compare AssetFiles from the same store unless it's the same Asset if File.exist?(asset.asset_file.file.path(:original)) && FileUtils.compare_file(asset.asset_file.file.path(:original), file) matching_file = asset.asset_file matching_asset = asset break end end if matching_file.nil? begin matching_file = AssetFile.new matching_file.file = file matching_file.save rescue matching_file = nil end matching_asset = Asset.new end self.asset_file = matching_file self.file = matching_asset.file if self.file.blank? && !matching_asset.file.blank? end |
#set_types_by_codes(asset_type_codes) ⇒ void
This method returns an undefined value.
Associate AssetTypes to the Asset.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/gemgento/asset.rb', line 76 def set_types_by_codes(asset_type_codes) applied_asset_types = [] # loop through each return category and add it to the product if needed asset_type_codes.each do |asset_type_code| next if asset_type_code.blank? asset_type = AssetType.find_by( product_attribute_set: self.product.product_attribute_set, code: asset_type_code, ) next if asset_type.nil? self.asset_types << asset_type unless self.asset_types.include? asset_type # don't duplicate the asset types applied_asset_types << asset_type.id # an AssetType can only be associated to one asset for every asset_type.assets.where(product_id: self.product_id, store_id: self.store_id).where('gemgento_assets.id != ?', self.id).find_each do |asset| asset.asset_types.destroy(asset_type) end end # destroy any asset type associations that were not in the list self.asset_types.delete(AssetType.where('id NOT IN (?)', applied_asset_types)) end |