Skip to content

Commit 1f29298

Browse files
committed
Fixes to Plan & Account models
Reorganize Plan model, Remove duplicated uniqueness validations, Fix seeds.rb Regression from coderwall#279, Remove payer_is_team_admin
1 parent 14c8682 commit 1f29298

File tree

3 files changed

+31
-41
lines changed

3 files changed

+31
-41
lines changed

app/models/plan.rb

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@
2323
class Plan < ActiveRecord::Base
2424
has_many :subscriptions , class_name: 'Teams::AccountPlan'
2525

26+
before_create :generate_public_id
2627
after_create :register_on_stripe
2728
after_destroy :unregister_from_stripe
2829

29-
before_create :generate_public_id
30-
3130
CURRENCIES = %w(usd)
3231
MONTHLY = 'month'
3332

@@ -41,6 +40,7 @@ class Plan < ActiveRecord::Base
4140
scope :free, -> { where(amount: 0) }
4241
scope :with_analytics, -> { where(analytics: true) }
4342
scope :without_analytics, -> { where(analytics: false) }
43+
4444
class << self
4545
def enhanced_team_page_analytics
4646
monthly.paid.with_analytics.first
@@ -59,8 +59,26 @@ def enhanced_team_page_free
5959
end
6060
end
6161

62-
6362
alias_attribute :stripe_plan_id, :public_id
63+
alias_attribute :has_analytics?, :analytics
64+
65+
def price
66+
amount / 100
67+
end
68+
69+
def subscription?
70+
!one_time?
71+
end
72+
73+
def free?
74+
amount.zero?
75+
end
76+
77+
# TODO refactor
78+
# We should avoid nil.
79+
def one_time?
80+
self.interval.nil?
81+
end
6482

6583
#copy to sidekiq worker
6684
def stripe_plan
@@ -69,7 +87,6 @@ def stripe_plan
6987
nil
7088
end
7189

72-
7390
#sidekiq it
7491
def register_on_stripe
7592
if subscription?
@@ -95,29 +112,8 @@ def unregister_from_stripe
95112
end
96113
end
97114

98-
def price
99-
amount / 100
100-
end
101-
102-
103-
def subscription?
104-
!one_time?
105-
end
106-
107-
def free?
108-
amount.zero?
109-
end
110-
111-
# TODO refactor
112-
# We should avoid nil.
113-
def one_time?
114-
self.interval.nil?
115-
end
116-
117-
alias_attribute :has_analytics?, :analytics
118-
119115
#TODO CHANGE with default in rails 4
120116
def generate_public_id
121-
self.public_id = SecureRandom.urlsafe_base64(4).downcase
117+
self.public_id ||= SecureRandom.urlsafe_base64(4).downcase
122118
end
123119
end

app/models/teams/account.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,12 @@ class Teams::Account < ActiveRecord::Base
1818
has_many :plans, through: :account_plans
1919
belongs_to :admin, class_name: 'User'
2020

21-
validates :team_id, presence: true, uniqueness: true
2221
validates_presence_of :stripe_card_token
2322
validates_presence_of :stripe_customer_token
23+
validates :team_id, presence: true, uniqueness: true
2424

2525
attr_protected :stripe_customer_token, :admin_id
2626

27-
validate :stripe_customer_token, presence: true
28-
validate :stripe_card_token, presence: true
29-
validate :admin_id, :payer_is_team_admin
30-
31-
def payer_is_team_admin
32-
if admin_id.nil? #or !team.admin?(admin)
33-
errors.add(:admin_id, "must be team admin to create an account")
34-
end
35-
end
36-
3727
def subscribe_to!(plan, force=false)
3828
self.plan_ids = [plan.id]
3929
if force || update_on_stripe(plan)
@@ -49,7 +39,7 @@ def save_with_payment(plan=nil)
4939
if valid?
5040
create_customer unless plan.try(:one_time?)
5141
subscribe_to!(plan) unless plan.nil?
52-
team.save!
42+
save!
5343
return true
5444
else
5545
return false

db/seeds.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ def self.create_network_for(name)
1313
end
1414
end
1515

16+
puts '---- NETWORKS ----'
17+
1618
S.create_network_for('Ruby')
1719
S.create_network_for('JavaScript')
1820

21+
puts '---- PLANS ----'
22+
1923
Plan.find_or_create_by_id(1) do |s|
2024
s.amount = 0
2125
s.interval = 'month'
@@ -116,19 +120,19 @@ def self.create_network_for(name)
116120
S.create_protip_for(bryce) do |p|
117121
p.title = 'Suspendisse potenti'
118122
p.body = '<p>Suspendisse potenti. Nunc iaculis risus vel &#8216;Orci Ornare&#8217; dignissim sed vitae nulla. Nulla lobortis tempus commodo. Suspendisse <em>potenti</em>. Duis sagittis, est sit amet gravida tristique, purus lectus venenatis urna, id &#8216;molestie&#8217; magna risus ut nunc. Donec tempus tempus tellus, ac <abbr title="Hypertext Markup Language">HTML</abbr> lacinia turpis mattis ac. Fusce ac sodales magna. Fusce ac sodales <abbr title="Cascading Style Sheets">CSS</abbr> magna.</p>'
119-
p.topics = %w{suspendisse potenti}
123+
p.topic_list = %w{suspendisse potenti}
120124
end
121125

122126
S.create_protip_for(bryce) do |p|
123127
p.title = 'Vinyl Blue Bottle four loko wayfarers'
124128
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.'
125-
p.topics = %w{etsy hipster}
129+
p.topic_list = %w{etsy hipster}
126130
end
127131

128132
S.create_protip_for(lisa) do |p|
129133
p.title = 'Cras molestie risus a enim convallis vitae luctus libero lacinia'
130134
p.body = '<p>Cras molestie risus a enim convallis vitae luctus libero lacinia. Maecenas sit <q cite="http://www.heydonworks.com">amet tellus nec mi gravida posuere</q> non pretium magna. Nulla vel magna sit amet dui <a href="#">lobortis</a> commodo vitae vel nulla. </p>'
131-
p.topics = %w{cras molestie}
135+
p.topic_list = %w{cras molestie}
132136
end
133137

134138
puts '---- TEAMS ----'

0 commit comments

Comments
 (0)