osem-fcy/app/controllers/admin/sponsors_controller.rb

137 lines
4.6 KiB
Ruby
Raw Normal View History

module Admin
class SponsorsController < Admin::BaseController
2014-08-12 11:51:59 +03:00
load_and_authorize_resource :conference, find_by: :short_title
load_and_authorize_resource :sponsor, through: :conference
before_action :sponsorship_level_required, only: [:index, :new]
2014-08-12 11:51:59 +03:00
def index
authorize! :index, Sponsor.new(conference_id: @conference.id)
end
2017-08-25 00:43:28 +03:00
def show
2017-08-29 16:20:21 +03:00
@sponsor.swag.reject! { |_key, value| value[:type].blank? || value[:quantity].blank? }
@sponsor.swag_transportation.reject! { |_key, value| value[:carrier_name].blank? || value[:tracking_number].blank? || value[:boxes].blank? }
2017-08-27 23:40:32 +03:00
@sponsor.swag_index = @sponsor.swag.length
2017-08-28 15:24:51 +03:00
@sponsor.carrier_index = @sponsor.swag_transportation.length
2017-08-25 00:43:28 +03:00
end
2017-08-18 16:07:19 +03:00
2017-08-25 00:43:28 +03:00
def edit
2017-08-29 16:20:21 +03:00
@sponsor.swag.reject! { |_key, value| value[:type].blank? || value[:quantity].blank? }
@sponsor.swag_transportation.reject! { |_key, value| value[:carrier_name].blank? || value[:tracking_number].blank? }
2017-08-27 23:40:32 +03:00
@sponsor.swag_index = @sponsor.swag.length
2017-08-28 15:24:51 +03:00
@sponsor.carrier_index = @sponsor.swag_transportation.length
2017-08-25 00:43:28 +03:00
end
def new
@sponsor = @conference.sponsors.new
end
def create
@sponsor = @conference.sponsors.new(sponsor_params)
if @sponsor.save
redirect_to admin_conference_sponsors_path(conference_id: @conference.short_title),
notice: 'Sponsor successfully created.'
else
flash.now[:error] = "Creating sponsor failed: #{@sponsor.errors.full_messages.join('. ')}."
render :new
end
end
def update
if @sponsor.update_attributes(sponsor_params)
2017-08-18 16:07:19 +03:00
redirect_to admin_conference_sponsor_path(
conference_id: @conference.short_title, id: @sponsor.id),
notice: 'Sponsor successfully updated.'
else
flash.now[:error] = "Update sponsor failed: #{@sponsor.errors.full_messages.join('. ')}."
render :edit
end
end
def destroy
if @sponsor.destroy
redirect_to admin_conference_sponsors_path(conference_id: @conference.short_title),
notice: 'Sponsor successfully deleted.'
else
redirect_to admin_conference_sponsors_path(conference_id: @conference.short_title),
error: 'Deleting sponsor failed! ' \
"#{@sponsor.errors.full_messages.join('. ')}."
end
end
2017-08-27 15:42:52 +03:00
def confirm
@sponsor.confirm!
2017-08-25 00:43:28 +03:00
2017-08-27 15:42:52 +03:00
if @sponsor.save
redirect_to admin_conference_sponsors_path(@conference.short_title),
2017-08-27 23:40:32 +03:00
notice: 'Sponsor successfully confirmed!'
2017-08-27 15:42:52 +03:00
else
flash[:error] = 'Sponsor couldn\' t be confirmed.'
end
end
def cancel
@sponsor.cancel!
2017-08-18 16:07:19 +03:00
2017-08-27 15:42:52 +03:00
if @sponsor.save
redirect_to admin_conference_sponsors_path(@conference.short_title),
2017-08-27 23:40:32 +03:00
notice: 'Sponsor successfully canceled'
2017-08-27 15:42:52 +03:00
else
flash[:error] = 'Sponsor couldn\'t be canceled'
end
end
2017-08-23 12:26:46 +03:00
2017-08-29 12:04:31 +03:00
def contact
@sponsor.contact!
if @sponsor.save
redirect_to admin_conference_sponsors_path(@conference.short_title),
notice: 'Sponsor\'s state successfully updated'
else
flash[:error] = 'Sponsor\'s state couldn\'t be updated'
end
end
2017-08-23 12:26:46 +03:00
def paid
@sponsor.paid = !@sponsor.paid
if @sponsor.save
2017-08-23 23:08:58 +03:00
flash[:notice] = 'Sponsor successfully updated.'
2017-08-23 12:26:46 +03:00
else
2017-08-23 23:08:58 +03:00
flash[:error] = 'Sponsor failed to be updated.'
2017-08-23 12:26:46 +03:00
end
end
def has_swag
@sponsor.has_swag = !@sponsor.has_swag
if @sponsor.save
2017-08-23 23:08:58 +03:00
flash[:notice] = 'Sponsor successfully updated.'
2017-08-23 12:26:46 +03:00
else
2017-08-23 23:08:58 +03:00
flash[:error] = 'Sponsor failed to be updated.'
2017-08-23 12:26:46 +03:00
end
end
def swag_received
@sponsor.swag_received = !@sponsor.swag_received
if @sponsor.save
2017-08-23 23:08:58 +03:00
flash[:notice] = 'Sponsor successfully updated.'
2017-08-23 12:26:46 +03:00
else
2017-08-23 23:08:58 +03:00
flash[:error] = 'Sponsor failed to be updated.'
2017-08-23 12:26:46 +03:00
end
end
private
def sponsor_params
2017-08-18 16:07:19 +03:00
params.require(:sponsor).permit(:name, :description, :website_url, :picture, :picture_cache, :sponsorship_level_id, :conference_id,
2017-08-28 15:24:51 +03:00
:paid, :has_swag, :swag_received, :address, :vat, :has_banner, :swag_index, :carrier_index, :amount,
swag: [:type, :quantity], swag_transportation: [:carrier_name, :tracking_number, :boxes])
end
def sponsorship_level_required
return unless @conference.sponsorship_levels.empty?
redirect_to admin_conference_sponsorship_levels_path(conference_id: @conference.short_title),
alert: 'You need to create atleast one sponsorship level to add a sponsor'
end
end
end