Skip to content

Commit

Permalink
Extract module for list of permitted params
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured committed Mar 13, 2024
1 parent 104a0a0 commit a838d97
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/api/using_grape/artworks_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ArtworksEndpoint < Grape::API

helpers do
def artwork_params
params.permit(:amount_cents, :artist_name, :medium, :title)
params.permit(*Artwork.permitted_params)
end
end

Expand Down
7 changes: 7 additions & 0 deletions app/models/artwork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ class Artwork < ApplicationRecord
validates :featured, inclusion: {in: [true, false]}
validates :medium, presence: true
validates :title, presence: true

include HasPermittedParams
has_permitted_params \
:amount_cents,
:artist_name,
:medium,
:title
end
13 changes: 13 additions & 0 deletions app/models/concerns/has_permitted_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module HasPermittedParams
extend ActiveSupport::Concern

class_methods do
def has_permitted_params(*params)
@permitted_params = params
end

def permitted_params
@permitted_params
end
end
end

0 comments on commit a838d97

Please sign in to comment.