forked from OpenNeo/impress
edit featured outfits on donation page
This commit is contained in:
parent
59d5e99312
commit
90b45dcecd
10 changed files with 129 additions and 12 deletions
6
app/assets/javascripts/donations/show.js
Normal file
6
app/assets/javascripts/donations/show.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
(function() {
|
||||
$('span.choose-outfit select').change(function(e) {
|
||||
var select = $(this);
|
||||
select.closest('li').find('input[type=text]').val(select.val());
|
||||
});
|
||||
})();
|
|
@ -3,3 +3,24 @@ body.donations-show
|
|||
border: 3px solid $module-border-color
|
||||
display: block
|
||||
margin: 0 auto 1em
|
||||
|
||||
#edit-donation
|
||||
ul
|
||||
list-style: none
|
||||
|
||||
label
|
||||
+inline-block
|
||||
width: 16em
|
||||
|
||||
.name input[type=text]
|
||||
width: 10em
|
||||
|
||||
.feature input[type=text]
|
||||
width: 23em
|
||||
|
||||
.choose-outfit
|
||||
font-size: 85%
|
||||
margin-left: 1em
|
||||
|
||||
select
|
||||
width: 10em
|
||||
|
|
|
@ -6,15 +6,32 @@ class DonationsController < ApplicationController
|
|||
|
||||
def show
|
||||
@donation = Donation.from_param(params[:id])
|
||||
@features = @donation.features
|
||||
@outfits = current_user.outfits.wardrobe_order if user_signed_in?
|
||||
end
|
||||
|
||||
def update
|
||||
@donation = Donation.from_param(params[:id])
|
||||
@donation.update_attributes params[:donation]
|
||||
@donation.save!
|
||||
|
||||
flash[:success] = 'Donation details saved! ' +
|
||||
'Also, have we thanked you yet today? Thank you!'
|
||||
redirect_to @donation
|
||||
feature_params = params[:feature] || {}
|
||||
@features = @donation.features.find(feature_params.keys)
|
||||
@features.each do |feature|
|
||||
feature.outfit_url = feature_params[feature.id.to_s][:outfit_url]
|
||||
end
|
||||
|
||||
begin
|
||||
Donation.transaction do
|
||||
@donation.save!
|
||||
@features.each(&:save!)
|
||||
end
|
||||
rescue ActiveRecord::RecordInvalid
|
||||
flash[:error] = "Couldn't save donation details. Do those outfits exist?"
|
||||
redirect_to @donation
|
||||
else
|
||||
flash[:success] = 'Donation details saved! ' +
|
||||
'Also, have we thanked you yet today? Thank you!'
|
||||
redirect_to @donation
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -12,11 +12,14 @@ module DonationsHelper
|
|||
'http://images.neopets.com/new_greetings/71.gif',
|
||||
'http://images.neopets.com/new_greetings/72.gif',
|
||||
'http://images.neopets.com/new_greetings/103.gif',
|
||||
'http://images.neopets.com/new_greetings/145.gif',
|
||||
'http://images.neopets.com/new_greetings/420.gif'
|
||||
]
|
||||
|
||||
def thank_you_greeting_url
|
||||
THANK_YOU_GREETINGS.sample
|
||||
end
|
||||
|
||||
def feature_outfit_url(outfit_id)
|
||||
outfit_url(outfit_id) if outfit_id
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
class Donation < ActiveRecord::Base
|
||||
FEATURE_COST = 500 # in cents = $5.00
|
||||
|
||||
attr_accessible :donor_name
|
||||
|
||||
belongs_to :user
|
||||
has_many :features, class_name: 'DonationFeature'
|
||||
|
||||
def to_param
|
||||
"#{id}-#{secret}"
|
||||
|
@ -27,7 +30,17 @@ class Donation < ActiveRecord::Base
|
|||
donation.user_id = user.try(:id)
|
||||
donation.donor_name = user.try(:name)
|
||||
donation.secret = new_secret
|
||||
donation.save!
|
||||
|
||||
num_features = amount / FEATURE_COST
|
||||
features = []
|
||||
num_features.times do
|
||||
features << donation.features.new
|
||||
end
|
||||
|
||||
Donation.transaction do
|
||||
donation.save!
|
||||
features.each(&:save!)
|
||||
end
|
||||
|
||||
donation
|
||||
end
|
||||
|
|
10
app/models/donation_feature.rb
Normal file
10
app/models/donation_feature.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
class DonationFeature < ActiveRecord::Base
|
||||
belongs_to :donation
|
||||
belongs_to :outfit
|
||||
|
||||
validates :outfit, presence: true, if: :outfit_id?
|
||||
|
||||
def outfit_url=(outfit_url)
|
||||
self.outfit_id = outfit_url.split('/').last rescue nil
|
||||
end
|
||||
end
|
|
@ -1,9 +1,13 @@
|
|||
- title "Thanks for donating!"
|
||||
|
||||
= image_tag thank_you_greeting_url, id: 'thank-you'
|
||||
= image_tag thank_you_greeting_url, id: 'thank-you',
|
||||
alt: '"Thank You" Neogreeting'
|
||||
|
||||
%p
|
||||
%strong Thanks so much!
|
||||
That's another
|
||||
= number_to_currency(@donation.amount.to_f / 100)
|
||||
toward this year's bills—thanks!
|
||||
Your support keeps Dress to Impress online and running smoothly,
|
||||
and we really, truly couldn't do this without you.
|
||||
Do you feel the love? Because we do <3
|
||||
|
@ -14,7 +18,28 @@
|
|||
(If you'd rather take care of this later, no worries!
|
||||
Check your email for a copy of this URL.)
|
||||
|
||||
= form_for @donation do |f|
|
||||
= f.label :donor_name, "What's your name?"
|
||||
= f.text_field :donor_name, placeholder: 'Anonymous'
|
||||
= f.submit
|
||||
= form_for @donation, html: {id: 'edit-donation'} do |f|
|
||||
%ul
|
||||
%li.name
|
||||
= f.label :donor_name, "Your name on the donors page"
|
||||
= f.text_field :donor_name, placeholder: 'Anonymous'
|
||||
|
||||
- @features.each do |feature|
|
||||
%li.feature
|
||||
= label_tag "feature[#{feature.id}][outfit_url]", "Featured outfit URL"
|
||||
= text_field_tag "feature[#{feature.id}][outfit_url]",
|
||||
feature_outfit_url(feature.outfit_id),
|
||||
placeholder: outfit_url(12345678)
|
||||
- if user_signed_in?
|
||||
%span.choose-outfit
|
||||
= surround '(', ')' do
|
||||
or choose
|
||||
= select_tag "feature[#{feature.id}][user_outfits]",
|
||||
options_for_select(@outfits.map { |o| [o.name, outfit_url(o)] },
|
||||
feature_outfit_url(feature.outfit_id)),
|
||||
include_blank: true
|
||||
= f.submit 'Save donation details'
|
||||
|
||||
- content_for :javascripts do
|
||||
= include_javascript_libraries :jquery
|
||||
= javascript_include_tag 'donations/show.js'
|
||||
|
|
10
db/migrate/20140910030549_create_donation_features.rb
Normal file
10
db/migrate/20140910030549_create_donation_features.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
class CreateDonationFeatures < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :donation_features do |t|
|
||||
t.integer :donation_id, null: false
|
||||
t.integer :outfit_id
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
|
@ -11,7 +11,7 @@
|
|||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20140910014231) do
|
||||
ActiveRecord::Schema.define(:version => 20140910030549) do
|
||||
|
||||
create_table "auth_servers", :force => true do |t|
|
||||
t.string "short_name", :limit => 10, :null => false
|
||||
|
@ -99,6 +99,13 @@ ActiveRecord::Schema.define(:version => 20140910014231) do
|
|||
add_index "contributions", ["contributed_id", "contributed_type"], :name => "index_contributions_on_contributed_id_and_contributed_type"
|
||||
add_index "contributions", ["user_id"], :name => "index_contributions_on_user_id"
|
||||
|
||||
create_table "donation_features", :force => true do |t|
|
||||
t.integer "donation_id", :null => false
|
||||
t.integer "outfit_id"
|
||||
t.datetime "created_at", :null => false
|
||||
t.datetime "updated_at", :null => false
|
||||
end
|
||||
|
||||
create_table "donations", :force => true do |t|
|
||||
t.integer "amount", :null => false
|
||||
t.string "charge_id", :null => false
|
||||
|
|
5
spec/models/donation_feature_spec.rb
Normal file
5
spec/models/donation_feature_spec.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe DonationFeature do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
Loading…
Reference in a new issue