Class: Workarea::Admin::DiscountRulesViewModel
- Inherits:
-
ApplicationViewModel
- Object
- ApplicationViewModel
- Workarea::Admin::DiscountRulesViewModel
- Defined in:
- app/view_models/workarea/admin/discount_rules_view_model.rb
Instance Method Summary collapse
- #amount ⇒ Object
- #amount_type ⇒ Object
- #category_ids ⇒ Object
- #displayed_fields ⇒ Object
- #generated_codes_id ⇒ Object
- #ignored_fields ⇒ Object
- #item_quantity ⇒ Object
- #order_total ⇒ Object
- #order_total_operator ⇒ Object
- #product_id ⇒ Object
- #product_ids ⇒ Object
- #promo_codes ⇒ Object
- #to_h ⇒ Object
Instance Method Details
#amount ⇒ Object
21 22 23 24 25 26 27 |
# File 'app/view_models/workarea/admin/discount_rules_view_model.rb', line 21 def amount if model.respond_to?(:amount_type) && model.amount_type == :percent "#{model.amount.to_i}%" else ActionController::Base.helpers.number_to_currency(model.amount) end end |
#amount_type ⇒ Object
17 18 19 |
# File 'app/view_models/workarea/admin/discount_rules_view_model.rb', line 17 def amount_type nil end |
#category_ids ⇒ Object
43 44 45 |
# File 'app/view_models/workarea/admin/discount_rules_view_model.rb', line 43 def category_ids find_models(Catalog::Category, model.category_ids) end |
#displayed_fields ⇒ Object
71 72 73 |
# File 'app/view_models/workarea/admin/discount_rules_view_model.rb', line 71 def displayed_fields model.class.fields.except(*ignored_fields).keys end |
#generated_codes_id ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'app/view_models/workarea/admin/discount_rules_view_model.rb', line 62 def generated_codes_id if model.generated_codes_id.present? Pricing::Discount::CodeList .where(id: model.generated_codes_id) .first .try(:name) end end |
#ignored_fields ⇒ Object
75 76 77 |
# File 'app/view_models/workarea/admin/discount_rules_view_model.rb', line 75 def ignored_fields Workarea::Pricing::Discount.fields.keys end |
#item_quantity ⇒ Object
29 30 31 32 33 |
# File 'app/view_models/workarea/admin/discount_rules_view_model.rb', line 29 def item_quantity if model.item_quantity.to_i > 0 model.item_quantity end end |
#order_total ⇒ Object
57 58 59 60 |
# File 'app/view_models/workarea/admin/discount_rules_view_model.rb', line 57 def order_total return nil if model.order_total.blank? model.order_total > 0 ? model.order_total : nil end |
#order_total_operator ⇒ Object
51 52 53 54 55 |
# File 'app/view_models/workarea/admin/discount_rules_view_model.rb', line 51 def order_total_operator if model.order_total.present? && model.order_total > 0 model.order_total_operator.to_s.humanize.downcase end end |
#product_id ⇒ Object
35 36 37 |
# File 'app/view_models/workarea/admin/discount_rules_view_model.rb', line 35 def product_id nil end |
#product_ids ⇒ Object
39 40 41 |
# File 'app/view_models/workarea/admin/discount_rules_view_model.rb', line 39 def product_ids find_models(Catalog::Product, model.product_ids) end |
#promo_codes ⇒ Object
47 48 49 |
# File 'app/view_models/workarea/admin/discount_rules_view_model.rb', line 47 def promo_codes model.promo_codes.map(&:upcase).join(', ') end |
#to_h ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'app/view_models/workarea/admin/discount_rules_view_model.rb', line 4 def to_h displayed_fields.reduce({}) do |memo, field| value = if respond_to?(field) send(field) else model.send(field) end memo[clean_field_name(field)] = value if value.present? memo end end |