Sha256: 1a184e7175740bdf0d86017d6b1379136f87b29da87ca609ea711612da604f2a

Contents?: true

Size: 1.64 KB

Versions: 12

Compression:

Stored size: 1.64 KB

Contents

<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApiController
  <%= s3_storage? ? "include MobileWorkflow::S3Storable" : "" %>
  before_action :rewrite_payload, only: :create

  load_and_authorize_resource

<% if index_action? -%>
  def index
    render json: @<%= plural_table_name %>.collect(&:list_item_as_json)
  end
<% end -%>

<% if show_action? -%>
  def show
    render json: @<%= singular_table_name %>.display_as_json
  end
<% end -%>

<% if create_action? -%>
  def create
    <% if doorkeeper_oauth? -%>
    @<%= singular_table_name %>.user = current_user
    <% end -%>
    if @<%= singular_table_name %>.save
      <% if s3_storage? -%>
      render json: { binary_urls: binary_urls(@<%= singular_table_name %>), response: @<%= singular_table_name %> }, status: :created
      <% else -%>
      render json: { response: @<%= singular_table_name %> }, status: :created      
      <% end -%>
    else
      render json: { message: @<%= singular_table_name %>.errors.full_messages.to_sentence }, status: :unprocessable_entity
    end
  end

  private
  def rewrite_payload
    # Use this method to make any changes to params to make them compatible with ActiveRecord
    
    # 1. Example to get selected id from a list
    # passport_id = params.dig(:payload, :choose_passport, :selected, :id)

    Rails.logger.debug "Pre-rewrite params: #{params}"

    parsed_params = <%= controller_class_name.singularize %>Deserializer.parse(params)
    params.merge!(parsed_params)
  end

  def <%= singular_table_name.underscore %>_params
    params.require(:<%= singular_table_name %>).permit(<%= permitted_params %>)
  end
<% end -%>
end
<% end %>

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
mobile_workflow-0.12.2 lib/generators/mobile_workflow/templates/controller.rb.erb
mobile_workflow-0.12.1 lib/generators/mobile_workflow/templates/controller.rb.erb
mobile_workflow-0.12.0 lib/generators/mobile_workflow/templates/controller.rb.erb
mobile_workflow-0.11.1 lib/generators/mobile_workflow/templates/controller.rb.erb
mobile_workflow-0.11.0 lib/generators/mobile_workflow/templates/controller.rb.erb
mobile_workflow-0.10.2 lib/generators/mobile_workflow/templates/controller.rb.erb
mobile_workflow-0.10.1 lib/generators/mobile_workflow/templates/controller.rb.erb
mobile_workflow-0.10.0 lib/generators/mobile_workflow/templates/controller.rb.erb
mobile_workflow-0.9.0 lib/generators/mobile_workflow/templates/controller.rb.erb
mobile_workflow-0.8.9 lib/generators/mobile_workflow/templates/controller.rb.erb
mobile_workflow-0.7.9 lib/generators/mobile_workflow/templates/controller.rb.erb
mobile_workflow-0.7.8 lib/generators/mobile_workflow/templates/controller.rb.erb