Module: Tang::Customer
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/tang/customer.rb
Class Method Summary collapse
Instance Method Summary collapse
- #card ⇒ Object
- #coupon_end ⇒ Object
- #discount_for_plan(plan) ⇒ Object
- #generate_password ⇒ Object
- #has_coupon? ⇒ Boolean
- #has_subscription_coupon? ⇒ Boolean
-
#subscribed_to?(stripe_id) ⇒ Boolean
NOTE: May be causing memory bloat.
- #subscription ⇒ Object
- #update_card_from_stripe(stripe_card) ⇒ Object
- #update_subscription_end(subscription) ⇒ Object
Class Method Details
.search(query) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/models/concerns/tang/customer.rb', line 35 def self.search(query) customers = Tang.customer_class.none if query.present? q = "%#{query.downcase}%" customer_table = ActiveRecord::Base.connection.quote_table_name(Customer.table_name) customers = Tang.customer_class. joins(:subscriptions, :coupon). where.not(stripe_id: nil). where("lower(#{customer_table}.stripe_id) like ? or lower(#{customer_table}.email) like ? or lower(tang_subscriptions.stripe_id) like ? or lower(tang_coupons.stripe_id) like ?", q, q, q, q). distinct end return customers end |
.table_name ⇒ Object
31 32 33 |
# File 'app/models/concerns/tang/customer.rb', line 31 def self.table_name return Tang.customer_class.to_s.downcase.pluralize end |
Instance Method Details
#card ⇒ Object
50 51 52 |
# File 'app/models/concerns/tang/customer.rb', line 50 def card self.cards.order(:created_at).last end |
#coupon_end ⇒ Object
85 86 87 88 89 90 |
# File 'app/models/concerns/tang/customer.rb', line 85 def coupon_end if coupon.present? && coupon.duration != 'forever' end return nil end |
#discount_for_plan(plan) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'app/models/concerns/tang/customer.rb', line 105 def discount_for_plan(plan) amount_off = 0 if self.coupon.percent_off.present? amount_off = (self.coupon.percent_off.to_f / 100.0) * plan.amount.to_f elsif self.coupon.amount_off.present? amount_off = self.coupon.amount_off end return amount_off end |
#generate_password ⇒ Object
58 59 60 |
# File 'app/models/concerns/tang/customer.rb', line 58 def generate_password self.password = Devise.friendly_token.first(8) end |
#has_coupon? ⇒ Boolean
92 93 94 95 96 97 98 99 |
# File 'app/models/concerns/tang/customer.rb', line 92 def has_coupon? if self.subscription.present? if self.subscription.coupon.present? return true end end return self.coupon.present? end |
#has_subscription_coupon? ⇒ Boolean
101 102 103 |
# File 'app/models/concerns/tang/customer.rb', line 101 def has_subscription_coupon? return self.subscription.present? && self.subscription.coupon.present? end |
#subscribed_to?(stripe_id) ⇒ Boolean
NOTE: May be causing memory bloat
73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/models/concerns/tang/customer.rb', line 73 def subscribed_to?(stripe_id) subscription_plan = self.subscription.plan if self.subscription.present? if subscription_plan.present? return true if subscription_plan.stripe_id == stripe_id if Tang.plan_inheritance other_plan = Plan.find_by(stripe_id: stripe_id) return true if other_plan.present? && subscription_plan.order >= other_plan.order end end return false end |
#subscription ⇒ Object
54 55 56 |
# File 'app/models/concerns/tang/customer.rb', line 54 def subscription @subscription ||= self.subscriptions.where.not(status: :canceled).order(:created_at).last end |
#update_card_from_stripe(stripe_card) ⇒ Object
62 63 64 65 |
# File 'app/models/concerns/tang/customer.rb', line 62 def update_card_from_stripe(stripe_card) my_card = self.card.present? ? self.card : Card.new(customer: self) my_card.update_from_stripe(stripe_card) end |
#update_subscription_end(subscription) ⇒ Object
67 68 69 70 |
# File 'app/models/concerns/tang/customer.rb', line 67 def update_subscription_end(subscription) self.active_until = subscription.period_end self.save! end |