From 8e22c271a4ec8689cb41ac149f7703b1e701ad7c Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 11 Sep 2014 17:40:37 -0500 Subject: [PATCH] track campaign progress --- .../partials/_campaign-progress.sass | 5 ++- app/controllers/donations_controller.rb | 4 ++- app/controllers/static_controller.rb | 4 ++- app/helpers/application_helper.rb | 33 +++++-------------- app/models/campaign.rb | 14 ++++++++ app/models/donation.rb | 10 ++++-- app/views/static/donate.html.haml | 10 +++--- db/migrate/20140910204019_create_campaigns.rb | 11 +++++++ ...40910204043_add_campaign_id_to_donation.rb | 5 +++ db/schema.rb | 12 ++++--- spec/models/campaign_spec.rb | 5 +++ 11 files changed, 70 insertions(+), 43 deletions(-) create mode 100644 app/models/campaign.rb create mode 100644 db/migrate/20140910204019_create_campaigns.rb create mode 100644 db/migrate/20140910204043_add_campaign_id_to_donation.rb create mode 100644 spec/models/campaign_spec.rb diff --git a/app/assets/stylesheets/partials/_campaign-progress.sass b/app/assets/stylesheets/partials/_campaign-progress.sass index 8cf59f75..08c47a05 100644 --- a/app/assets/stylesheets/partials/_campaign-progress.sass +++ b/app/assets/stylesheets/partials/_campaign-progress.sass @@ -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 diff --git a/app/controllers/donations_controller.rb b/app/controllers/donations_controller.rb index dd03e728..ba5cdb36 100644 --- a/app/controllers/donations_controller.rb +++ b/app/controllers/donations_controller.rb @@ -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 diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index c15fa64b..e7c2c36c 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 89674306..7c821320 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/models/campaign.rb b/app/models/campaign.rb new file mode 100644 index 00000000..e8a0c78e --- /dev/null +++ b/app/models/campaign.rb @@ -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 diff --git a/app/models/donation.rb b/app/models/donation.rb index 5495c5a0..0a152394 100644 --- a/app/models/donation.rb +++ b/app/models/donation.rb @@ -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 diff --git a/app/views/static/donate.html.haml b/app/views/static/donate.html.haml index 72ac19a3..f050ca9b 100644 --- a/app/views/static/donate.html.haml +++ b/app/views/static/donate.html.haml @@ -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'), diff --git a/db/migrate/20140910204019_create_campaigns.rb b/db/migrate/20140910204019_create_campaigns.rb new file mode 100644 index 00000000..fe55194c --- /dev/null +++ b/db/migrate/20140910204019_create_campaigns.rb @@ -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 diff --git a/db/migrate/20140910204043_add_campaign_id_to_donation.rb b/db/migrate/20140910204043_add_campaign_id_to_donation.rb new file mode 100644 index 00000000..14f00a0c --- /dev/null +++ b/db/migrate/20140910204043_add_campaign_id_to_donation.rb @@ -0,0 +1,5 @@ +class AddCampaignIdToDonation < ActiveRecord::Migration + def change + add_column :donations, :campaign_id, :integer, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 720ef3cc..f4e32d88 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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| diff --git a/spec/models/campaign_spec.rb b/spec/models/campaign_spec.rb new file mode 100644 index 00000000..06d3d5ad --- /dev/null +++ b/spec/models/campaign_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Campaign do + pending "add some examples to (or delete) #{__FILE__}" +end