Skip to content

Instantly share code, notes, and snippets.

@CamonZ
Last active September 5, 2016 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CamonZ/e851b72a870ec5d9fcb2f78352742064 to your computer and use it in GitHub Desktop.
Save CamonZ/e851b72a870ec5d9fcb2f78352742064 to your computer and use it in GitHub Desktop.
module BaseParams
include JsonApiDeserialization
def allowed_params(raw_params)
jsonapi_parsed_params(raw_params, self.class.const_get(MODEL_NAME)).
require(self.class.const_get(MODEL_NAME)).
permit(self.class.const_get(PERMITTED_PARAMS))
end
end
require 'active_support/concern'
module FooParams
extend ActiveSupport::Concern
PERMITTED_PARAMS = [:foobar, :bar, :baz]
MODEL_NAME = :foo
include BaseParams
end
class FoosController < AuthenticatedController
include FooParams
def create
foo = Foo.create!(allowed_params(params))
render json: foo
end
end
require 'active_support/concern'
module JsonApiDeserialization
extend ActiveSupport::Concern
protected
def jsonapi_parsed_params(raw_params, root_key)
ActionController::Parameters.new({ "#{root_key}" => ActiveModelSerializers::Deserialization.jsonapi_parse!(raw_params) })
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment