forked from OpenNeo/impress
Move fundraising models into the Fundraising module
This was mostly straightforward it seems, whew!
This commit is contained in:
parent
82be7fe301
commit
d39e7cea81
11 changed files with 126 additions and 120 deletions
|
@ -65,7 +65,7 @@ class ClosetHangersController < ApplicationController
|
|||
current_user.assign_closeted_to_items!(items)
|
||||
end
|
||||
|
||||
@campaign = Campaign.current
|
||||
@campaign = Fundraising::Campaign.current
|
||||
end
|
||||
|
||||
def petpage
|
||||
|
|
|
@ -18,7 +18,7 @@ class ItemsController < ApplicationController
|
|||
assign_closeted!
|
||||
respond_to do |format|
|
||||
format.html {
|
||||
@campaign = Campaign.current rescue nil
|
||||
@campaign = Fundraising::Campaign.current rescue nil
|
||||
if @items.count == 1
|
||||
redirect_to @items.first
|
||||
else
|
||||
|
@ -45,7 +45,7 @@ class ItemsController < ApplicationController
|
|||
else
|
||||
respond_to do |format|
|
||||
format.html {
|
||||
@campaign = Campaign.current rescue nil
|
||||
@campaign = Fundraising::Campaign.current rescue nil
|
||||
@newest_items = Item.newest.includes(:translations).limit(18)
|
||||
}
|
||||
format.js { render json: {error: '$q required'}}
|
||||
|
|
|
@ -76,7 +76,7 @@ class OutfitsController < ApplicationController
|
|||
|
||||
@neopets_usernames = user_signed_in? ? current_user.neopets_usernames : []
|
||||
|
||||
@campaign = Campaign.current rescue nil
|
||||
@campaign = Fundraising::Campaign.current rescue nil
|
||||
end
|
||||
|
||||
def show
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
class Campaign < ApplicationRecord
|
||||
has_many :donations
|
||||
|
||||
def progress_percent
|
||||
[(progress.to_f / goal) * 100, 100].min
|
||||
end
|
||||
|
||||
def remaining
|
||||
goal - progress
|
||||
end
|
||||
|
||||
def complete?
|
||||
progress >= goal
|
||||
end
|
||||
|
||||
def self.current
|
||||
self.where(active: true).first or
|
||||
raise ActiveRecord::RecordNotFound.new("no current campaign")
|
||||
end
|
||||
end
|
|
@ -1,69 +0,0 @@
|
|||
class Donation < ApplicationRecord
|
||||
FEATURE_COST = 500 # in cents = $5.00
|
||||
|
||||
belongs_to :campaign
|
||||
belongs_to :user, optional: true
|
||||
has_many :features, class_name: 'DonationFeature'
|
||||
|
||||
def to_param
|
||||
"#{id}-#{secret}"
|
||||
end
|
||||
|
||||
def self.create_from_charge(campaign, user, params)
|
||||
amount = (BigDecimal.new(params[:amount]) * 100).floor
|
||||
|
||||
campaign.progress += amount
|
||||
|
||||
charge_params = {
|
||||
amount: amount,
|
||||
description: 'Donation (thank you!)',
|
||||
currency: 'usd'
|
||||
}
|
||||
|
||||
if params[:stripe_token_type] == 'card'
|
||||
customer = Stripe::Customer.create(
|
||||
card: params[:stripe_token]
|
||||
)
|
||||
charge_params[:customer] = customer.id
|
||||
elsif params[:stripe_token_type] == 'bitcoin_receiver'
|
||||
charge_params[:card] = params[:stripe_token]
|
||||
else
|
||||
raise ArgumentError, "unexpected stripe token type #{params[:stripe_token_type]}"
|
||||
end
|
||||
|
||||
charge = Stripe::Charge.create(charge_params)
|
||||
|
||||
donation = campaign.donations.build
|
||||
donation.amount = amount
|
||||
donation.charge_id = charge.id
|
||||
donation.user = user
|
||||
donation.donor_name = user.try(:name)
|
||||
donation.donor_email = params[:donor_email]
|
||||
donation.secret = new_secret
|
||||
|
||||
num_features = amount / FEATURE_COST
|
||||
features = []
|
||||
num_features.times do
|
||||
features << donation.features.new
|
||||
end
|
||||
|
||||
Donation.transaction do
|
||||
campaign.save!
|
||||
donation.save!
|
||||
features.each(&:save!)
|
||||
end
|
||||
|
||||
DonationMailer.thank_you_email(donation, donation.donor_email).deliver
|
||||
|
||||
donation
|
||||
end
|
||||
|
||||
def self.new_secret
|
||||
SecureRandom.urlsafe_base64 8
|
||||
end
|
||||
|
||||
def self.from_param(param)
|
||||
id, secret = param.split('-', 2)
|
||||
self.where(secret: secret).find(id)
|
||||
end
|
||||
end
|
|
@ -1,24 +0,0 @@
|
|||
class DonationFeature < ApplicationRecord
|
||||
belongs_to :donation
|
||||
belongs_to :outfit, optional: true
|
||||
|
||||
validates :outfit, presence: true, if: :outfit_id_present?
|
||||
|
||||
delegate :donor_name, to: :donation
|
||||
|
||||
def as_json(options={})
|
||||
{donor_name: donor_name, outfit_image_url: outfit_image_url}
|
||||
end
|
||||
|
||||
def outfit_url=(outfit_url)
|
||||
self.outfit_id = outfit_url.split('/').last rescue nil
|
||||
end
|
||||
|
||||
def outfit_id_present?
|
||||
outfit_id.present?
|
||||
end
|
||||
|
||||
def outfit_image_url
|
||||
outfit && outfit.image ? outfit.image.medium.url : nil
|
||||
end
|
||||
end
|
22
app/models/fundraising/campaign.rb
Normal file
22
app/models/fundraising/campaign.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
module Fundraising
|
||||
class Campaign < ApplicationRecord
|
||||
has_many :donations
|
||||
|
||||
def progress_percent
|
||||
[(progress.to_f / goal) * 100, 100].min
|
||||
end
|
||||
|
||||
def remaining
|
||||
goal - progress
|
||||
end
|
||||
|
||||
def complete?
|
||||
progress >= goal
|
||||
end
|
||||
|
||||
def self.current
|
||||
self.where(active: true).first or
|
||||
raise ActiveRecord::RecordNotFound.new("no current campaign")
|
||||
end
|
||||
end
|
||||
end
|
71
app/models/fundraising/donation.rb
Normal file
71
app/models/fundraising/donation.rb
Normal file
|
@ -0,0 +1,71 @@
|
|||
module Fundraising
|
||||
class Donation < ApplicationRecord
|
||||
FEATURE_COST = 500 # in cents = $5.00
|
||||
|
||||
belongs_to :campaign
|
||||
belongs_to :user, optional: true
|
||||
has_many :features, class_name: 'DonationFeature'
|
||||
|
||||
def to_param
|
||||
"#{id}-#{secret}"
|
||||
end
|
||||
|
||||
def self.create_from_charge(campaign, user, params)
|
||||
amount = (BigDecimal.new(params[:amount]) * 100).floor
|
||||
|
||||
campaign.progress += amount
|
||||
|
||||
charge_params = {
|
||||
amount: amount,
|
||||
description: 'Donation (thank you!)',
|
||||
currency: 'usd'
|
||||
}
|
||||
|
||||
if params[:stripe_token_type] == 'card'
|
||||
customer = Stripe::Customer.create(
|
||||
card: params[:stripe_token]
|
||||
)
|
||||
charge_params[:customer] = customer.id
|
||||
elsif params[:stripe_token_type] == 'bitcoin_receiver'
|
||||
charge_params[:card] = params[:stripe_token]
|
||||
else
|
||||
raise ArgumentError, "unexpected stripe token type #{params[:stripe_token_type]}"
|
||||
end
|
||||
|
||||
charge = Stripe::Charge.create(charge_params)
|
||||
|
||||
donation = campaign.donations.build
|
||||
donation.amount = amount
|
||||
donation.charge_id = charge.id
|
||||
donation.user = user
|
||||
donation.donor_name = user.try(:name)
|
||||
donation.donor_email = params[:donor_email]
|
||||
donation.secret = new_secret
|
||||
|
||||
num_features = amount / FEATURE_COST
|
||||
features = []
|
||||
num_features.times do
|
||||
features << donation.features.new
|
||||
end
|
||||
|
||||
Donation.transaction do
|
||||
campaign.save!
|
||||
donation.save!
|
||||
features.each(&:save!)
|
||||
end
|
||||
|
||||
DonationMailer.thank_you_email(donation, donation.donor_email).deliver
|
||||
|
||||
donation
|
||||
end
|
||||
|
||||
def self.new_secret
|
||||
SecureRandom.urlsafe_base64 8
|
||||
end
|
||||
|
||||
def self.from_param(param)
|
||||
id, secret = param.split('-', 2)
|
||||
self.where(secret: secret).find(id)
|
||||
end
|
||||
end
|
||||
end
|
26
app/models/fundraising/donation_feature.rb
Normal file
26
app/models/fundraising/donation_feature.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
module Fundraising
|
||||
class DonationFeature < ApplicationRecord
|
||||
belongs_to :donation
|
||||
belongs_to :outfit, optional: true
|
||||
|
||||
validates :outfit, presence: true, if: :outfit_id_present?
|
||||
|
||||
delegate :donor_name, to: :donation
|
||||
|
||||
def as_json(options={})
|
||||
{donor_name: donor_name, outfit_image_url: outfit_image_url}
|
||||
end
|
||||
|
||||
def outfit_url=(outfit_url)
|
||||
self.outfit_id = outfit_url.split('/').last rescue nil
|
||||
end
|
||||
|
||||
def outfit_id_present?
|
||||
outfit_id.present?
|
||||
end
|
||||
|
||||
def outfit_image_url
|
||||
outfit && outfit.image ? outfit.image.medium.url : nil
|
||||
end
|
||||
end
|
||||
end
|
|
@ -79,15 +79,15 @@ OpenneoImpressItems::Application.routes.draw do
|
|||
end
|
||||
|
||||
# Donation campaign stuff!
|
||||
scope module: "fundraising" do
|
||||
scope module: "fundraising", as: "fundraising" do
|
||||
resources :donations, only: [:create, :show, :update] do
|
||||
collection do
|
||||
resources :donation_features, path: 'features', only: [:index]
|
||||
end
|
||||
end
|
||||
resources :campaigns, only: [:show], path: '/donate/campaigns'
|
||||
get '/donate' => 'campaigns#current', as: :donate
|
||||
end
|
||||
get '/donate' => 'fundraising/campaigns#current', as: :donate
|
||||
|
||||
# Static pages!
|
||||
get '/terms', as: :terms,
|
||||
|
|
|
@ -209,4 +209,4 @@ Zone.create(:id => 52, :label => "Foreground", :plain_label => "foreground", :de
|
|||
|
||||
# NOTE: Creating an AuthUser automatically creates a User, too.
|
||||
AuthUser.create(name: "test", password: "test123", email: "test@gmail.example")
|
||||
Campaign.create(goal: 100_00, active: true, advertised: false, description: "")
|
||||
Fundraising::Campaign.create(goal: 100_00, active: true, advertised: false, description: "")
|
||||
|
|
Loading…
Reference in a new issue