Skip to content

Commit

Permalink
Merge pull request #139 from cobudget/epic/the-last-sprint
Browse files Browse the repository at this point in the history
[WIP] Epic/the last sprint
  • Loading branch information
data-doge committed Apr 21, 2016
2 parents 6c14142 + 48054f1 commit 00f3d8f
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 19 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ gem 'rails', '~> 4.2.3'
gem 'rails-api'
gem 'spring', :group => :development

# payments
gem 'stripe'

# persistance
gem 'pg'
gem 'foreigner'
Expand Down
15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ GEM
rails (< 6)
diff-lcs (1.2.5)
docile (1.1.5)
domain_name (0.5.20160310)
unf (>= 0.0.5, < 1.0.0)
erubis (2.7.0)
factory_girl (4.5.0)
activesupport (>= 3.0.0)
Expand All @@ -99,6 +101,8 @@ GEM
activesupport (>= 3)
hashie (3.4.2)
highline (1.7.2)
http-cookie (1.0.2)
domain_name (~> 0.5)
i18n (0.7.0)
json (1.8.3)
loofah (2.0.2)
Expand Down Expand Up @@ -126,6 +130,7 @@ GEM
net-ssh (2.9.2)
net-ssh-gateway (1.2.0)
net-ssh (>= 2.6.5)
netrc (0.11.0)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
omniauth (1.2.2)
Expand Down Expand Up @@ -192,6 +197,10 @@ GEM
redcarpet (3.3.3)
responders (2.1.0)
railties (>= 4.2.0, < 5)
rest-client (1.8.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
rspec-core (3.3.1)
rspec-support (~> 3.3.0)
rspec-expectations (3.3.0)
Expand Down Expand Up @@ -227,12 +236,17 @@ GEM
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0)
stripe (1.41.0)
rest-client (~> 1.4)
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.1)
timecop (0.7.4)
tzinfo (1.2.2)
thread_safe (~> 0.1)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.2)
warden (1.2.6)
rack (>= 1.0)

Expand Down Expand Up @@ -274,4 +288,5 @@ DEPENDENCIES
simplecov
sinatra
spring
stripe
timecop
2 changes: 1 addition & 1 deletion app/controllers/buckets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ def bucket_params_create
end

def bucket_params_update
params.require(:bucket).permit(:name, :description, :target)
params.require(:bucket).permit(:name, :description, :target, :status)
end
end
35 changes: 34 additions & 1 deletion app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,41 @@ def update
render json: [group]
end

api :POST, '/groups/:id/add_customer', 'Add a credit card that pays for the group'
def add_customer
group = Group.find(params[:id])
if current_user.is_admin_for(group)
group.add_customer(current_user.email)
render json: [group]
else
render nothing: true, status: 403
end
end

api :POST, '/groups/:id/add_card', 'Add a credit card that pays for the group'
def add_card
group = Group.find(params[:id])
if current_user.is_admin_for?(group)
group.add_card(params[:stripeEmail], params[:stripeToken])
render status: 200, nothing: true
else
render status: 403, nothing: true
end
end

api :POST, '/groups/:id/extend_trial', 'Extend the group trial by 30 days'
def extend_trial
group = Group.find(params[:id])
if current_user.is_admin_for?(group)
group.extend_trial()
render status: 200, nothing: true
else
render status: 403, nothing: true
end
end

private
def group_params
params.require(:group).permit(:name, :currency_code)
params.require(:group).permit(:name, :currency_code, :plan)
end
end
2 changes: 1 addition & 1 deletion app/controllers/memberships_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def upload_review

private
def membership_params
params.require(:membership).permit(:is_admin)
params.require(:membership).permit(:is_admin, :closed_admin_help_card_at, :closed_member_help_card_at)
end

def membership
Expand Down
35 changes: 35 additions & 0 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,37 @@ def add_admin(user)
end
end

def add_customer(email)
customer = Stripe::Customer.create(
:description => "Customer for Group " + self.id.to_s,
:email => email,
:plan => 1
)
self.customer_id = customer.id
self.trial_end = Time.at(customer.subscriptions.data[0].trial_end)
self.plan = "paid"
self.save
end

def add_card(email, token)
customer = Stripe::Customer.retrieve(self.customer_id)
customer.source = token
customer.email = email
customer.save
end

def extend_trial
# the number of seconds in thirty days
thirty_days = 60 * 24 * 30

customer = Stripe::Customer.retrieve(self.customer_id)
sub = Stripe::Subscription.retrieve(customer.subscriptions.data[0].id)
sub.trial_end = sub.trial_end + thirty_days
sub.save
self.trial_end = Time.at(sub.trial_end)
self.save
end

def add_member(user)
memberships.create!(member: user, is_admin: false)
end
Expand All @@ -28,6 +59,10 @@ def formatted_balance
Money.new(balance * 100, currency_code).format
end

def is_launched
members.any? && allocations.any?
end

def last_activity_at
last_bucket = buckets.order(updated_at: :desc).limit(1).first
last_bucket_updated_at = last_bucket ? last_bucket.updated_at : nil
Expand Down
5 changes: 4 additions & 1 deletion app/serializers/group_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ class GroupSerializer < ActiveModel::Serializer
:name,
:balance,
:currency_symbol,
:currency_code
:currency_code,
:is_launched,
:plan,
:trial_end
end
4 changes: 3 additions & 1 deletion app/serializers/membership_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ class MembershipSerializer < ActiveModel::Serializer
:created_at,
:balance,
:archived_at,
:raw_balance
:raw_balance,
:closed_admin_help_card_at,
:closed_member_help_card_at

has_one :member, serializer: UserSerializer, root: 'users'
has_one :group, serializer: GroupSerializer, root: 'groups'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ul style="list-style: none">
<% buckets.each do |bucket| %>
<li style="margin-bottom: 10px">
<a href="<%= "#{root_url}#/buckets/#{bucket.id}" %>"><b><%= bucket.name %> (<%= bucket.formatted_target %>)</b></a><br>
<a href="<%= "#{root_url}#/buckets/#{bucket.id}" %>"><b><%= bucket.name %> (<%= bucket.formatted_total_contributions %>)</b></a><br>
<%= bucket.num_of_contributors %> funders
</li>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ul style="list-style: none">
<% buckets.each do |bucket| %>
<li style="margin-bottom: 10px">
<a href="<%= "#{root_url}#/buckets/#{bucket.id}" %>"><b><%= bucket.name %> (<%= bucket.formatted_target %>)</b></a><br>
<a href="<%= "#{root_url}#/buckets/#{bucket.id}" %>"><b><%= bucket.name %> (<%= bucket.formatted_total_contributions %>)</b></a><br>
By <%= bucket.author_name %><br>
<%= bucket.num_of_contributors %> funders
</li>
Expand Down
6 changes: 6 additions & 0 deletions config/initializers/stripe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Rails.configuration.stripe = {
:publishable_key => ENV['STRIPE_PUBLISHABLE_KEY'],
:secret_key => ENV['STRIPE_SECRET_KEY']
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]
9 changes: 8 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@
end
end

resources :groups, only: [:index, :show, :create, :update]

resources :groups, only: [:index, :show, :create, :update] do
member do
post :add_customer
post :add_card
post :extend_trial
end
end

resources :comments, only: [:index, :create]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class AddClosedHelpCardTimestampsToMemberships < ActiveRecord::Migration
def up
add_column :memberships, :closed_member_help_card_at, :datetime
add_column :memberships, :closed_admin_help_card_at, :datetime
Membership.where(is_admin: false)
.joins(:member).where.not(users: {confirmed_at: nil})
.update_all(closed_member_help_card_at: DateTime.now.utc)
Membership.where(is_admin: true)
.joins(:member).where.not(users: {confirmed_at: nil})
.update_all(closed_admin_help_card_at: DateTime.now.utc)
end

def down
remove_column :memberships, :closed_member_help_card_at
remove_column :memberships, :closed_admin_help_card_at
end
end
7 changes: 7 additions & 0 deletions db/migrate/20160419140651_add_customer_to_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddCustomerToGroup < ActiveRecord::Migration
def change
add_column :groups, :customer_id, :string
add_column :groups, :trial_end, :datetime
add_column :groups, :plan, :string
end
end
52 changes: 42 additions & 10 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160412073353) do
ActiveRecord::Schema.define(version: 20160419140651) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -83,43 +83,65 @@

add_index "delayed_jobs", ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree

create_table "events", force: :cascade do |t|
t.string "kind", limit: 255
t.integer "eventable_id"
t.string "eventable_type"
t.integer "user_id"
t.integer "group_id"
t.integer "sequence_id"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "events", ["eventable_type", "eventable_id"], name: "index_events_on_eventable_type_and_eventable_id", using: :btree
add_index "events", ["group_id", "sequence_id"], name: "index_events_on_group_id_and_sequence_id", unique: true, using: :btree
add_index "events", ["group_id"], name: "index_events_on_group_id", using: :btree
add_index "events", ["sequence_id"], name: "index_events_on_sequence_id", using: :btree
add_index "events", ["user_id"], name: "index_events_on_user_id", using: :btree

create_table "groups", force: :cascade do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.string "currency_symbol", default: "$"
t.string "currency_code", default: "USD"
t.string "customer_id"
t.datetime "trial_end"
t.string "plan"
end

create_table "memberships", force: :cascade do |t|
t.integer "group_id", null: false
t.integer "member_id", null: false
t.boolean "is_admin", default: false, null: false
t.integer "group_id", null: false
t.integer "member_id", null: false
t.boolean "is_admin", default: false, null: false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "archived_at"
t.datetime "closed_member_help_card_at"
t.datetime "closed_admin_help_card_at"
end

add_index "memberships", ["group_id"], name: "index_memberships_on_group_id", using: :btree
add_index "memberships", ["member_id"], name: "index_memberships_on_member_id", using: :btree

create_table "subscription_trackers", force: :cascade do |t|
t.integer "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "subscribed_to_email_notifications", default: false
t.string "email_digest_delivery_frequency", default: "never"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "subscription_trackers", ["user_id"], name: "index_subscription_trackers_on_user_id", using: :btree

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
Expand All @@ -134,10 +156,20 @@
t.integer "utc_offset"
t.datetime "confirmed_at"
t.datetime "joined_first_group_at"
t.boolean "is_super_admin", default: false
t.boolean "is_super_admin", default: false
end

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

create_table "webhooks", force: :cascade do |t|
t.integer "hookable_id"
t.string "hookable_type"
t.string "kind", null: false
t.string "uri", null: false
t.text "event_types", default: [], array: true
end

add_index "webhooks", ["hookable_type", "hookable_id"], name: "index_webhooks_on_hookable_type_and_hookable_id", using: :btree

end
1 change: 0 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
target: rand(0..1000),
status: 'live',
created_at: Time.zone.now - rand(1..10).days,
funding_closes_at: Time.zone.now + rand(10..30).days,
live_at: Time.now.utc
)
rand(10).times { bucket.comments.create(user: group.members.sample, body: Faker::Lorem.sentence) }
Expand Down

0 comments on commit 00f3d8f

Please sign in to comment.