Skip to content

Commit

Permalink
Fixes undefined helper_method for rails 5 api
Browse files Browse the repository at this point in the history
- [x] rails 5 ActionController::API does not have `helper_method`
anymore

closes #163
  • Loading branch information
vnegrisolo committed Sep 16, 2016
1 parent 2a8cba5 commit e627cae
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/decent_exposure/exposure.rb
Expand Up @@ -67,6 +67,8 @@ def expose_attribute!
end

def expose_helper_methods!
return unless controller.respond_to?(:helper_method)

controller.helper_method attribute.getter_method_name
end

Expand Down
90 changes: 90 additions & 0 deletions spec/features/api_birds_controller_spec.rb
@@ -0,0 +1,90 @@
require "spec_helper"
require "support/rails_app"
require "rspec/rails"

RSpec.describe Api::BirdsController, type: :controller do
let(:bird){ Bird.new }

context 'when birds relation is exposed' do
class Api::BirdsController < API_SUPER_CLASS
expose :birds, ->{ Bird.all }

def index
head :ok
end
end

it "fetches all birds" do
expect(Bird).to receive(:all).and_return([bird])
get :index
expect(controller.birds).to eq([bird])
end
end

context 'when a bird is exposed' do
class Api::BirdsController < API_SUPER_CLASS
expose :bird

def show
head :ok
end

def new
head :ok
end
end

it "finds model by id" do
expect(Bird).to receive(:find).with("bird-id").and_return(bird)
get :show, request_params(id: "bird-id")
expect(controller.bird).to eq(bird)
end

it "finds model by bird_id" do
expect(Bird).to receive(:find).with("bird-id").and_return(bird)
get :new, request_params(bird_id: "bird-id")
expect(controller.bird).to eq(bird)
end

it "builds bird if id is not provided" do
get :new
expect(controller.bird).to be_a(Bird)
end
end

context "when bird_params is defined" do
class Api::BirdsController < API_SUPER_CLASS
expose :bird

def create
head :ok
end

def bird_params
params.require(:bird).permit(:name)
end
end

it "bird is build with params set" do
post :create, request_params(bird: { name: "crow" })
expect(controller.bird.name).to eq("crow")
end
end

context 'when a bird? with a question mark is exposed' do
class Api::BirdsController < API_SUPER_CLASS
expose :bird
expose :bird?, -> { bird.present? }

def show
head :ok
end
end

it "exposes bird?" do
expect(Bird).to receive(:find).with("bird-id").and_return(bird)
get :show, request_params(id: "bird-id")
expect(controller.bird?).to be true
end
end
end
16 changes: 16 additions & 0 deletions spec/support/rails_app.rb
Expand Up @@ -16,6 +16,10 @@ def routes
@routes ||= ActionDispatch::Routing::RouteSet.new.tap do |routes|
routes.draw do
resources :birds

namespace :api do
resources :birds
end
end
end
end
Expand All @@ -41,3 +45,15 @@ class ApplicationController < ActionController::Base

class BirdsController < ApplicationController
end

module Api
end

API_SUPER_CLASS = if Rails::VERSION::MAJOR < 5
ApplicationController
else
ActionController::API
end

class Api::BirdsController < API_SUPER_CLASS
end

0 comments on commit e627cae

Please sign in to comment.