Class: Workarea::Admin::ProductViewModel

Inherits:
ApplicationViewModel
  • Object
show all
Includes:
CommentableViewModel
Defined in:
app/view_models/workarea/admin/product_view_model.rb

Instance Method Summary collapse

Methods included from CommentableViewModel

#comment_count, #comments, #has_comments?, #new_comments_for?, #subscribed_users

Instance Method Details

#available_inventoryObject



77
78
79
# File 'app/view_models/workarea/admin/product_view_model.rb', line 77

def available_inventory
  @available_inventory ||= Inventory.total_available(*model.skus)
end

#categoriesObject



21
22
23
# File 'app/view_models/workarea/admin/product_view_model.rb', line 21

def categories
  @categories ||= CategoryViewModel.wrap(categorization.to_models)
end

#categorizationObject



17
18
19
# File 'app/view_models/workarea/admin/product_view_model.rb', line 17

def categorization
  @categorization ||= Categorization.new(model)
end

#contentObject



119
120
121
# File 'app/view_models/workarea/admin/product_view_model.rb', line 119

def content
  @content ||= Content.for(model)
end

#customization_optionsObject



109
110
111
# File 'app/view_models/workarea/admin/product_view_model.rb', line 109

def customization_options
  default_customization_options + configured_customization_options
end

#default_categoryObject



35
36
37
38
39
40
# File 'app/view_models/workarea/admin/product_view_model.rb', line 35

def default_category
  return @default_category if defined? @default_category
  @default_category = if categorization.default_model.present?
    CategoryViewModel.wrap(categorization.default_model)
  end
end

#displayable?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'app/view_models/workarea/admin/product_view_model.rb', line 136

def displayable?
  active? && inventory.any?(&:displayable?)
end


30
31
32
33
# File 'app/view_models/workarea/admin/product_view_model.rb', line 30

def featured_categories
  @featured_categories ||=
    categories.select { |c| c.featured_product?(model.id) }
end

#ignore_inventory?Boolean

Returns:

  • (Boolean)


81
82
83
84
# File 'app/view_models/workarea/admin/product_view_model.rb', line 81

def ignore_inventory?
  return @ignore_inventory if defined?(@ignore_inventory)
  @ignore_inventory ||= inventory.all?(&:ignore?)
end

#images_by_optionObject



113
114
115
116
117
# File 'app/view_models/workarea/admin/product_view_model.rb', line 113

def images_by_option
  images.asc(:position).group_by do |image|
    image.option.to_s.titleize
  end
end

#insightsObject



127
128
129
# File 'app/view_models/workarea/admin/product_view_model.rb', line 127

def insights
  @insights ||= Insights::ProductViewModel.wrap(model, @options)
end

#inventoryObject



131
132
133
134
# File 'app/view_models/workarea/admin/product_view_model.rb', line 131

def inventory
  @inventory ||=
    options[:inventory] || Inventory::Collection.new(model.skus)
end

#inventory_messageObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/view_models/workarea/admin/product_view_model.rb', line 140

def inventory_message
  @inventory_message ||=
    if !active?
      t('workarea.admin.featured_products.statuses.inactive')
    elsif inventory.backordered?
      t('workarea.admin.featured_products.statuses.backordered')
    elsif inventory.out_of_stock?
      t('workarea.admin.featured_products.statuses.out_of_stock')
    elsif inventory.low_inventory?
      t(
        'workarea.admin.featured_products.statuses.low_inventory',
        count: inventory.available_to_sell
      )
    else
      ''
    end
end

#one_price?Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'app/view_models/workarea/admin/product_view_model.rb', line 55

def one_price?
  return false if sell_min_price.nil? || original_min_price.nil?
  sell_min_price >= original_min_price
end

#optionsObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/view_models/workarea/admin/product_view_model.rb', line 96

def options
  variants.reduce({}) do |memo, variant|
    variant.details.each do |name, value|
      memo[name] ||= []
      memo[name] << value
      memo[name].flatten!
      memo[name].uniq!
    end

    memo
  end
end

#pricingObject



49
50
51
52
53
# File 'app/view_models/workarea/admin/product_view_model.rb', line 49

def pricing
  @pricing ||= options[:pricing] || Pricing::Collection.new(
    options[:sku].presence || model.variants.map(&:sku)
  )
end

#pricing?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/view_models/workarea/admin/product_view_model.rb', line 65

def pricing?
  storefront_view_model.present?
end

#rules_categoriesObject



25
26
27
28
# File 'app/view_models/workarea/admin/product_view_model.rb', line 25

def rules_categories
  @rules_categories ||=
    categories.reject { |c| c.featured_product?(model.id) }
end

#salesObject



73
74
75
# File 'app/view_models/workarea/admin/product_view_model.rb', line 73

def sales
  @sales ||= Inventory.total_sales(*model.skus)
end

#show_sell_range?Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'app/view_models/workarea/admin/product_view_model.rb', line 60

def show_sell_range?
  return false if sell_min_price.nil? || sell_max_price.nil?
  sell_min_price < sell_max_price
end

#storefront_recommendationsObject



123
124
125
# File 'app/view_models/workarea/admin/product_view_model.rb', line 123

def storefront_recommendations
  storefront_view_model.recommendations
end

#storefront_view_modelObject



42
43
44
45
46
47
# File 'app/view_models/workarea/admin/product_view_model.rb', line 42

def storefront_view_model
  @storefront_view_model ||= Storefront::ProductViewModel.wrap(
    model,
    @options
  )
end

#templatesObject



86
87
88
89
90
91
92
93
94
# File 'app/view_models/workarea/admin/product_view_model.rb', line 86

def templates
  [
    [t('workarea.admin.catalog_products.templates.generic'), 'generic']
  ] + Workarea.config.product_templates.reject do |template|
    template.to_s.include?('test')
  end.sort.map(&:to_s).map do |template_name|
    [template_name.titleize, template_name.optionize]
  end
end

#timelineObject



13
14
15
# File 'app/view_models/workarea/admin/product_view_model.rb', line 13

def timeline
  @timeline ||= TimelineViewModel.new(model)
end

#variant_sell_price(variant) ⇒ Object



69
70
71
# File 'app/view_models/workarea/admin/product_view_model.rb', line 69

def variant_sell_price(variant)
  pricing.for_sku(variant.sku).sell
end