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

Instance Method Summary collapse

Methods included from SkuItem

#sku_h?, #sku_v?

Instance Method Details

#as_json(options = {}) ⇒ Object



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

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



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

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



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

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

#code=(code) ⇒ Object



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

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

#image_urlObject



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

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

#master?Boolean

Returns:

  • (Boolean)


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

def master?
  option_types.empty?
end

#option_typesObject



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

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

#option_types_attributes=(_option_types_attributes) ⇒ Object



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

def option_types_attributes=(_option_types_attributes)
end

#priceObject



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

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

#price=(price) ⇒ Object



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

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

#propertiesObject



95
96
97
98
99
100
101
# File 'app/models/comable/product.rb', line 95

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)


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

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

#sku_h_item_nameObject



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

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

#sku_v_item_nameObject



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

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

#stocked?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/models/comable/product.rb', line 33

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

#stocks=(stocks) ⇒ Object



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

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

#unstocked?Boolean

Returns:

  • (Boolean)


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

def unstocked?
  !stocked?
end