Skip to content

Commit

Permalink
refactor from helper to controller concern
Browse files Browse the repository at this point in the history
  • Loading branch information
rubyconvict committed Apr 1, 2016
1 parent 8c20655 commit cbbca94
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.4.2)
rspec (3.4.0)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-core (3.4.3)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
Expand All @@ -96,6 +100,7 @@ GEM
rspec-mocks (~> 3.4.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
ruby-progressbar (1.7.5)
simplecov (0.11.2)
docile (~> 1.1.0)
json (~> 1.8)
Expand Down
1 change: 1 addition & 0 deletions lib/cache_key_for/controller_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module CacheKeyFor
module ControllerHelpers
extend ActiveSupport::Concern
include AbstractController::Helpers

included do
helper_method :cache_key_for, :cache_key_for_view
Expand Down
62 changes: 60 additions & 2 deletions spec/controllers/concerns/cache_key_for_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'rails_helper'
=begin
require_relative '../../../lib/cache_key_for/controller_helpers'
Expand Down Expand Up @@ -29,7 +28,7 @@ class ApplicationController < ActionController::Base
end
end
=end

=begin
# spec/controllers/concerns/likable_spec.rb
require_relative '../../../lib/cache_key_for/controller_helpers'
Expand All @@ -50,4 +49,63 @@ def index
end
# write test
end
=end

# This is an example spec that uses the ControllerConcernHelper module
require "rails_helper"
require 'ostruct'
require 'rails'
require_relative '../../../lib/cache_key_for/controller_helpers'

# https://gist.github.com/penman/00a9b834db160fe71866
describe CacheKeyFor::ControllerHelpers, type: :controller_concern do
controller do
#include AbstractController::Callbacks
include AbstractController::Rendering

#before_action :authenticate

def test_action
person = OpenStruct.new
person.id = 1
person.name = 'John'
person.cache_key = '1-john'

cc = cache_key_for([person], 'datacenters')
render text: cc
end
end

context "when no authentication is supplied" do
it "responds with 401 Unauthorized" do
#expect_any_instance_of(CacheKeyFor::ControllerHelpers).to receive(:cache_key_for).and_return('1-john')
expect(request.get("/").status).to eq 200
end
end
=begin
context "when correct authentication is supplied" do
let!(:secret) { SecureRandom.uuid }
let!(:device) { create(:device, secret: secret) }
let!(:response) { request.get("/", authenticate(device.id, secret)) }
it "responds with 200 OK" do
expect(response.status).to eq 200
end
it "finds device ID" do
expect(response.body.chomp).to eq device.id
end
end
context "when incorrect authentication is supplied" do
it "responds with 401 Unauthorized" do
authentication = cache_key_for(SecureRandom.uuid, SecureRandom.uuid)
response = request.get("/", authentication)
expect(response.status).to eq 401
end
end
=end
end
5 changes: 3 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# require "rails/test_unit/railtie"

require 'rspec/rails'
#require_relative './../lib/cache_key_for/controller_helpers'
require_relative './../lib/cache_key_for/controller_helpers'

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
Expand All @@ -26,5 +26,6 @@
config.include ActionView::Helpers
# config.include ControllerHelpers, :type => :controller
#config.include ApplicationHelper, :type => :helper
#config.include CacheKeyFor::ControllerHelpers, :type => :controller
#config.include AbstractController::Helpers, :type => :helper
#config.include CacheKeyFor::ControllerHelpers, type: :helper
end
31 changes: 31 additions & 0 deletions spec/support/controller_concern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module ControllerConcernHelper
module ClassMethods
def controller(&block)
before { controller(&block) }
end
end

def controller(&block)
@controller ||= begin
concern = described_class
Class.new(ActionController::Metal) do
include concern
end
end
@controller.class_eval(&block) if block
@controller
end

def app
@controller.action(:test_action)
end

def request
Rack::MockRequest.new(app)
end
end

RSpec.configure do |config|
config.include ControllerConcernHelper, type: :controller_concern
config.extend ControllerConcernHelper::ClassMethods, type: :controller_concern
end

0 comments on commit cbbca94

Please sign in to comment.