module Undercarriage::Controllers::Restful::PermittedAttributesConcern

Permitted attributes

Usage

class ExamplesController < ApplicationController
  include Undercarriage::Controllers::Restful::PermittedAttributesConcern
end

Protected Instance Methods

create_resource_params() click to toggle source

Permitted params for `create` action

Permitted params for the `create` action scoped to the resource.

# File lib/undercarriage/controllers/restful/permitted_attributes_concern.rb, line 82
def create_resource_params
  permitted = permitted_create_attributes

  params.require(resource_scope).permit(permitted)
end
permitted_create_attributes() click to toggle source

Permitted attributes for `create` action

Permitted attributes for the `create` action. If `create` and `update` do not need to be different, you can still override the `permitted_attributes` method which would be applied to both `create` and `update`.

Example

def permitted_create_attributes
  %i[thingy dilly whatsit]
end

# Use this method is `create` and `update` do not need different permitted attributes
def permitted_attributes
  [
    :thingy, :dilly, :whatsit,
    { whosits_attributes: %i[id _destroy name] }
  ]
end
# File lib/undercarriage/controllers/restful/permitted_attributes_concern.rb, line 40
def permitted_create_attributes
  permitted_attributes_fallback
end
permitted_update_attributes() click to toggle source

Permitted attributes for `update` action

Permitted attributes for the `update` action. If `create` and `update` do not need to be different, you can still override the `permitted_attributes` method which would be applied to both `create` and `update`.

Example

def permitted_update_attributes
  %i[thingy dilly whatsit]
end

# Use this method is `create` and `update` do not need different permitted attributes
def permitted_attributes
  [
    :thingy, :dilly, :whatsit,
    { whosits_attributes: %i[id _destroy name] }
  ]
end
# File lib/undercarriage/controllers/restful/permitted_attributes_concern.rb, line 63
def permitted_update_attributes
  permitted_attributes_fallback
end
resource_new_params() click to toggle source

New resource decider

For the `new` action, resource params are `nil`. For the `create` action, resource params are the posted params from `create_resource_params` method

# File lib/undercarriage/controllers/restful/permitted_attributes_concern.rb, line 73
def resource_new_params
  action_name == 'new' ? nil : create_resource_params
end
update_resource_params() click to toggle source

Permitted params for `update` action

Permitted params for the `update` action scoped to the resource.

# File lib/undercarriage/controllers/restful/permitted_attributes_concern.rb, line 93
def update_resource_params
  permitted = permitted_update_attributes

  params.require(resource_scope).permit(permitted)
end

Private Instance Methods

permitted_attributes_fallback() click to toggle source
# File lib/undercarriage/controllers/restful/permitted_attributes_concern.rb, line 101
def permitted_attributes_fallback
  with_method = self.class.instance_methods(false).include?(:permitted_attributes)

  with_method ? permitted_attributes : []
end