Make it easier to customize allowed parameters
When customizing CONSUL, one of the most common actions is adding a new
field to a form.
This requires modifying the permitted/allowed parameters. However, in
most cases, the method returning these parameters returned an instance
of `ActionController::Parameters`, so adding more parameters to it
wasn't easy.
So customizing the code required copying the method returning those
parameters and adding the new ones. For example:
```
def something_params
params.require(:something).permit(
:one_consul_attribute,
:another_consul_attribute,
:my_custom_attribute
)
end
```
This meant that, if the `something_params` method changed in CONSUL, the
customization of this method had to be updated as well.
So we're extracting the logic returning the parameters to a method which
returns an array. Now this code can be customized without copying the
original method:
```
alias_method :consul_allowed_params, :allowed_params
def allowed_params
consul_allowed_params + [:my_custom_attribute]
end
```
This commit is contained in:
@@ -29,6 +29,10 @@ class Admin::Poll::ActivePollsController < Admin::Poll::BaseController
|
||||
end
|
||||
|
||||
def active_poll_params
|
||||
params.require(:active_poll).permit(translation_params(ActivePoll))
|
||||
params.require(:active_poll).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
[translation_params(ActivePoll)]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -39,6 +39,10 @@ class Admin::Poll::BoothsController < Admin::Poll::BaseController
|
||||
private
|
||||
|
||||
def booth_params
|
||||
params.require(:poll_booth).permit(:name, :location)
|
||||
params.require(:poll_booth).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
[:name, :location]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -76,10 +76,14 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController
|
||||
end
|
||||
|
||||
def poll_params
|
||||
params.require(:poll).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
attributes = [:name, :starts_at, :ends_at, :geozone_restricted, :budget_id, :related_sdg_list,
|
||||
geozone_ids: [], image_attributes: image_attributes]
|
||||
|
||||
params.require(:poll).permit(*attributes, *report_attributes, translation_params(Poll))
|
||||
[*attributes, *report_attributes, translation_params(Poll)]
|
||||
end
|
||||
|
||||
def search_params
|
||||
|
||||
@@ -32,8 +32,11 @@ class Admin::Poll::Questions::Answers::ImagesController < Admin::Poll::BaseContr
|
||||
private
|
||||
|
||||
def images_params
|
||||
params.require(:poll_question_answer).permit(:answer_id,
|
||||
images_attributes: image_attributes)
|
||||
params.require(:poll_question_answer).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
[:answer_id, images_attributes: image_attributes]
|
||||
end
|
||||
|
||||
def load_answer
|
||||
|
||||
@@ -44,7 +44,11 @@ class Admin::Poll::Questions::Answers::VideosController < Admin::Poll::BaseContr
|
||||
private
|
||||
|
||||
def video_params
|
||||
params.require(:poll_question_answer_video).permit(:title, :url, :answer_id)
|
||||
params.require(:poll_question_answer_video).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
[:title, :url, :answer_id]
|
||||
end
|
||||
|
||||
def load_answer
|
||||
|
||||
@@ -51,12 +51,14 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
|
||||
private
|
||||
|
||||
def answer_params
|
||||
attributes = [:title, :description, :given_order, :question_id,
|
||||
documents_attributes: document_attributes]
|
||||
params.require(:poll_question_answer).permit(allowed_params)
|
||||
end
|
||||
|
||||
params.require(:poll_question_answer).permit(
|
||||
*attributes, translation_params(Poll::Question::Answer)
|
||||
)
|
||||
def allowed_params
|
||||
attributes = [:title, :description, :given_order, :question_id,
|
||||
documents_attributes: document_attributes]
|
||||
|
||||
[*attributes, translation_params(Poll::Question::Answer)]
|
||||
end
|
||||
|
||||
def load_answer
|
||||
|
||||
@@ -54,8 +54,13 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController
|
||||
private
|
||||
|
||||
def question_params
|
||||
params.require(:poll_question).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
attributes = [:poll_id, :question, :proposal_id]
|
||||
params.require(:poll_question).permit(*attributes, translation_params(Poll::Question))
|
||||
|
||||
[*attributes, translation_params(Poll::Question)]
|
||||
end
|
||||
|
||||
def search_params
|
||||
|
||||
@@ -56,9 +56,13 @@ class Admin::Poll::ShiftsController < Admin::Poll::BaseController
|
||||
end
|
||||
|
||||
def shift_params
|
||||
date_attributes = [:vote_collection_date, :recount_scrutiny_date]
|
||||
attributes = [:booth_id, :officer_id, :task, date: date_attributes]
|
||||
shift_params = params.require(:shift).permit(*attributes)
|
||||
shift_params = params.require(:shift).permit(allowed_params)
|
||||
shift_params.merge(date: shift_params[:date]["#{shift_params[:task]}_date".to_sym])
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
date_attributes = [:vote_collection_date, :recount_scrutiny_date]
|
||||
|
||||
[:booth_id, :officer_id, :task, date: date_attributes]
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user