edit featured outfits on donation page

This commit is contained in:
Emi Matchu 2014-09-09 23:16:02 -05:00
parent 59d5e99312
commit 90b45dcecd
10 changed files with 129 additions and 12 deletions

View 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());
});
})();

View file

@ -3,3 +3,24 @@ body.donations-show
border: 3px solid $module-border-color border: 3px solid $module-border-color
display: block display: block
margin: 0 auto 1em 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

View file

@ -6,15 +6,32 @@ class DonationsController < ApplicationController
def show def show
@donation = Donation.from_param(params[:id]) @donation = Donation.from_param(params[:id])
@features = @donation.features
@outfits = current_user.outfits.wardrobe_order if user_signed_in?
end end
def update def update
@donation = Donation.from_param(params[:id]) @donation = Donation.from_param(params[:id])
@donation.update_attributes params[:donation] @donation.update_attributes params[:donation]
@donation.save!
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! ' + flash[:success] = 'Donation details saved! ' +
'Also, have we thanked you yet today? Thank you!' 'Also, have we thanked you yet today? Thank you!'
redirect_to @donation redirect_to @donation
end end
end end
end

View file

@ -12,11 +12,14 @@ module DonationsHelper
'http://images.neopets.com/new_greetings/71.gif', 'http://images.neopets.com/new_greetings/71.gif',
'http://images.neopets.com/new_greetings/72.gif', 'http://images.neopets.com/new_greetings/72.gif',
'http://images.neopets.com/new_greetings/103.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' 'http://images.neopets.com/new_greetings/420.gif'
] ]
def thank_you_greeting_url def thank_you_greeting_url
THANK_YOU_GREETINGS.sample THANK_YOU_GREETINGS.sample
end end
def feature_outfit_url(outfit_id)
outfit_url(outfit_id) if outfit_id
end
end end

View file

@ -1,7 +1,10 @@
class Donation < ActiveRecord::Base class Donation < ActiveRecord::Base
FEATURE_COST = 500 # in cents = $5.00
attr_accessible :donor_name attr_accessible :donor_name
belongs_to :user belongs_to :user
has_many :features, class_name: 'DonationFeature'
def to_param def to_param
"#{id}-#{secret}" "#{id}-#{secret}"
@ -27,7 +30,17 @@ class Donation < ActiveRecord::Base
donation.user_id = user.try(:id) donation.user_id = user.try(:id)
donation.donor_name = user.try(:name) donation.donor_name = user.try(:name)
donation.secret = new_secret donation.secret = new_secret
num_features = amount / FEATURE_COST
features = []
num_features.times do
features << donation.features.new
end
Donation.transaction do
donation.save! donation.save!
features.each(&:save!)
end
donation donation
end end

View 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

View file

@ -1,9 +1,13 @@
- title "Thanks for donating!" - 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 %p
%strong Thanks so much! %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, Your support keeps Dress to Impress online and running smoothly,
and we really, truly couldn't do this without you. and we really, truly couldn't do this without you.
Do you feel the love? Because we do <3 Do you feel the love? Because we do <3
@ -14,7 +18,28 @@
(If you'd rather take care of this later, no worries! (If you'd rather take care of this later, no worries!
Check your email for a copy of this URL.) Check your email for a copy of this URL.)
= form_for @donation do |f| = form_for @donation, html: {id: 'edit-donation'} do |f|
= f.label :donor_name, "What's your name?" %ul
%li.name
= f.label :donor_name, "Your name on the donors page"
= f.text_field :donor_name, placeholder: 'Anonymous' = f.text_field :donor_name, placeholder: 'Anonymous'
= f.submit
- @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'

View 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

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # 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| create_table "auth_servers", :force => true do |t|
t.string "short_name", :limit => 10, :null => false 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", ["contributed_id", "contributed_type"], :name => "index_contributions_on_contributed_id_and_contributed_type"
add_index "contributions", ["user_id"], :name => "index_contributions_on_user_id" 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| create_table "donations", :force => true do |t|
t.integer "amount", :null => false t.integer "amount", :null => false
t.string "charge_id", :null => false t.string "charge_id", :null => false

View file

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