Skip to content

Instantly share code, notes, and snippets.

@petitviolet
Last active April 25, 2024 16:43
Show Gist options
  • Select an option

  • Save petitviolet/1f7965e2c3624f475282d71df6b36c58 to your computer and use it in GitHub Desktop.

Select an option

Save petitviolet/1f7965e2c3624f475282d71df6b36c58 to your computer and use it in GitHub Desktop.

Revisions

  1. petitviolet revised this gist Nov 10, 2020. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions rails_call_another_controller_action.rb
    Original file line number Diff line number Diff line change
    @@ -32,22 +32,30 @@ class ItemsController < ActionController::Base

    def show
    request.params[:item_id] = request.params[:id]
    res = AnotherController.dispatch(:get, request, response)
    ac = AnotherController.new
    res = ac.dispatch(:get, request, response)
    item_id = ac.instance_variable_get(:@item_id)
    puts "instance var item_id: #{item_id}"
    render json: { msg: "OK", body: JSON.parse(res[2].body) }, status: 200
    end
    end

    class AnotherController < ActionController::Base
    include Rails.application.routes.url_helpers
    before_action :set_item_id

    def get
    item = {
    "1" => "hello",
    "2" => "world",
    }[params[:item_id]]
    }[@item_id]

    render json: { item: item }, status: 200
    end
    private
    def set_item_id
    @item_id = params.require(:item_id)
    end
    end

    require "minitest/autorun"
  2. petitviolet created this gist Nov 9, 2020.
    76 changes: 76 additions & 0 deletions rails_call_another_controller_action.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    # frozen_string_literal: true

    require "bundler/inline"

    gemfile(true) do
    source "https://rubygems.org"

    git_source(:github) { |repo| "https://github.com/#{repo}.git" }

    gem "rails", "6.0.0"
    end

    require "rack/test"
    require "action_controller/railtie"

    class TestApp < Rails::Application
    config.root = __dir__
    config.hosts << "example.org"
    config.session_store :cookie_store, key: "cookie_store_key"
    secrets.secret_key_base = "secret_key_base"

    config.logger = Logger.new($stdout)
    Rails.logger = config.logger

    routes.draw do
    get "/items/:id" => "items#show"
    end
    end

    class ItemsController < ActionController::Base
    include Rails.application.routes.url_helpers

    def show
    request.params[:item_id] = request.params[:id]
    res = AnotherController.dispatch(:get, request, response)
    render json: { msg: "OK", body: JSON.parse(res[2].body) }, status: 200
    end
    end

    class AnotherController < ActionController::Base
    include Rails.application.routes.url_helpers

    def get
    item = {
    "1" => "hello",
    "2" => "world",
    }[params[:item_id]]

    render json: { item: item }, status: 200
    end
    end

    require "minitest/autorun"

    class BugTest < Minitest::Test
    include Rack::Test::Methods

    def test_returns_success
    get "/items/1"
    assert last_response.ok?
    assert last_response.body == '{"msg":"OK","body":{"item":"hello"}}', last_response.body

    get "/items/2"
    assert last_response.ok?
    assert last_response.body == '{"msg":"OK","body":{"item":"world"}}', last_response.body

    get "/items/3"
    assert last_response.ok?
    assert last_response.body == '{"msg":"OK","body":{"item":null}}', last_response.body
    end

    private
    def app
    Rails.application
    end
    end