Class: SolidusFriendlyPromotions::Promotion
- Inherits:
-
Spree::Base
- Object
- Spree::Base
- SolidusFriendlyPromotions::Promotion
- Includes:
- Spree::SoftDeletable
- Defined in:
- app/models/solidus_friendly_promotions/promotion.rb
Class Method Summary collapse
Instance Method Summary collapse
- #active?(time = Time.current) ⇒ Boolean
-
#discounted_orders ⇒ Object
All orders that have been discounted using this promotion.
- #eligibility_results ⇒ Object
- #expired?(time = Time.current) ⇒ Boolean
- #inactive?(time = Time.current) ⇒ Boolean
- #not_expired?(time = Time.current) ⇒ Boolean
- #not_started?(time = Time.current) ⇒ Boolean
- #products ⇒ Object
- #started?(time = Time.current) ⇒ Boolean
-
#usage_count(excluded_orders: []) ⇒ Integer
Number of times the code has been used overall.
-
#usage_limit_exceeded?(excluded_orders: []) ⇒ Boolean
Whether the promotion has exceeded its usage restrictions.
- #used_by?(user, excluded_orders = []) ⇒ Boolean
Class Method Details
.human_enum_name(enum_name, enum_value) ⇒ Object
42 43 44 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 42 def self.human_enum_name(enum_name, enum_value) I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{enum_name.to_s.pluralize}.#{enum_value}") end |
.lane_options ⇒ Object
46 47 48 49 50 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 46 def self. ordered_lanes.map do |lane_name, _index| [human_enum_name(:lane, lane_name), lane_name] end end |
.ordered_lanes ⇒ Object
52 53 54 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 52 def self.ordered_lanes lanes.sort_by(&:last).to_h end |
Instance Method Details
#active?(time = Time.current) ⇒ Boolean
116 117 118 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 116 def active?(time = Time.current) started?(time) && not_expired?(time) && benefits.present? end |
#discounted_orders ⇒ Object
All orders that have been discounted using this promotion
61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 61 def discounted_orders Spree::Order .joins(:all_adjustments) .where( spree_adjustments: { source_type: "SolidusFriendlyPromotions::Benefit", source_id: benefits.map(&:id), eligible: true } ).distinct end |
#eligibility_results ⇒ Object
132 133 134 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 132 def eligibility_results @eligibility_results ||= SolidusFriendlyPromotions::EligibilityResults.new(self) end |
#expired?(time = Time.current) ⇒ Boolean
124 125 126 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 124 def expired?(time = Time.current) expires_at.present? && expires_at < time end |
#inactive?(time = Time.current) ⇒ Boolean
120 121 122 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 120 def inactive?(time = Time.current) !active?(time) end |
#not_expired?(time = Time.current) ⇒ Boolean
104 105 106 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 104 def not_expired?(time = Time.current) !expired?(time) end |
#not_started?(time = Time.current) ⇒ Boolean
108 109 110 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 108 def not_started?(time = Time.current) !started?(time) end |
#products ⇒ Object
128 129 130 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 128 def products conditions.where(type: "SolidusFriendlyPromotions::Conditions::Product").flat_map(&:products).uniq end |
#started?(time = Time.current) ⇒ Boolean
112 113 114 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 112 def started?(time = Time.current) starts_at.nil? || starts_at < time end |
#usage_count(excluded_orders: []) ⇒ Integer
Number of times the code has been used overall
77 78 79 80 81 82 83 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 77 def usage_count(excluded_orders: []) discounted_orders .complete .where.not(id: [excluded_orders.map(&:id)]) .where.not(spree_orders: {state: :canceled}) .count end |
#usage_limit_exceeded?(excluded_orders: []) ⇒ Boolean
Whether the promotion has exceeded its usage restrictions.
98 99 100 101 102 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 98 def usage_limit_exceeded?(excluded_orders: []) return unless usage_limit usage_count(excluded_orders: excluded_orders) >= usage_limit end |
#used_by?(user, excluded_orders = []) ⇒ Boolean
85 86 87 88 89 90 91 92 |
# File 'app/models/solidus_friendly_promotions/promotion.rb', line 85 def used_by?(user, excluded_orders = []) discounted_orders .complete .where.not(id: excluded_orders.map(&:id)) .where(user: user) .where.not(spree_orders: {state: :canceled}) .exists? end |