Skip to content

Commit

Permalink
Support Rails 4
Browse files Browse the repository at this point in the history
  • Loading branch information
jonleighton committed Oct 17, 2013
1 parent dae49ec commit 38b98a7
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 43 deletions.
4 changes: 4 additions & 0 deletions Appraisals
@@ -1,3 +1,7 @@
appraise 'rails-4-0' do
gem 'rails', '~> 4.0.0'
end

appraise 'rails-3-2' do
gem 'rails', '~> 3.2.0'
end
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -111,6 +111,7 @@ to be called as it would always be `#call`. So this is omitted:

``` ruby
require 'focused_controller/functional_test_helper'
require_dependency 'users_controller'

module UsersController
class CreateTest < ActionController::TestCase
Expand Down Expand Up @@ -184,6 +185,7 @@ It also provides accessors for these objects in your test class.

``` ruby
require 'focused_controller/test_helper'
require_dependency 'users_controller'

module UsersController
class CreateTest < ActiveSupport::TestCase
Expand Down
4 changes: 2 additions & 2 deletions focused_controller.gemspec
Expand Up @@ -18,9 +18,9 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency 'actionpack', '~> 3.0'
s.add_dependency 'actionpack', '>= 3.0', '<= 4.0'

s.add_development_dependency 'minitest', '~> 2.11'
s.add_development_dependency 'minitest', '~> 4.2'
s.add_development_dependency 'capybara', '~> 2.1'
s.add_development_dependency 'capybara_minitest_spec', '~> 1.0'
s.add_development_dependency 'poltergeist', '~> 1.3'
Expand Down
6 changes: 5 additions & 1 deletion lib/focused_controller/mixin.rb
Expand Up @@ -13,7 +13,7 @@ module Mixin
end

module ClassMethods
def controller_path
def focused_controller_path
@focused_controller_path ||= name && name.sub(/\:\:[^\:]+$/, '').sub(/Controller$/, '').underscore
end

Expand Down Expand Up @@ -44,6 +44,10 @@ def controller_name
end
end

def _prefixes
super.dup.unshift(self.class.focused_controller_path)
end

def action_name
self.class.name.demodulize.underscore
end
Expand Down
10 changes: 9 additions & 1 deletion lib/focused_controller/test_helper.rb
Expand Up @@ -28,7 +28,7 @@ def initialize(env = {})
end

def cookie_jar
@cookie_jar ||= ActionDispatch::Cookies::CookieJar.new
@cookie_jar ||= ActionDispatch::Cookies::CookieJar.build(self)
end

def flash
Expand Down Expand Up @@ -59,6 +59,10 @@ def gsub(*)
self
end

def delete(*)
self
end

def to_s
"#{helper_name}(#{args.each(&:to_s).join(', ')})"
end
Expand Down Expand Up @@ -172,5 +176,9 @@ def stub_url(*names)
end
end
end

def optimize_routes_generation?(*)
false
end
end
end
2 changes: 1 addition & 1 deletion test/app/Gemfile
@@ -1,6 +1,6 @@
source 'http://rubygems.org'

gem 'rails', ENV['RAILS_VERSION'] || '~> 3.2.1'
gem 'rails', ENV['RAILS_VERSION'] || '~> 4.0.0'
gem 'focused_controller', :path => '../..'
gem 'rspec'
gem 'rspec-rails'
2 changes: 2 additions & 0 deletions test/app/app/models/post.rb
@@ -1,3 +1,5 @@
require 'active_model'

class Post
extend ActiveModel::Naming
include ActiveModel::Validations
Expand Down
2 changes: 1 addition & 1 deletion test/app/app/views/posts/index.html.erb
Expand Up @@ -13,7 +13,7 @@
<td><%= post.body %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
<td><%= link_to 'Destroy', post, :method => :delete %></td>
</tr>
<% end %>
</table>
Expand Down
5 changes: 2 additions & 3 deletions test/app/config/environments/development.rb
Expand Up @@ -6,9 +6,6 @@
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false

# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true

# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
Expand All @@ -18,4 +15,6 @@

# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin

config.eager_load = false
end
5 changes: 2 additions & 3 deletions test/app/config/environments/test.rb
Expand Up @@ -11,9 +11,6 @@
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"

# Log error messages when you accidentally call methods on nil
config.whiny_nils = true

# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
Expand All @@ -26,4 +23,6 @@

# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr

config.eager_load = false
end
1 change: 1 addition & 0 deletions test/app/config/initializers/secret_token.rb
Expand Up @@ -5,3 +5,4 @@
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
App::Application.config.secret_token = '0bdf8849022e542ad15148bd7052bff324953e0b40f3545d0cb3f11f0347837d9cde80c10af262744b5725e85838a432fbd3bab2f2f82ede04da29877dfa5358'
App::Application.config.secret_key_base = '0bdf8849022e542ad15148bd7052bff324953e0b40f3545d0cb3f11f0347837d9cde80c10af262744b5725e85838a432fbd3bab2f2f82ede04da29877dfa5358'
1 change: 1 addition & 0 deletions test/app/test/functional/posts_controller_test.rb
@@ -1,4 +1,5 @@
require 'test_helper'
require_dependency 'posts_controller'

module PostsController
class TestCase < ActionController::TestCase
Expand Down
1 change: 1 addition & 0 deletions test/app/test/unit/controllers/posts_controller_test.rb
@@ -1,4 +1,5 @@
require 'test_helper'
require_dependency 'posts_controller'

module PostsController
class TestCase < ActiveSupport::TestCase
Expand Down
52 changes: 26 additions & 26 deletions test/unit/functional_test_helper_test.rb
Expand Up @@ -29,6 +29,20 @@ class ShowTest < TestCase
describe FunctionalTestHelper do
subject { FakePostsController::IndexTest.new }

def record_last_process
subject.singleton_class.class_eval do
attr_reader :last_process

def process(action_name, *args)
if ActionPack::VERSION::MAJOR < 4
args.unshift args.pop
end

@last_process = [action_name, *args]
end
end
end

it 'automatically determines the controller class' do
FakePostsController::IndexTest.controller_class.
must_equal FakePostsController::Index
Expand All @@ -37,64 +51,50 @@ class ShowTest < TestCase
end

it "doesn't require using the action name to dispatch the action" do
subject.singleton_class.class_eval do
attr_reader :last_process

def process(*args)
@last_process = args
end
end
record_last_process

parameters = {:foo => "bar"}
session = {:baz => "bat"}
flash = {:quux => "wibble"}


subject.get parameters, session, flash
subject.last_process.must_equal ['call', parameters, session, flash, 'GET']
subject.last_process.must_equal ['call', 'GET', parameters, session, flash]

subject.post parameters, session, flash
subject.last_process.must_equal ['call', parameters, session, flash, 'POST']
subject.last_process.must_equal ['call', 'POST', parameters, session, flash]

subject.put parameters, session, flash
subject.last_process.must_equal ['call', parameters, session, flash, 'PUT']
subject.last_process.must_equal ['call', 'PUT', parameters, session, flash]

subject.delete parameters, session, flash
subject.last_process.must_equal ['call', parameters, session, flash, 'DELETE']
subject.last_process.must_equal ['call', 'DELETE', parameters, session, flash]

subject.head parameters, session, flash
subject.last_process.must_equal ['call', parameters, session, flash, 'HEAD']
subject.last_process.must_equal ['call', 'HEAD', parameters, session, flash]
end


describe "testing a non-focused controller" do
it "allows using the action name to dispatch the action" do
subject.singleton_class.class_eval do
attr_reader :last_process

def process(*args)
@last_process = args
end
end
record_last_process

parameters = {:foo => "bar"}
session = {:baz => "bat"}
flash = {:quux => "wibble"}

subject.get :show, parameters, session, flash
subject.last_process.must_equal [:show, parameters, session, flash, 'GET']
subject.last_process.must_equal [:show, 'GET', parameters, session, flash]

subject.post :create, parameters, session, flash
subject.last_process.must_equal [:create, parameters, session, flash, 'POST']
subject.last_process.must_equal [:create, 'POST', parameters, session, flash]

subject.put :update, parameters, session, flash
subject.last_process.must_equal [:update, parameters, session, flash, 'PUT']
subject.last_process.must_equal [:update, 'PUT', parameters, session, flash]

subject.delete :destroy, parameters, session, flash
subject.last_process.must_equal [:destroy, parameters, session, flash, 'DELETE']
subject.last_process.must_equal [:destroy, 'DELETE', parameters, session, flash]

subject.head :show, parameters, session, flash
subject.last_process.must_equal [:show, parameters, session, flash, 'HEAD']
subject.last_process.must_equal [:show, 'HEAD', parameters, session, flash]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/unit/mixin_test.rb
Expand Up @@ -30,8 +30,8 @@ module FocusedController

subject { klass.new }

it "has a .controller_path of 'posts'" do
klass.controller_path.must_equal 'posts'
it "has a .focused_controller_path of 'posts'" do
klass.focused_controller_path.must_equal 'posts'
end

it "has a .call which dispatches the #call action" do
Expand Down
6 changes: 3 additions & 3 deletions test/unit/route_mapper_test.rb
Expand Up @@ -27,8 +27,8 @@ def recognize(path, environment = {})
it 'creates routes that map to focused controllers' do
route_set.draw do
focused_controller_routes do
match 'posts' => 'PostsController::Index'
match 'posts/all' => 'posts#index'
get 'posts' => 'PostsController::Index'
get 'posts/all' => 'posts#index'

resources :comments do
resources :replies
Expand Down Expand Up @@ -67,7 +67,7 @@ def app.call; end

route_set.draw do
focused_controller_routes do
match 'posts' => app
get 'posts' => app
end
end
recognize('/posts').app.must_equal app
Expand Down

0 comments on commit 38b98a7

Please sign in to comment.