track campaign progress

This commit is contained in:
Emi Matchu 2014-09-11 17:40:37 -05:00
parent 04a328e6ee
commit 8e22c271a4
11 changed files with 70 additions and 43 deletions

View file

@ -1,14 +1,13 @@
=campaign-progress
.campaign-progress-wrapper
+border-radius(8px)
background: #aaa
background-image: linear-gradient(color-stops(#ccc, #aaa))
background: desaturate(lighten($module-border-color, 30%), 85%)
background-image: linear-gradient(color-stops(desaturate(lighten($module-border-color, 50%), 85%), desaturate(lighten($module-border-color, 30%), 85%)))
border: 4px solid $module-border-color
clear: both
margin-bottom: 1.5em
position: relative
visibility: hidden
.button
+loud-awesome-button-color

View file

@ -1,6 +1,8 @@
class DonationsController < ApplicationController
def create
@donation = Donation.create_from_charge(current_user, params[:donation])
@campaign = Campaign.current
@donation = Donation.create_from_charge(
@campaign, current_user, params[:donation])
redirect_to @donation
end

View file

@ -1,6 +1,8 @@
class StaticController < ApplicationController
def donate
# TODO: scope by campaign?
@donations = Donation.includes(features: :outfit).order('created_at DESC')
@campaign = Campaign.current
@donations = @campaign.donations.includes(features: :outfit).
order('created_at DESC')
end
end

View file

@ -20,21 +20,19 @@ module ApplicationHelper
end
end
CAMPAIGN_ACTIVE = false
def campaign_progress(options={}, &block)
if CAMPAIGN_ACTIVE || options[:always]
include_campaign_progress_requirements
def campaign_progress(campaign, &block)
if campaign
if block_given?
content = capture(&block)
else
content = link_to('We made it! Image Mode has been released.', donate_path) +
link_to('Read more', donate_path, :class => 'button')
content = link_to('Help Dress to Impress stay online!', donate_path) +
link_to('Learn more', donate_path, :class => 'button')
end
html = content_tag(:div, nil, :class => 'campaign-progress') +
content_tag(:div, content, :class => 'campaign-progress-label')
content_tag(:div, html, :class => 'campaign-progress-wrapper')
meter = content_tag(:div, nil, :class => 'campaign-progress',
style: "width: #{campaign.progress_percent}%;")
label = content_tag(:div, content, :class => 'campaign-progress-label')
content_tag(:div, meter + label, :class => 'campaign-progress-wrapper')
end
end
@ -59,21 +57,6 @@ module ApplicationHelper
end)
end
def include_campaign_progress_requirements
unless @included_campaign_progress_requirements
content_for(:javascripts,
include_javascript_libraries(:jquery) +
javascript_include_tag('pledgie')
)
content_for(:meta,
tag(:meta, :name => 'pledgie-campaign-id', :content => PLEDGIE_CAMPAIGN_ID)
)
@included_campaign_progress_requirements = true
end
end
def hide_home_link
@hide_home_link = true
end

14
app/models/campaign.rb Normal file
View file

@ -0,0 +1,14 @@
class Campaign < ActiveRecord::Base
attr_accessible :active, :goal, :progress
has_many :donations
def progress_percent
(progress.to_f / goal) * 100
end
def self.current
self.where(active: true).first or
raise ActiveRecord::RecordNotFound.new("no current campaign")
end
end

View file

@ -3,6 +3,7 @@ class Donation < ActiveRecord::Base
attr_accessible :donor_name
belongs_to :campaign
belongs_to :user
has_many :features, class_name: 'DonationFeature'
@ -10,9 +11,11 @@ class Donation < ActiveRecord::Base
"#{id}-#{secret}"
end
def self.create_from_charge(user, params)
def self.create_from_charge(campaign, user, params)
amount = (BigDecimal.new(params[:amount]) * 100).floor
campaign.progress += amount
customer = Stripe::Customer.create(
card: params[:stripe_token]
)
@ -24,10 +27,10 @@ class Donation < ActiveRecord::Base
:currency => 'usd'
)
donation = Donation.new
donation = campaign.donations.build
donation.amount = amount
donation.charge_id = charge.id
donation.user_id = user.try(:id)
donation.user = user
donation.donor_name = user.try(:name)
donation.donor_email = params[:donor_email]
donation.secret = new_secret
@ -39,6 +42,7 @@ class Donation < ActiveRecord::Base
end
Donation.transaction do
campaign.save!
donation.save!
features.each(&:save!)
end

View file

@ -1,10 +1,10 @@
- title "Support Dress to Impress"
= campaign_progress(:always => true) do
We've received
= precede '$' do
%span.campaign-raised
toward our hosting costs this year. Thanks so much!
= campaign_progress(@campaign) do
We've received #{number_to_currency(@campaign.progress / 100.0)}
toward our hosting costs this year.
- if @campaign.progress > 0
Thanks so much!
= form_tag donations_path, method: 'POST', id: 'donation-form',
'data-checkout-image' => image_path('default_preview.png'),

View file

@ -0,0 +1,11 @@
class CreateCampaigns < ActiveRecord::Migration
def change
create_table :campaigns do |t|
t.integer :progress, null: false, default: 0
t.integer :goal, null: false
t.boolean :active, null: false
t.timestamps
end
end
end

View file

@ -0,0 +1,5 @@
class AddCampaignIdToDonation < ActiveRecord::Migration
def change
add_column :donations, :campaign_id, :integer, null: false
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20140910181819) do
ActiveRecord::Schema.define(:version => 20140910204043) do
create_table "auth_servers", :force => true do |t|
t.string "short_name", :limit => 10, :null => false
@ -33,10 +33,11 @@ ActiveRecord::Schema.define(:version => 20140910181819) do
add_index "campaign_translations", ["locale"], :name => "index_campaign_translations_on_locale"
create_table "campaigns", :force => true do |t|
t.integer "goal_cents"
t.integer "progress_cents"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "progress", :null => false
t.integer "goal", :null => false
t.boolean "active", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "campaigns_old", :force => true do |t|
@ -115,6 +116,7 @@ ActiveRecord::Schema.define(:version => 20140910181819) do
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "donor_email"
t.integer "campaign_id", :null => false
end
create_table "donations_old", :force => true do |t|

View file

@ -0,0 +1,5 @@
require 'spec_helper'
describe Campaign do
pending "add some examples to (or delete) #{__FILE__}"
end