Files
nairobi/app/controllers/officing/ballot_sheets_controller.rb
Javi Martín 11832cc07d 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
```
2022-04-07 19:35:40 +02:00

67 lines
1.6 KiB
Ruby

class Officing::BallotSheetsController < Officing::BaseController
before_action :verify_booth
before_action :load_poll
before_action :load_officer_assignments, only: [:new, :create]
def index
load_ballot_sheets
end
def show
load_ballot_sheet
end
def new
end
def create
load_officer_assignment
@ballot_sheet = Poll::BallotSheet.new(ballot_sheet_params)
if @ballot_sheet.save
redirect_to officing_poll_ballot_sheet_path(@poll, @ballot_sheet)
else
flash.now[:alert] = @ballot_sheet.errors.full_messages.join(", ")
render :new
end
end
private
def load_poll
@poll = Poll.find(params[:poll_id])
end
def load_ballot_sheets
@ballot_sheets = Poll::BallotSheet.where(poll: @poll)
end
def load_ballot_sheet
@ballot_sheet = Poll::BallotSheet.find(params[:id])
end
def load_officer_assignments
@officer_assignments = ::Poll::OfficerAssignment.
includes(booth_assignment: [:booth]).
joins(:booth_assignment).
final.
where(id: current_user.poll_officer.officer_assignment_ids).
where(poll_booth_assignments: { poll_id: @poll.id }).
where(date: Date.current)
end
def load_officer_assignment
@officer_assignment = current_user.poll_officer.officer_assignments.final
.find_by(id: ballot_sheet_params[:officer_assignment_id])
end
def ballot_sheet_params
params.permit(allowed_params)
end
def allowed_params
[:data, :poll_id, :officer_assignment_id]
end
end