From 90b45dcecd62ac96417d61b0e96fb9af1bc1c56b Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 9 Sep 2014 23:16:02 -0500 Subject: [PATCH] edit featured outfits on donation page --- app/assets/javascripts/donations/show.js | 6 ++++ app/assets/stylesheets/donations/_show.sass | 21 +++++++++++ app/controllers/donations_controller.rb | 25 ++++++++++--- app/helpers/donations_helper.rb | 5 ++- app/models/donation.rb | 15 +++++++- app/models/donation_feature.rb | 10 ++++++ app/views/donations/show.html.haml | 35 ++++++++++++++++--- ...20140910030549_create_donation_features.rb | 10 ++++++ db/schema.rb | 9 ++++- spec/models/donation_feature_spec.rb | 5 +++ 10 files changed, 129 insertions(+), 12 deletions(-) create mode 100644 app/assets/javascripts/donations/show.js create mode 100644 app/models/donation_feature.rb create mode 100644 db/migrate/20140910030549_create_donation_features.rb create mode 100644 spec/models/donation_feature_spec.rb diff --git a/app/assets/javascripts/donations/show.js b/app/assets/javascripts/donations/show.js new file mode 100644 index 00000000..67371c68 --- /dev/null +++ b/app/assets/javascripts/donations/show.js @@ -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()); + }); +})(); diff --git a/app/assets/stylesheets/donations/_show.sass b/app/assets/stylesheets/donations/_show.sass index 1e44b0e7..b1139bdf 100644 --- a/app/assets/stylesheets/donations/_show.sass +++ b/app/assets/stylesheets/donations/_show.sass @@ -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 diff --git a/app/controllers/donations_controller.rb b/app/controllers/donations_controller.rb index 3f7b1b71..dd03e728 100644 --- a/app/controllers/donations_controller.rb +++ b/app/controllers/donations_controller.rb @@ -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 diff --git a/app/helpers/donations_helper.rb b/app/helpers/donations_helper.rb index d16de073..2121233d 100644 --- a/app/helpers/donations_helper.rb +++ b/app/helpers/donations_helper.rb @@ -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 diff --git a/app/models/donation.rb b/app/models/donation.rb index 8315e2a4..2f3b5ce5 100644 --- a/app/models/donation.rb +++ b/app/models/donation.rb @@ -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 diff --git a/app/models/donation_feature.rb b/app/models/donation_feature.rb new file mode 100644 index 00000000..b4dfee88 --- /dev/null +++ b/app/models/donation_feature.rb @@ -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 diff --git a/app/views/donations/show.html.haml b/app/views/donations/show.html.haml index a9d9594d..ab992408 100644 --- a/app/views/donations/show.html.haml +++ b/app/views/donations/show.html.haml @@ -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' diff --git a/db/migrate/20140910030549_create_donation_features.rb b/db/migrate/20140910030549_create_donation_features.rb new file mode 100644 index 00000000..3643a116 --- /dev/null +++ b/db/migrate/20140910030549_create_donation_features.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 56f26f20..b47e72cf 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 => 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 diff --git a/spec/models/donation_feature_spec.rb b/spec/models/donation_feature_spec.rb new file mode 100644 index 00000000..f7f969dc --- /dev/null +++ b/spec/models/donation_feature_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe DonationFeature do + pending "add some examples to (or delete) #{__FILE__}" +end