Class: Comable::Product

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Liquidable, Csvable, Search, Ransackable, SkuItem
Defined in:
app/models/comable/product.rb,
app/models/comable/product/csvable.rb,
app/models/concerns/comable/product/search.rb

Defined Under Namespace

Modules: Csvable, Search

Constant Summary collapse

PREVIEW_SESSION_KEY =
:preview_product

Constants included from Search

Search::SEARCH_COLUMNS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SkuItem

#sku_h?, #sku_v?

Class Method Details

.cache_keyObject



28
29
30
31
32
33
# File 'app/models/comable/product.rb', line 28

def cache_key
  page = all.current_page if all.respond_to? :current_page
  maximum_updated_at = maximum(:updated_at)
  timestamp = maximum_updated_at.utc.to_s(cache_timestamp_format) if maximum_updated_at
  "#{model_name.cache_key}/all-#{[page, timestamp, count].compact.join('-')}"
end

Instance Method Details

#as_json(options = {}) ⇒ Object



62
63
64
65
# File 'app/models/comable/product.rb', line 62

def as_json(options = {})
  options[:include] = { variants: { except: :product, methods: [:quantity, :option_names] } }
  super
end

#category_path_names=(category_path_names, delimiter: Comable::Category::DEFAULT_PATH_NAME_DELIMITER) ⇒ Object



54
55
56
# File 'app/models/comable/product.rb', line 54

def category_path_names=(category_path_names, delimiter: Comable::Category::DEFAULT_PATH_NAME_DELIMITER)
  self.categories = Comable::Category.find_by_path_names(category_path_names, delimiter: delimiter)
end

#codeObject



75
76
77
# File 'app/models/comable/product.rb', line 75

def code
  variants.first.try(:sku)
end

#code=(code) ⇒ Object



79
80
81
# File 'app/models/comable/product.rb', line 79

def code=(code)
  variants.each { |v| v.sku = code }
end

#image_urlObject



36
37
38
39
40
# File 'app/models/comable/product.rb', line 36

def image_url
  image = images.first
  return image.url if image
  'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9InllcyI/PjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iMTcxIiBoZWlnaHQ9IjE4MCIgdmlld0JveD0iMCAwIDE3MSAxODAiIHByZXNlcnZlQXNwZWN0UmF0aW89Im5vbmUiPjxkZWZzLz48cmVjdCB3aWR0aD0iMTcxIiBoZWlnaHQ9IjE4MCIgZmlsbD0iI0VFRUVFRSIvPjxnPjx0ZXh0IHg9IjU4IiB5PSI5MCIgc3R5bGU9ImZpbGw6I0FBQUFBQTtmb250LXdlaWdodDpib2xkO2ZvbnQtZmFtaWx5OkFyaWFsLCBIZWx2ZXRpY2EsIE9wZW4gU2Fucywgc2Fucy1zZXJpZiwgbW9ub3NwYWNlO2ZvbnQtc2l6ZToxMHB0O2RvbWluYW50LWJhc2VsaW5lOmNlbnRyYWwiPjE3MXgxODA8L3RleHQ+PC9nPjwvc3ZnPg=='
end

#master?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/models/comable/product.rb', line 58

def master?
  option_types.empty?
end

#option_typesObject



97
98
99
# File 'app/models/comable/product.rb', line 97

def option_types
  variants.map { |variant| variant.option_values.map(&:option_type) }.flatten.uniq
end

#option_types_attributes=(_option_types_attributes) ⇒ Object



101
102
# File 'app/models/comable/product.rb', line 101

def option_types_attributes=(_option_types_attributes)
end

#priceObject



83
84
85
# File 'app/models/comable/product.rb', line 83

def price
  variants.first.try(:price)
end

#price=(price) ⇒ Object



87
88
89
# File 'app/models/comable/product.rb', line 87

def price=(price)
  variants.each { |v| v.price = price }
end

#propertiesObject



104
105
106
107
108
109
110
# File 'app/models/comable/product.rb', line 104

def properties
  parse_property = JSON.parse(property) # propertyに不正な値が入っていた場合に例外発生する
  return [] unless parse_property.is_a?(Array) && parse_property.all? { |prop| prop.is_a?(Hash) }
  parse_property
rescue
  []
end

#published?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/comable/product.rb', line 50

def published?
  published_at.present? && published_at <= Time.now
end

#sku_h_item_nameObject



67
68
69
# File 'app/models/comable/product.rb', line 67

def sku_h_item_name
  option_types.first.try(:name)
end

#sku_v_item_nameObject



71
72
73
# File 'app/models/comable/product.rb', line 71

def sku_v_item_name
  option_types.second.try(:name)
end

#stocked?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/comable/product.rb', line 42

def stocked?
  variants.any?(&:can_supply?)
end

#stocks=(stocks) ⇒ Object



93
94
95
# File 'app/models/comable/product.rb', line 93

def stocks=(stocks)
  stocks.map { |stock| variants.build(stocks: [stock]) }
end

#unstocked?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/models/comable/product.rb', line 46

def unstocked?
  !stocked?
end