Skip to content

Instantly share code, notes, and snippets.

@fcy
Forked from rodrigopinto/refactory.rb
Created January 13, 2012 20:03
Show Gist options
  • Select an option

  • Save fcy/1608422 to your computer and use it in GitHub Desktop.

Select an option

Save fcy/1608422 to your computer and use it in GitHub Desktop.

Revisions

  1. fcy revised this gist Jan 13, 2012. 1 changed file with 17 additions and 2 deletions.
    19 changes: 17 additions & 2 deletions refactory.rb
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,24 @@
    # encoding: utf-8

    class OperationsController < ApplicationController
    respond_to :json
    rescue_from ActiveRecord::RecordNotFound, :with => :not_found

    def show
    operation = Operation.find(params[:id])
    respond_with operation
    end

    def create
    app = App.find_by_app_key!(params[:app_key]) #aqui retorna um 404
    app = App.find_by_app_key!(params[:app_key])
    operation = app.new_operation(params[:operation])
    operation.save
    respond_with operation
    end
    end

    private
    def not_found(exception = nil)
    response = {error: { :message => exception.message, :request => request.path }}
    render json: response, status: :not_found
    end
    end
  2. @rodrigopinto rodrigopinto revised this gist Jan 13, 2012. 1 changed file with 5 additions and 21 deletions.
    26 changes: 5 additions & 21 deletions refactory.rb
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,9 @@
    class OperationsController < ApplicationController
    respond_to :json
    def create
    app = App.find_by_app_key(params[:app_key])
    if !app.nil?
    operation = app.new_operation(params[:operation])
    if operation.save
    respond_to do |format|
    format.json { render json: operation, status: :created }
    end
    else
    respond_to do |format|
    format.json { render json: {errors: operation.errors}, status: :bad_request }
    end
    end
    else
    error = {
    request: request.path,
    message: 'No app found for that app_key'
    }
    respond_to do |format|
    format.json { render json: {error: error}, status: :not_found }
    end
    end
    app = App.find_by_app_key!(params[:app_key]) #aqui retorna um 404
    operation = app.new_operation(params[:operation])
    operation.save
    respond_with operation
    end
    end
  3. @rodrigopinto rodrigopinto created this gist Jan 13, 2012.
    25 changes: 25 additions & 0 deletions refactory.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    class OperationsController < ApplicationController
    def create
    app = App.find_by_app_key(params[:app_key])
    if !app.nil?
    operation = app.new_operation(params[:operation])
    if operation.save
    respond_to do |format|
    format.json { render json: operation, status: :created }
    end
    else
    respond_to do |format|
    format.json { render json: {errors: operation.errors}, status: :bad_request }
    end
    end
    else
    error = {
    request: request.path,
    message: 'No app found for that app_key'
    }
    respond_to do |format|
    format.json { render json: {error: error}, status: :not_found }
    end
    end
    end
    end