Campaign template create, delete

This commit is contained in:
Kasia Jarmołkowicz 2018-05-18 17:18:46 +02:00 committed by Eric Schultz
parent abd5fbe63e
commit d888be0242
7 changed files with 127 additions and 11 deletions

View file

@ -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

View file

@ -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

View file

@ -6,8 +6,9 @@
<%= content_for :javascripts do %>
<script>
app.current_nonprofit_user = "<%= current_nonprofit_user? %>"
app.nonprofit_id = <%= @nonprofit.id %>
</script>
<%= 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|%>
<tr>
<td class='u-padding--0 u-width--200 u-hideIf--400'>
<img src='<%= template.main_image_url(:normal) %>'>
#<img src='<%= %>'>
</td>
<td class='u-padding--10'>
<h6 class='u-marginTop--0 u-marginBottom--5'>
@ -50,6 +51,9 @@
<p class='u-marginBottom--15'>
<small>Customizable attributes: <%= template.customizable_attributes_list %></small>
</p>
<a class="button red">Delete template</a>
<!--= on 'click' (delete_template <%= template.id %>) -->
</td>
</tr>
<% end %>

View file

@ -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')
})
})

View file

@ -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

View file

@ -0,0 +1,7 @@
class AddNonprofitIdToCampaignTemplates < ActiveRecord::Migration
def change
change_table :campaign_templates do |t|
t.references :nonprofit
end
end
end

View file

@ -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');