module Admin
  class CampaignsController < Admin::BaseController
    load_and_authorize_resource :conference, find_by: :short_title
    load_and_authorize_resource :campaign, through: :conference

    def index
      authorize! :index, Campaign.new(conference_id: @conference.id)
      @campaigns = @conference.campaigns
    end

    def create
      @campaign.attributes = params[:campaign]

      if @conference.save
        redirect_to(admin_conference_campaigns_path(conference_id: @conference.short_title),
                    notice: 'Campaign successfully created.')
      else
	flash[:error] = 'Campaign creation failed. ' + @campaign.errors.full_messages.to_sentence
	render action: 'new'
      end
    end

    def new; end

    def edit; end

    def update
      if @campaign.update_attributes(params[:campaign])
        redirect_to(admin_conference_campaigns_path(
                        conference_id: @conference.short_title),
                    notice: "Campaign '#{@campaign.name}' successfully updated.")
      else
	flash[:error] = 'Campaign update failed. ' + @campaign.errors.full_messages.to_sentence
	render action: 'edit'
      end
    end

    def destroy
      if @campaign.destroy
        redirect_to(admin_conference_campaigns_path(conference_id: @conference.short_title),
                    notice: "Campaign '#{@campaign.name}' successfully deleted.")
      else
        redirect_to(admin_conference_campaigns_path(conference_id: @conference.short_title),
                    alert: "Delete of Campaign for #{@conference.short_title} failed." \
                    "#{@campaign.errors.full_messages.join('. ')}.")
      end
    end
  end
end
