From d888be02422ab52f848e4defc9692e331cfd1132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasia=20Jarmo=C5=82kowicz?= Date: Fri, 18 May 2018 17:18:46 +0200 Subject: [PATCH] Campaign template create, delete --- .../campaign_templates_controller.rb | 12 ++- app/models/campaign_template.rb | 77 ++++++++++++++++++- .../campaign_templates/index.html.erb | 8 +- client/js/campaign_templates/new/wizard.js | 26 ++++++- config/routes.rb | 2 +- ..._add_nonprofit_id_to_campaign_templates.rb | 7 ++ db/structure.sql | 6 +- 7 files changed, 127 insertions(+), 11 deletions(-) create mode 100644 db/migrate/201810202124320_add_nonprofit_id_to_campaign_templates.rb diff --git a/app/controllers/nonprofits/campaign_templates_controller.rb b/app/controllers/nonprofits/campaign_templates_controller.rb index 72d4ba64..8188900e 100644 --- a/app/controllers/nonprofits/campaign_templates_controller.rb +++ b/app/controllers/nonprofits/campaign_templates_controller.rb @@ -7,12 +7,20 @@ module Nonprofits def index @templates = CampaignTemplate.all + @nonprofit = current_nonprofit end def create - puts params + template = CampaignTemplate.create(params[:campaign_template]) - render :status_ok + json_saved template + end + + def destroy + campaign = CampaignTemplate.find(params[:id]) + campaign.destroy + + render json: {}, status: :no_content end end end diff --git a/app/models/campaign_template.rb b/app/models/campaign_template.rb index fcfe7d63..099dea01 100644 --- a/app/models/campaign_template.rb +++ b/app/models/campaign_template.rb @@ -1,24 +1,97 @@ class CampaignTemplate < ActiveRecord::Base # these are very arbitrary names – some are attrs of campaign, some are not # might be a good idea to get the default list from settings - CUSTOMIZABLE_ATTR = %i(goal_amount_dollars campaigner_photo reason_for_supporting) + CUSTOMIZABLE_ATTR = %i(goal_amount) attr_accessible \ :template_name, :name, # refers to campaign name :tagline, :goal_amount, + :goal_amount_dollars, # accessor: translated into goal_amount (cents) :main_image, :remove_main_image, # for carrierwave :video_url, :vimeo_video_id, :youtube_video_id, :summary, - :body + :body, + :end_datetime, + :goal_customizable + + attr_accessor :goal_amount_dollars + attr_accessor :goal_customizable + attr_accessor :end_datetime + attr_accessor :hide_activity_feed + attr_accessor :deleted + attr_accessor :hide_title + attr_accessor :slug + attr_accessor :custom_banner_url + attr_accessor :published + attr_accessor :show_total_raised + attr_accessor :show_total_count + attr_accessor :hide_goal + attr_accessor :hide_thermometer + attr_accessor :hide_custom_amounts + attr_accessor :receipt_message has_many :campaigns + belongs_to :nonprofit def customizable_attribute?(attribute_name) CUSTOMIZABLE_ATTR.include? attribute_name.to_sym end + + def recurring_fund? + end + + def main_image_url(url) + end + + def slug + Format::Url.convert_to_slug(template_name) + end + + def customizable_attributes_list + CUSTOMIZABLE_ATTR + end + + def name + if self[:name] + self[:name] + else + 'no name' + end + end + + def url + "#{self.nonprofit.url}/campaigns/#{self.slug}" + end + + def days_left + return 0 if self.end_datetime.nil? + (self.end_datetime.to_date - Date.today).to_i + end + + before_validation do + if self.goal_amount_dollars.present? + self.goal_amount = (self.goal_amount_dollars.gsub(',','').to_f * 100).to_i + end + self + end + + after_create do + # user = self.profile.user + # Role.create(name: :campaign_editor, user_id: user.id, host: self) + end + + before_validation(on: :create) do + self.set_defaults + self + end + + def set_defaults + # self.total_supporters = 1 + # self.published = false if self.published.nil? + end end diff --git a/app/views/nonprofits/campaign_templates/index.html.erb b/app/views/nonprofits/campaign_templates/index.html.erb index 1b420ae8..f1890623 100644 --- a/app/views/nonprofits/campaign_templates/index.html.erb +++ b/app/views/nonprofits/campaign_templates/index.html.erb @@ -6,8 +6,9 @@ <%= content_for :javascripts do %> - <%= IncludeAsset.js '/client/js/campaigns/index/page.js' %> + <%= IncludeAsset.js '/client/js/campaign_templates/index/page.js' %> <% end %> @@ -33,7 +34,7 @@ <% @templates.each do |template|%> - + #
@@ -50,6 +51,9 @@

Customizable attributes: <%= template.customizable_attributes_list %>

+ + Delete template + <% end %> diff --git a/client/js/campaign_templates/new/wizard.js b/client/js/campaign_templates/new/wizard.js index 172a4a6a..c1a79bb8 100644 --- a/client/js/campaign_templates/new/wizard.js +++ b/client/js/campaign_templates/new/wizard.js @@ -1,7 +1,6 @@ require('../../common/pikaday-timepicker') require('../../components/wizard') require('../../common/image_uploader') -var checkName = require('../../common/ajax/check_campaign_or_event_name') var format_err = require('../../common/format_response_error') appl.def('advance_campaign_template_name_step', function(form_obj) { @@ -17,8 +16,9 @@ appl.def('create_campaign_template', function(el) { post_campaign_template(form_data) .then(function(req) { - appl.notify("Redirecting to your campaign template...") - appl.redirect(JSON.parse(req.response).url) + appl.notify("Campaign template created!") + var template_id = JSON.parse(req.response).id + appl.redirect('/nonprofits/' + app.nonprofit_id + '/campaign_templates') }) .catch(function(req) { appl.def('new_campaign_template_wiz.loading', false) @@ -48,3 +48,23 @@ function post_campaign_template(form_data) { } }) } + +appl.def('delete_template', function(id) { + appl.def('loading', true) + var url = '/nonprofits/' + app.nonprofit_id + '/campaign_templates/' + id + + return new Promise(function(resolve, reject) { + var req = new XMLHttpRequest() + req.open("DELETE", url) + req.setRequestHeader('X-CSRF-Token', window._csrf) + req.send({ campaign_template: {id: id} }) + req.onload = function(ev) { + if(req.status === 204) resolve(req) + else reject(req) + } + }).then(function() { + appl.def('loading', false) + appl.notify('Successfully deleted template.') + appl.redirect('/nonprofits/' + app.nonprofit_id + '/campaign_templates') + }) +}) diff --git a/config/routes.rb b/config/routes.rb index 6fbd3c1e..df8e90a1 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -110,7 +110,7 @@ Commitchange::Application.routes.draw do post(:send_code) end - resources(:campaign_templates, {only: [:index, :create]}) + resources(:campaign_templates, {only: [:index, :create, :show, :destroy]}) post 'tracking', controller: 'trackings', action: 'create' end diff --git a/db/migrate/201810202124320_add_nonprofit_id_to_campaign_templates.rb b/db/migrate/201810202124320_add_nonprofit_id_to_campaign_templates.rb new file mode 100644 index 00000000..59693ac6 --- /dev/null +++ b/db/migrate/201810202124320_add_nonprofit_id_to_campaign_templates.rb @@ -0,0 +1,7 @@ +class AddNonprofitIdToCampaignTemplates < ActiveRecord::Migration + def change + change_table :campaign_templates do |t| + t.references :nonprofit + end + end +end diff --git a/db/structure.sql b/db/structure.sql index e63f858a..7df2d0c1 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -377,7 +377,8 @@ CREATE TABLE public.campaign_templates ( summary text, body text, created_at timestamp without time zone NOT NULL, - updated_at timestamp without time zone NOT NULL + updated_at timestamp without time zone NOT NULL, + nonprofit_id integer ); @@ -4418,3 +4419,6 @@ INSERT INTO schema_migrations (version) VALUES ('201810202124317'); INSERT INTO schema_migrations (version) VALUES ('201810202124318'); INSERT INTO schema_migrations (version) VALUES ('201810202124319'); + +INSERT INTO schema_migrations (version) VALUES ('201810202124320'); +