From 10458f82ebf395e99287ed0c376c1bc5017a8352 Mon Sep 17 00:00:00 2001 From: Matt LeBel Date: Fri, 20 Dec 2013 15:16:46 -0800 Subject: [PATCH 1/2] added ability to associate an image with a reward --- app/assets/javascripts/admin.js.coffee | 2 +- app/controllers/admin/campaigns_controller.rb | 4 ++++ app/models/reward.rb | 2 +- app/serializers/reward_serializer.rb | 2 +- app/views/admin/campaigns/_form.html.erb | 2 ++ app/views/theme/assets/stylesheets/campaign.css.scss | 3 +++ app/views/theme/views/campaign.html.erb | 1 + db/migrate/20131220221035_add_image_to_reward.rb | 5 +++++ db/schema.rb | 3 ++- 9 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20131220221035_add_image_to_reward.rb diff --git a/app/assets/javascripts/admin.js.coffee b/app/assets/javascripts/admin.js.coffee index 84f4091d..d2c80243 100644 --- a/app/assets/javascripts/admin.js.coffee +++ b/app/assets/javascripts/admin.js.coffee @@ -79,7 +79,7 @@ Crowdhoster.admin = $('#reward-add').on 'click', (e) -> e.preventDefault() - $('#rewards ul').append('
  • RewardNumber ClaimedDelete?
    $



    0
  • ') + $('#rewards ul').append('
  • RewardNumber ClaimedDelete?
    $




    0
  • ') $('.faq.sortable').sortable stop: (e, ui) -> diff --git a/app/controllers/admin/campaigns_controller.rb b/app/controllers/admin/campaigns_controller.rb index aae42d14..bda5c99e 100644 --- a/app/controllers/admin/campaigns_controller.rb +++ b/app/controllers/admin/campaigns_controller.rb @@ -42,6 +42,7 @@ def copy # Completely refresh the rewards old_campaign.rewards.each do |reward| @campaign.rewards.create title: reward.title, + image_url: reward.image_url, description: reward.description, delivery_date: reward.delivery_date, number: reward.number, @@ -107,6 +108,7 @@ def create params[:reward].each do |reward| unless reward['delete'] && reward['delete'] == 'delete' @campaign.rewards.create title: reward['title'], + image_url: reward['image_url'], description: reward['description'], delivery_date: reward['delivery_date'], number: reward['number'].to_i, @@ -180,6 +182,7 @@ def update if reward['id'] r = Reward.find(reward['id']) r.title = reward['title'] + r.image_url = reward['image_url'] r.description = reward['description'] r.delivery_date = reward['delivery_date'] r.number = reward['number'].to_i @@ -191,6 +194,7 @@ def update end else @campaign.rewards.create title: reward['title'], + image_url: reward['image_url'], description: reward['description'], delivery_date: reward['delivery_date'], number: reward['number'].to_i, diff --git a/app/models/reward.rb b/app/models/reward.rb index cac9fbe6..4a976db0 100644 --- a/app/models/reward.rb +++ b/app/models/reward.rb @@ -1,5 +1,5 @@ class Reward < ActiveRecord::Base - attr_accessible :title, :description, :delivery_date, :number, :price, :campaign_id, :visible_flag + attr_accessible :title, :description, :delivery_date, :number, :price, :campaign_id, :visible_flag, :image_url validates :title, :description, :delivery_date, :price, presence: true diff --git a/app/serializers/reward_serializer.rb b/app/serializers/reward_serializer.rb index fb7c6954..484a7080 100644 --- a/app/serializers/reward_serializer.rb +++ b/app/serializers/reward_serializer.rb @@ -1,3 +1,3 @@ class RewardSerializer < ActiveModel::Serializer - attributes :id, :title, :description, :price, :delivery_date, :number, :campaign_id, :number_of_payments + attributes :id, :title, :description, :price, :delivery_date, :number, :campaign_id, :number_of_payments, :image_url end \ No newline at end of file diff --git a/app/views/admin/campaigns/_form.html.erb b/app/views/admin/campaigns/_form.html.erb index cfcdae54..c2714021 100644 --- a/app/views/admin/campaigns/_form.html.erb +++ b/app/views/admin/campaigns/_form.html.erb @@ -122,6 +122,8 @@
    + +

    diff --git a/app/views/theme/assets/stylesheets/campaign.css.scss b/app/views/theme/assets/stylesheets/campaign.css.scss index bd8d1c83..681b2a28 100644 --- a/app/views/theme/assets/stylesheets/campaign.css.scss +++ b/app/views/theme/assets/stylesheets/campaign.css.scss @@ -248,6 +248,9 @@ font-weight: bold; margin-bottom: 12px; } + &.image { + margin-bottom: 12px; + } &.description { margin-bottom: 12px; } diff --git a/app/views/theme/views/campaign.html.erb b/app/views/theme/views/campaign.html.erb index 5bfc3f8c..ee25f786 100644 --- a/app/views/theme/views/campaign.html.erb +++ b/app/views/theme/views/campaign.html.erb @@ -162,6 +162,7 @@

    $<%= number_with_precision(reward.price, precision: (reward.price*100%100==0.0 ? 0 : 2)) %>

    <%= reward.title %>

    + <% if reward.image_url.present? %>

    <% end %>

    <%= reward.description %>

    Estimated Delivery: <%= reward.delivery_date %>

    diff --git a/db/migrate/20131220221035_add_image_to_reward.rb b/db/migrate/20131220221035_add_image_to_reward.rb new file mode 100644 index 00000000..1cb62b4a --- /dev/null +++ b/db/migrate/20131220221035_add_image_to_reward.rb @@ -0,0 +1,5 @@ +class AddImageToReward < ActiveRecord::Migration + def change + add_column :rewards, :image_url, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 60d20fd7..8fee4477 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 => 20131206225359) do +ActiveRecord::Schema.define(:version => 20131220221035) do create_table "campaigns", :force => true do |t| t.string "name" @@ -137,6 +137,7 @@ t.datetime "created_at", :null => false t.datetime "updated_at", :null => false t.boolean "visible_flag", :default => true, :null => false + t.string "image_url" end create_table "settings", :force => true do |t| From 4f7ecbf51cf11d5c228204eba158c9ccee36f724 Mon Sep 17 00:00:00 2001 From: Matt LeBel Date: Mon, 23 Dec 2013 16:08:12 -0800 Subject: [PATCH 2/2] add client and server validation for reward creation --- app/assets/javascripts/validate.js | 25 +++++++++++++++++++++++++ app/assets/stylesheets/admin.css.scss | 5 +++++ app/models/reward.rb | 1 + 3 files changed, 31 insertions(+) diff --git a/app/assets/javascripts/validate.js b/app/assets/javascripts/validate.js index 54b250cb..99ea8c88 100644 --- a/app/assets/javascripts/validate.js +++ b/app/assets/javascripts/validate.js @@ -83,6 +83,12 @@ $( document ).ready(function() { "campaign[fixed_payment_amount]": { required: true, number: true, min: 1 }, "campaign[additional_info_label]": { required: true }, "campaign[reward_reference]": { required: true }, + "reward[][price]": { required: true, number: true }, + "reward[][title]": { required: true }, + "reward[][image_url]": { url: true }, + "reward[][description]": { required: true }, + "reward[][delivery_date]": { required: true }, + "reward[][number]": { number: true }, "campaign[contributor_reference]": { required: true }, "campaign[video_embed_id]": { minlength: 11 , maxlength: 11}, "campaign[primary_call_to_action_button]": { required: true }, @@ -125,6 +131,25 @@ $( document ).ready(function() { "campaign[reward_reference]": { required: "You must choose a word" }, + "reward[][price]": { + required: "You must enter a price", + number: "The price must be a number" + }, + "reward[][title]": { + required: "You must enter a title" + }, + "reward[][image_url]": { + url: "This must be a valid image URL" + }, + "reward[][description]": { + required: "You must enter a description" + }, + "reward[][delivery_date]": { + required: "You must choose a delivery date" + }, + "reward[][number]": { + number: "This must be a number" + }, "campaign[contributor_reference]": { required: "You must choose a word" }, diff --git a/app/assets/stylesheets/admin.css.scss b/app/assets/stylesheets/admin.css.scss index 5f0c19dd..4e337970 100644 --- a/app/assets/stylesheets/admin.css.scss +++ b/app/assets/stylesheets/admin.css.scss @@ -101,6 +101,11 @@ color: black; font-size: 14px; margin-bottom: 0px; + &.error { + color: #b94a48; + margin-bottom: 10px; + font-size: 14px; + } } table { margin-bottom: 0px; diff --git a/app/models/reward.rb b/app/models/reward.rb index 4a976db0..f985d860 100644 --- a/app/models/reward.rb +++ b/app/models/reward.rb @@ -2,6 +2,7 @@ class Reward < ActiveRecord::Base attr_accessible :title, :description, :delivery_date, :number, :price, :campaign_id, :visible_flag, :image_url validates :title, :description, :delivery_date, :price, presence: true + validates :image_url, :format => URI::regexp(%w(http https)) belongs_to :campaign has_many :payments