Skip to content

Commit

Permalink
Move a bit more permitted attributes logic into policies
Browse files Browse the repository at this point in the history
  • Loading branch information
tjgrathwell committed Dec 13, 2016
1 parent acad1b8 commit d91752c
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 17 deletions.
6 changes: 1 addition & 5 deletions app/controllers/devise_overrides/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ def after_inactive_sign_up_path_for(resource)
private

def user_params
params.require(:user).permit(policy(User).permitted_attributes + [:current_password, {
region_ids: [],
subscribed_organization_ids: [],
profile_attributes: policy(Profile).permitted_attributes
}])
permitted_attributes(User)
end

# check if we need password to update user data
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ def destroy
private

def location_params
attributes = policy(Location).permitted_attributes
attributes = attributes + [:contact_info, :notes] if @location && policy(@location).edit_additional_details?
params.require(:location).permit(attributes)
permitted_attributes(@location || Location.new)
end

def assign_location
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/rsvps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ def apply_other_changes_from_params

def rsvp_params
role_id = params[:rsvp][:role_id].to_i
params.require(:rsvp).permit(policy(Rsvp).permitted_attributes + [
event_session_ids: [], dietary_restriction_diets: []
]).tap do |params|
permitted_attributes(Rsvp).tap do |params|
if role_id == Role::STUDENT.id
user_choices = Array(params[:event_session_ids]).select(&:present?).map(&:to_i)
required_sessions = @event.event_sessions.where(required_for_students: true).pluck(:id)
Expand Down
6 changes: 5 additions & 1 deletion app/policies/event_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ def permitted_attributes
:email_on_approval,
:has_childcare,
:restrict_operating_systems,
:survey_greeting
:survey_greeting,
{
event_sessions_attributes: EventSessionPolicy.new(user, EventSession).permitted_attributes + [:id],
allowed_operating_system_ids: []
}
]
end
end
9 changes: 8 additions & 1 deletion app/policies/location_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def edit_additional_details?
end

def permitted_attributes
[
attributes = [
:name,
:address_1,
:address_2,
Expand All @@ -30,5 +30,12 @@ def permitted_attributes
:zip,
:region_id
]
if edit_additional_details?
attributes += [
:contact_info,
:notes
]
end
attributes
end
end
6 changes: 5 additions & 1 deletion app/policies/rsvp_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ def permitted_attributes
:class_level,
:dietary_info,
:needs_childcare,
:plus_one_host
:plus_one_host,
{
event_session_ids: [],
dietary_restriction_diets: []
}
]
end
end
8 changes: 7 additions & 1 deletion app/policies/user_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ def permitted_attributes
:remember_me,
:time_zone,
:gender,
:allow_event_email
:allow_event_email,
:current_password,
{
region_ids: [],
subscribed_organization_ids: [],
profile_attributes: ProfilePolicy.new(user, Profile).permitted_attributes
}
]
end
end
2 changes: 0 additions & 2 deletions app/services/event_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ def initialize(event:, notice: nil, render: nil, status: nil)

def event_params(event = nil)
permitted = EventPolicy.new(current_user, Event).permitted_attributes.dup
permitted << {event_sessions_attributes: EventSessionPolicy.new(current_user, EventSession).permitted_attributes + [:id]}
permitted << {allowed_operating_system_ids: []}

derived_params = {}
if params[:save_draft]
Expand Down

0 comments on commit d91752c

Please sign in to comment.