From 1f292983a68cf826699bfa53b1058d5d331e44b5 Mon Sep 17 00:00:00 2001 From: Rohit Paul Kuruvilla Date: Sat, 10 Jan 2015 16:16:49 +0530 Subject: [PATCH 1/2] Fixes to Plan & Account models Reorganize Plan model, Remove duplicated uniqueness validations, Fix seeds.rb Regression from #279, Remove payer_is_team_admin --- app/models/plan.rb | 48 +++++++++++++++++-------------------- app/models/teams/account.rb | 14 ++--------- db/seeds.rb | 10 +++++--- 3 files changed, 31 insertions(+), 41 deletions(-) diff --git a/app/models/plan.rb b/app/models/plan.rb index b0563fe1..e67bccc7 100644 --- a/app/models/plan.rb +++ b/app/models/plan.rb @@ -23,11 +23,10 @@ class Plan < ActiveRecord::Base has_many :subscriptions , class_name: 'Teams::AccountPlan' + before_create :generate_public_id after_create :register_on_stripe after_destroy :unregister_from_stripe - before_create :generate_public_id - CURRENCIES = %w(usd) MONTHLY = 'month' @@ -41,6 +40,7 @@ class Plan < ActiveRecord::Base scope :free, -> { where(amount: 0) } scope :with_analytics, -> { where(analytics: true) } scope :without_analytics, -> { where(analytics: false) } + class << self def enhanced_team_page_analytics monthly.paid.with_analytics.first @@ -59,8 +59,26 @@ def enhanced_team_page_free end end - alias_attribute :stripe_plan_id, :public_id + alias_attribute :has_analytics?, :analytics + + def price + amount / 100 + end + + def subscription? + !one_time? + end + + def free? + amount.zero? + end + + # TODO refactor + # We should avoid nil. + def one_time? + self.interval.nil? + end #copy to sidekiq worker def stripe_plan @@ -69,7 +87,6 @@ def stripe_plan nil end - #sidekiq it def register_on_stripe if subscription? @@ -95,29 +112,8 @@ def unregister_from_stripe end end - def price - amount / 100 - end - - - def subscription? - !one_time? - end - - def free? - amount.zero? - end - - # TODO refactor - # We should avoid nil. - def one_time? - self.interval.nil? - end - - alias_attribute :has_analytics?, :analytics - #TODO CHANGE with default in rails 4 def generate_public_id - self.public_id = SecureRandom.urlsafe_base64(4).downcase + self.public_id ||= SecureRandom.urlsafe_base64(4).downcase end end diff --git a/app/models/teams/account.rb b/app/models/teams/account.rb index b6bda7d6..21750bec 100644 --- a/app/models/teams/account.rb +++ b/app/models/teams/account.rb @@ -18,22 +18,12 @@ class Teams::Account < ActiveRecord::Base has_many :plans, through: :account_plans belongs_to :admin, class_name: 'User' - validates :team_id, presence: true, uniqueness: true validates_presence_of :stripe_card_token validates_presence_of :stripe_customer_token + validates :team_id, presence: true, uniqueness: true attr_protected :stripe_customer_token, :admin_id - validate :stripe_customer_token, presence: true - validate :stripe_card_token, presence: true - validate :admin_id, :payer_is_team_admin - - def payer_is_team_admin - if admin_id.nil? #or !team.admin?(admin) - errors.add(:admin_id, "must be team admin to create an account") - end - end - def subscribe_to!(plan, force=false) self.plan_ids = [plan.id] if force || update_on_stripe(plan) @@ -49,7 +39,7 @@ def save_with_payment(plan=nil) if valid? create_customer unless plan.try(:one_time?) subscribe_to!(plan) unless plan.nil? - team.save! + save! return true else return false diff --git a/db/seeds.rb b/db/seeds.rb index 6b0ea561..6a4aaf41 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -13,9 +13,13 @@ def self.create_network_for(name) end end +puts '---- NETWORKS ----' + S.create_network_for('Ruby') S.create_network_for('JavaScript') +puts '---- PLANS ----' + Plan.find_or_create_by_id(1) do |s| s.amount = 0 s.interval = 'month' @@ -116,19 +120,19 @@ def self.create_network_for(name) S.create_protip_for(bryce) do |p| p.title = 'Suspendisse potenti' p.body = '

Suspendisse potenti. Nunc iaculis risus vel ‘Orci Ornare’ dignissim sed vitae nulla. Nulla lobortis tempus commodo. Suspendisse potenti. Duis sagittis, est sit amet gravida tristique, purus lectus venenatis urna, id ‘molestie’ magna risus ut nunc. Donec tempus tempus tellus, ac HTML lacinia turpis mattis ac. Fusce ac sodales magna. Fusce ac sodales CSS magna.

' - p.topics = %w{suspendisse potenti} + p.topic_list = %w{suspendisse potenti} end S.create_protip_for(bryce) do |p| p.title = 'Vinyl Blue Bottle four loko wayfarers' p.body = 'Austin try-hard artisan, bicycle rights salvia squid dreamcatcher hoodie before they sold out Carles scenester ennui. Organic mumblecore Tumblr, gentrify retro 90\'s fanny pack flexitarian raw denim roof party cornhole. Hella direct trade mixtape +1 cliche, slow-carb Neutra craft beer tousled fap DIY.' - p.topics = %w{etsy hipster} + p.topic_list = %w{etsy hipster} end S.create_protip_for(lisa) do |p| p.title = 'Cras molestie risus a enim convallis vitae luctus libero lacinia' p.body = '

Cras molestie risus a enim convallis vitae luctus libero lacinia. Maecenas sit amet tellus nec mi gravida posuere non pretium magna. Nulla vel magna sit amet dui lobortis commodo vitae vel nulla.

' - p.topics = %w{cras molestie} + p.topic_list = %w{cras molestie} end puts '---- TEAMS ----' From aa09cb33b7e80206db75802c9e164ec664c4cad1 Mon Sep 17 00:00:00 2001 From: Rohit Paul Kuruvilla Date: Sat, 10 Jan 2015 16:38:02 +0530 Subject: [PATCH 2/2] Fix new subscriptions Strong Parameters for account params Fix if condition in save_with_payment When save_with_payment is called, stripe_customer_token might not be available, so we should check for just stripe_card_token. --- app/controllers/accounts_controller.rb | 8 ++++++-- app/models/teams/account.rb | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 42b4a2f0..5486a7e1 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -15,7 +15,7 @@ def new def create redirect_to teamname_path(slug: @team.slug) if @plan.free? - @account = @team.build_account(params[:account]) + @account = @team.build_account(account_params) @account.admin_id = current_user.id # TODO: (whatupdave) this doesn't look like it's being used any more. Remove if possible # @account.trial_end = Date.new(2013, 1, 1).to_time.to_i if session[:discount] == ENV['DISCOUNT_TOKEN'] @@ -38,7 +38,7 @@ def create end def update - if @account.update_attributes(params[:account]) && @account.save_with_payment(@plan) + if @account.update_attributes(account_params) && @account.save_with_payment(@plan) redirect_to new_team_opportunity_path(@team), notice: "You are subscribed to #{@plan.name}." + plan_capability(@plan, @team) else flash[:error] = @account.errors.full_messages.join("\n") @@ -107,4 +107,8 @@ def paying_user_context # Honeybadger.context(user_email: current_user.try(:email)) if current_user end + def account_params + params.require(:teams_account).permit(:stripe_card_token) + end + end diff --git a/app/models/teams/account.rb b/app/models/teams/account.rb index 21750bec..265c750e 100644 --- a/app/models/teams/account.rb +++ b/app/models/teams/account.rb @@ -36,7 +36,7 @@ def subscribe_to!(plan, force=false) end def save_with_payment(plan=nil) - if valid? + if stripe_card_token create_customer unless plan.try(:one_time?) subscribe_to!(plan) unless plan.nil? save!