Sha256: 3300ab37f4b325484cd98473b5f0c77e3dc026738a44ff7d638e71055e88ac9f

Contents?: true

Size: 1.79 KB

Versions: 3

Compression:

Stored size: 1.79 KB

Contents

class API::V1::<%= resource.pluralize %>Controller < RedBase::APIController
  before_filter :build_resource, :only => :create
  load_and_authorize_resource :except => [:destory]

  # GET /api/v1/<%= resource.pluralize.underscore %>
  def index
    respond_with(@<%= resource.pluralize.underscore %>)
  end

  def create
    <% fields.each do |name, type| %><% if type == "has_many" %>
    <%= name.underscore %> = <%= name.singularize.camelize %>.where({:id => params[:<%= resource.underscore %>][:<%= name %>]})
    @<%= resource.underscore %>.<%= name.underscore %> = <%= name.underscore %>
    <% end %><% end %>
    if @<%= resource.underscore %>.save
        respond_with(@<%= resource.underscore %>)
    else
        respond_with(@<%= resource.underscore %>.errors)
    end
  end

  def show
    respond_with(@<%= resource.underscore %>)
  end

  def update
    <% fields.each do |name, type| %><% if type == "has_many" %>
    <%= name.underscore %> = <%= name.singularize.camelize %>.where({:id => params[:<%= resource.underscore %>][:<%= name %>]})
    @<%= resource.underscore %>.<%= name.underscore %> = <%= name.underscore %>
    <% end %><% end %>

    if @<%= resource.underscore %>.update(resource_params)
      respond_with(@<%= resource.underscore %>)
    else
      respond_with(@<%= resource.underscore %>.errors)
    end
  end

  def destroy
    ids = params[:id].split(",")
    @<%= resource.pluralize.underscore %> = ::<%= resource %>.where(:id => ids)
    authorize! :destroy, @<%= resource.pluralize.underscore %>
    @<%= resource.pluralize.underscore %>.destroy_all
  end

  def build_resource
    @<%= resource.underscore %> = ::<%= resource %>.new(resource_params)
  end

  def resource_params
    params.require(:<%= resource.underscore %>).permit(:id<%= fields_as_params(:relations => true) %>)
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
red_base-0.6.0 lib/generators/red_base/templates/api/controller.rb.erb
red_base-0.5.1 lib/generators/red_base/templates/api/controller.rb.erb
red_base-0.5.0 lib/generators/red_base/templates/api/controller.rb.erb