Class: Gemgento::ImageImport
- Inherits:
-
Import
- Object
- ActiveRecord::Base
- Import
- Gemgento::ImageImport
show all
- Defined in:
- app/models/gemgento/image_import.rb
Overview
Instance Attribute Summary collapse
Attributes inherited from Import
#header_row, #row, #spreadsheet
Instance Method Summary
collapse
Methods inherited from Import
#content_index_range, #percentage_complete, #process, #process_later, #set_default_options, #set_default_process_errors, #set_total_rows, #value
Instance Attribute Details
#product ⇒ Object
Returns the value of attribute product.
8
9
10
|
# File 'app/models/gemgento/image_import.rb', line 8
def product
@product
end
|
Instance Method Details
#create_image(file_name, types, position, label) ⇒ Void
Create an image for the product.
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'app/models/gemgento/image_import.rb', line 119
def create_image(file_name, types, position, label)
asset = Asset.new
asset.product = self.product
asset.store = store
asset.position = position
asset.label = label
asset.set_file(File.open(file_name))
types.each do |type|
asset.asset_types << type
end
asset.sync_needed = false
asset.save
asset.sync_needed = true
unless asset.save
self.process_errors << "Row #{current_row}: #{asset.errors[:base]}"
end
end
|
#create_images ⇒ Void
Search for and create all possible images for a product.
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'app/models/gemgento/image_import.rb', line 86
def create_images
self.image_labels.each_with_index do |label, position|
self.image_file_extensions.each do |extension|
file_name = self.image_path + value('image') + '_' + label + extension
next unless File.exist?(file_name)
types = []
unless self.image_types[position].nil?
types = Gemgento::AssetType.where(product_attribute_set: self.product.product_attribute_set, code: self.image_types[position])
end
unless types.is_a? Array
types = [types]
end
create_image(file_name, types, position, label)
end
end
Rails.cache.clear
end
|
#default_options ⇒ Object
12
13
14
15
16
17
18
19
20
21
|
# File 'app/models/gemgento/image_import.rb', line 12
def default_options
{
image_labels: [],
image_file_extensions: [],
image_types: [],
image_path: nil,
store_id: nil,
destroy_existing: false
}
end
|
#destroy_existing? ⇒ Boolean
56
57
58
|
# File 'app/models/gemgento/image_import.rb', line 56
def destroy_existing?
options[:destroy_existing].to_bool
end
|
#destroy_existing_assets ⇒ Void
Destroy all Assets associated with self.product.
73
74
75
76
77
78
79
80
81
|
# File 'app/models/gemgento/image_import.rb', line 73
def destroy_existing_assets
self.product.assets.where(store: store).find_each do |asset|
begin
asset.destroy
rescue
end
end
end
|
#image_file_extensions_raw ⇒ Object
31
32
33
|
# File 'app/models/gemgento/image_import.rb', line 31
def image_file_extensions_raw
image_file_extensions.join(', ')
end
|
#image_file_extensions_raw=(values) ⇒ Object
35
36
37
|
# File 'app/models/gemgento/image_import.rb', line 35
def image_file_extensions_raw=(values)
options[:image_file_extensions] = values.gsub(' ', '').split(',')
end
|
#image_labels_raw ⇒ Object
23
24
25
|
# File 'app/models/gemgento/image_import.rb', line 23
def image_labels_raw
image_labels.join("\n")
end
|
#image_labels_raw=(values) ⇒ Object
27
28
29
|
# File 'app/models/gemgento/image_import.rb', line 27
def image_labels_raw=(values)
options[:image_labels] = values.gsub("\r", '').split("\n")
end
|
#image_types_raw ⇒ Object
39
40
41
|
# File 'app/models/gemgento/image_import.rb', line 39
def image_types_raw
image_types.join("\n")
end
|
#image_types_raw=(values) ⇒ Object
43
44
45
46
|
# File 'app/models/gemgento/image_import.rb', line 43
def image_types_raw=(values)
options[:image_types] = []
options[:image_types] = values.gsub("\r", '').split("\n").map { |t| t.split(',').collect(&:strip) }
end
|
#process_row ⇒ Object
60
61
62
63
64
65
66
67
68
|
# File 'app/models/gemgento/image_import.rb', line 60
def process_row
sku = value('sku')
self.product = Product.not_deleted.find_by(sku: sku)
return if self.product.nil?
API::SOAP::Catalog::ProductAttributeMedia.fetch(self.product, store)
destroy_existing_assets if destroy_existing?
create_images
end
|
#store ⇒ Object
48
49
50
51
52
53
54
|
# File 'app/models/gemgento/image_import.rb', line 48
def store
if options[:store_id].nil?
nil
else
@store ||= Gemgento::Store.find(options[:store_id])
end
end
|