Files
nairobi/app/controllers/admin/legislation/processes_controller.rb
Javi Martín 7ca55c44e0 Apply Rails/SaveBang rubocop rule
Having exceptions is better than having silent bugs.

There are a few methods I've kept the same way they were.

The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.

We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").

Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.

There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.

For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
2019-10-23 14:39:31 +02:00

103 lines
2.9 KiB
Ruby

class Admin::Legislation::ProcessesController < Admin::Legislation::BaseController
include Translatable
include ImageAttributes
include DownloadSettingsHelper
has_filters %w[active all], only: :index
load_and_authorize_resource :process, class: "Legislation::Process"
def index
@processes = ::Legislation::Process.send(@current_filter).order(start_date: :desc)
.page(params[:page])
respond_to do |format|
format.html
format.csv do
send_data to_csv(process_for_download, Legislation::Process),
type: "text/csv",
disposition: "attachment",
filename: "legislation_processes.csv"
end
end
end
def create
if @process.save
link = legislation_process_path(@process)
notice = t("admin.legislation.processes.create.notice", link: link)
redirect_to edit_admin_legislation_process_path(@process), notice: notice
else
flash.now[:error] = t("admin.legislation.processes.create.error")
render :new
end
end
def update
if @process.update(process_params)
set_tag_list
link = legislation_process_path(@process)
redirect_back(fallback_location: (request.referer || root_path),
notice: t("admin.legislation.processes.update.notice", link: link))
else
flash.now[:error] = t("admin.legislation.processes.update.error")
render :edit
end
end
def destroy
@process.destroy!
notice = t("admin.legislation.processes.destroy.notice")
redirect_to admin_legislation_processes_path, notice: notice
end
private
def process_for_download
::Legislation::Process.send(@current_filter).order(start_date: :desc)
end
def process_params
params.require(:legislation_process).permit(allowed_params)
end
def allowed_params
[
:start_date,
:end_date,
:debate_start_date,
:debate_end_date,
:draft_start_date,
:draft_end_date,
:draft_publication_date,
:allegations_start_date,
:allegations_end_date,
:proposals_phase_start_date,
:proposals_phase_end_date,
:result_publication_date,
:debate_phase_enabled,
:draft_phase_enabled,
:allegations_phase_enabled,
:proposals_phase_enabled,
:draft_publication_enabled,
:result_publication_enabled,
:published,
:custom_list,
:background_color,
:font_color,
translation_params(::Legislation::Process),
documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy],
image_attributes: image_attributes
]
end
def set_tag_list
@process.set_tag_list_on(:customs, process_params[:custom_list])
@process.save!
end
def resource
@process || ::Legislation::Process.find(params[:id])
end
end