From e9ad3eff91f499ddb49155f6340aefcbbeab55ae Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Sat, 6 Jul 2019 03:17:51 -0500 Subject: [PATCH] this seems easier than expected --- app/models/billing/page_unlock_promo_code.rb | 5 +---- app/models/user.rb | 6 ++++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/models/billing/page_unlock_promo_code.rb b/app/models/billing/page_unlock_promo_code.rb index 34464411..81b37552 100644 --- a/app/models/billing/page_unlock_promo_code.rb +++ b/app/models/billing/page_unlock_promo_code.rb @@ -6,16 +6,13 @@ class PageUnlockPromoCode < ApplicationRecord page_types.split('|') & Rails.application.config.content_types[:all].map(&:name) end - def already_activated_users - promotions.select(:user_id) - end def activate!(user) return false unless uses_remaining > 0 # Make sure this user hasn't activated this promo code before return false unless user.present? - # return if users.where(id: user.id).any? + return false if users.pluck(:id).include?(user.id) # Activate! # technically two requests at the same time could still double-activate at diff --git a/app/models/user.rb b/app/models/user.rb index 60909e92..6641d6e0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -25,6 +25,7 @@ class User < ApplicationRecord end has_many :promotions + has_many :image_uploads has_one :referral_code, dependent: :destroy @@ -133,6 +134,7 @@ class User < ApplicationRecord "https://www.gravatar.com/avatar/#{email_md5}?d=identicon&s=#{size}".html_safe end + # TODO these (3) can probably all be scopes on the related object, no? def active_subscriptions subscriptions .where('start_date < ?', Time.now) @@ -144,6 +146,10 @@ class User < ApplicationRecord BillingPlan.where(id: billing_plan_ids).uniq end + def active_promotions + promotions.active + end + def initialize_stripe_customer if self.stripe_customer_id.nil? customer_data = Stripe::Customer.create(email: self.email)