Class: Workarea::Admin::DiscountViewModel

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CommentableViewModel

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

Class Method Details

.wrap(model, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'app/view_models/workarea/admin/discount_view_model.rb', line 6

def self.wrap(model, options = {})
  if model.is_a?(Enumerable)
    model.map { |m| wrap(m, options) }
  elsif NewDiscount.valid_class?(model.class)
    slug = model.class.name.demodulize
    view_model_class = "Workarea::Admin::Discounts::#{slug}ViewModel"
    view_model_class.constantize.new(model, options)
  else
    new(model, options)
  end
end

Instance Method Details

#amount_type_optionsObject



34
35
36
# File 'app/view_models/workarea/admin/discount_view_model.rb', line 34

def amount_type_options
  [[Money.default_currency.symbol, 'flat'], ['%', 'percent']]
end

#application_optionsObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/view_models/workarea/admin/discount_view_model.rb', line 68

def application_options
  [
    [
      t('workarea.admin.pricing_discounts.options.application_automatically'),
      nil
    ],
    [
      t('workarea.admin.pricing_discounts.options.application_promo_code'),
      'promo_code'
    ]
  ]
end

#code_list_optionsObject



100
101
102
103
104
105
106
# File 'app/view_models/workarea/admin/discount_view_model.rb', line 100

def code_list_options
  @code_list_options ||= [
    [t('workarea.admin.pricing_discounts.options.code_list_select'), nil]
  ] + Pricing::Discount::CodeList.all.map do |code_list|
    [code_list.name, code_list.id]
  end
end

#compatible_discountsObject



108
109
110
111
112
# File 'app/view_models/workarea/admin/discount_view_model.rb', line 108

def compatible_discounts
  @compatible_discounts ||= Pricing::Discount
    .where(:id.in => compatible_discount_ids)
    .to_a
end

#excluded_categoriesObject



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

def excluded_categories
  @excluded_categories ||= Catalog::Category.in(
    id: excluded_category_ids
  )
end

#excluded_productsObject



120
121
122
123
124
# File 'app/view_models/workarea/admin/discount_view_model.rb', line 120

def excluded_products
  @excluded_products ||= Catalog::Product.in(
    id: excluded_product_ids
  )
end

#insightsObject



18
19
20
# File 'app/view_models/workarea/admin/discount_view_model.rb', line 18

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

#max_applications_optionsObject



38
39
40
41
42
43
44
45
# File 'app/view_models/workarea/admin/discount_view_model.rb', line 38

def max_applications_options
  (1..5).to_a.unshift(
    [
      t('workarea.admin.pricing_discounts.options.unlimited_applications'),
      nil
    ]
  )
end

#order_total_operator_optionsObject



94
95
96
97
98
# File 'app/view_models/workarea/admin/discount_view_model.rb', line 94

def order_total_operator_options
  Pricing::Discount::Conditions::OrderTotal::OPERATORS.map do |op|
    [op.to_s.humanize.downcase, op]
  end
end

#promo_code_optionsObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/view_models/workarea/admin/discount_view_model.rb', line 47

def promo_code_options
  [
    [
      t('workarea.admin.pricing_discounts.options.promo_code_is'),
      'promo_codes_list'
    ],
    [
      t('workarea.admin.pricing_discounts.options.promo_code_is_from'),
      'generated_codes_id'
    ]
  ]
end

#rules_summaryObject



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

def rules_summary
  @rules_summary ||= DiscountRulesViewModel.new(model).to_h
end

#selected_application_optionObject



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

def selected_application_option
  if promo_code?
    'promo_code'
  end
end

#selected_promo_code_optionObject



60
61
62
63
64
65
66
# File 'app/view_models/workarea/admin/discount_view_model.rb', line 60

def selected_promo_code_option
  if generated_codes_id.present?
    'generated_codes_id'
  else
    'promo_codes_list'
  end
end

#slugObject



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

def slug
  model.class.name.demodulize.underscore
end

#timelineObject



22
23
24
# File 'app/view_models/workarea/admin/discount_view_model.rb', line 22

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

#workflow_paramsObject



87
88
89
90
91
92
# File 'app/view_models/workarea/admin/discount_view_model.rb', line 87

def workflow_params
  params = { discount: { name: model.name, tag_list: model.tag_list } }
  params.merge!(options.slice(:type, :discount))
  params[:id] = model.id if model.persisted?
  params
end