Skip to content

Commit

Permalink
Merge pull request #145 from plataformatec/rails-5
Browse files Browse the repository at this point in the history
Rails 5
  • Loading branch information
lucasmazza committed Dec 19, 2015
2 parents 3f9ae66 + 7b4cc69 commit f62fa08
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 41 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
@@ -1,3 +1,5 @@
sudo: false
cache: bundler
language: ruby
rvm:
- 1.9.3
Expand All @@ -6,9 +8,19 @@ rvm:
- 2.2
gemfile:
- Gemfile
- gemfiles/Gemfile-rails.5.0.x
matrix:
exclude:
- rvm: 1.9.3
gemfile: gemfiles/Gemfile-rails.5.0.x
- rvm: 2.0.0
gemfile: gemfiles/Gemfile-rails.5.0.x
- rvm: 2.1
gemfile: gemfiles/Gemfile-rails.5.0.x
notifications:
email: false
slack:
on_pull_requests: false
on_success: change
on_failure: always
secure: Qr9/3LRW22qAoJ/9mynVKiI1p/AVIdW4GqcaymMjC6KH46EUwtbB4DxBa3K+gfBYOcfDKDe0vt3TUL/FMCgchqHum5QaNrAfSLj6k363sZqqW+dEuEMJO2e+QsIQUexBnDDQIJNSnr5DqoQ5+npZ8YwzWeAp8nRUs5uYI/RlXXo=
11 changes: 7 additions & 4 deletions Gemfile.lock
Expand Up @@ -2,7 +2,7 @@ PATH
remote: .
specs:
responders (2.1.0)
railties (>= 4.2.0, < 5)
railties (>= 4.2.0, < 5.1)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -33,7 +33,7 @@ GEM
erubis (2.7.0)
i18n (0.7.0)
json (1.8.2)
loofah (2.0.1)
loofah (2.0.3)
nokogiri (>= 1.5.9)
metaclass (0.0.4)
mini_portile (0.6.2)
Expand All @@ -42,12 +42,12 @@ GEM
metaclass (~> 0.0.1)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
rack (1.6.0)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.6)
rails-dom-testing (1.0.7)
activesupport (>= 4.2.0.beta, < 5.0)
nokogiri (~> 1.6.0)
rails-deprecated_sanitizer (>= 1.0.1)
Expand All @@ -71,3 +71,6 @@ DEPENDENCIES
activemodel (= 4.2.1)
mocha
responders!

BUNDLED WITH
1.11.2
9 changes: 9 additions & 0 deletions gemfiles/Gemfile-rails.5.0.x
@@ -0,0 +1,9 @@
source 'https://rubygems.org'

gemspec path: '..'

gem 'activemodel', github: 'rails/rails'
gem 'railties', github: 'rails/rails'
gem 'mocha'
gem 'rails-controller-testing', github: 'rails/rails-controller-testing'
gem 'rack', github: 'rack/rack'
2 changes: 1 addition & 1 deletion responders.gemspec
Expand Up @@ -18,5 +18,5 @@ Gem::Specification.new do |s|
s.files = Dir["CHANGELOG.md", "MIT-LICENSE", "README.md", "lib/**/*"]
s.require_paths = ["lib"]

s.add_dependency "railties", ">= 4.2.0", "< 5"
s.add_dependency "railties", ">= 4.2.0", "< 5.1"
end
27 changes: 14 additions & 13 deletions test/action_controller/respond_with_test.rb
Expand Up @@ -52,7 +52,7 @@ class Store < Question

class RespondWithController < ApplicationController
class CustomerWithJson < Customer
def to_json; super; end
def to_json(*); super; end
end

respond_to :html, :json, :touch
Expand All @@ -69,13 +69,13 @@ def using_hash_resource

def using_resource_with_block
respond_with(resource) do |format|
format.csv { render :text => "CSV" }
format.csv { render :body => "CSV" }
end
end

def using_resource_with_overwrite_block
respond_with(resource) do |format|
format.html { render :text => "HTML" }
format.html { render :body => "HTML" }
end
end

Expand Down Expand Up @@ -105,7 +105,7 @@ def using_options_with_template
end

def using_resource_with_responder
responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" }
responder = proc { |c, r, o| c.render :body => "Resource name is #{r.first.name}" }
respond_with(resource, :responder => responder)
end

Expand All @@ -117,7 +117,7 @@ def using_resource_with_action

def using_responder_with_respond
responder = Class.new(ActionController::Responder) do
def respond; @controller.render :text => "respond #{format}"; end
def respond; @controller.render :body => "respond #{format}"; end
end
respond_with(resource, :responder => responder)
end
Expand Down Expand Up @@ -145,7 +145,7 @@ class InheritedRespondWithController < RespondWithController

def index
respond_with(resource) do |format|
format.json { render :text => "JSON" }
format.json { render :body => "JSON" }
end
end
end
Expand Down Expand Up @@ -408,15 +408,13 @@ def test_using_resource_for_put_with_html_rerender_on_failure_even_on_method_ove
def test_using_resource_for_put_with_xml_yields_no_content_on_success
@request.accept = "application/xml"
put :using_resource
assert_equal "application/xml", @response.content_type
assert_equal 204, @response.status
assert_equal "", @response.body
end

def test_using_resource_for_put_with_json_yields_no_content_on_success
@request.accept = "application/json"
put :using_resource_with_json
assert_equal "application/json", @response.content_type
assert_equal 204, @response.status
assert_equal "", @response.body
end
Expand Down Expand Up @@ -458,7 +456,6 @@ def test_using_resource_for_delete_with_xml_yields_no_content_on_success
Customer.any_instance.stubs(:destroyed?).returns(true)
@request.accept = "application/xml"
delete :using_resource
assert_equal "application/xml", @response.content_type
assert_equal 204, @response.status
assert_equal "", @response.body
end
Expand All @@ -467,7 +464,6 @@ def test_using_resource_for_delete_with_json_yields_no_content_on_success
Customer.any_instance.stubs(:destroyed?).returns(true)
@request.accept = "application/json"
delete :using_resource_with_json
assert_equal "application/json", @response.content_type
assert_equal 204, @response.status
assert_equal "", @response.body
end
Expand Down Expand Up @@ -640,7 +636,7 @@ def test_using_resource_with_responder
end

def test_using_resource_with_set_responder
RespondWithController.responder = proc { |c, r, o| c.render :text => "Resource name is #{r.first.name}" }
RespondWithController.responder = proc { |c, r, o| c.render :body => "Resource name is #{r.first.name}" }
get :using_resource
assert_equal "Resource name is david", @response.body
ensure
Expand Down Expand Up @@ -679,7 +675,12 @@ def with_test_route_set

class LocationsController < ApplicationController
respond_to :html
before_filter :set_resource
# TODO: Remove this when we drop support for Rails 4.2.
if respond_to?(:before_action)
before_action :set_resource
else
before_filter :set_resource
end

def create
respond_with @resource, location: -> { 'given_location' }
Expand All @@ -704,7 +705,7 @@ def test_redirects_to_block_location_on_success
end

def test_renders_page_on_fail
post :create, fail: true
post :create, params: { fail: true }
assert @response.body.include?('new.html.erb')
end

Expand Down
2 changes: 1 addition & 1 deletion test/responders/controller_method_test.rb
Expand Up @@ -29,7 +29,7 @@ class PeopleController < ApplicationController
def index
@array = []
respond_with(@array) do |format|
format.html { render :text => "Success!" }
format.html { render :body => "Success!" }
end
end
end
Expand Down
57 changes: 39 additions & 18 deletions test/responders/flash_responder_test.rb
Expand Up @@ -5,24 +5,43 @@ class FlashResponder < ActionController::Responder
end

class AddressesController < ApplicationController
before_filter :set_resource
if respond_to?(:before_action)
before_action :set_resource
else
before_filter :set_resource
end
self.responder = FlashResponder

respond_to :js, :only => :create

FLASH_PARAM_VALUES = {
'true' => true,
'false' => false
}

FLASH_NOW_PARAM_VALUES = {
'true' => true,
'false' => false,
'on_success' => :on_success,
'on_failure' => :on_failure
}

def action
options = params.slice(:flash, :flash_now)
set_flash = FLASH_PARAM_VALUES[params[:flash].to_s]
set_flash_now = FLASH_NOW_PARAM_VALUES[params[:flash_now].to_s]

flash[:success] = "Flash is set" if params[:set_flash]
respond_with(@resource, options)
respond_with(@resource, flash: set_flash, flash_now: set_flash_now)
end

alias :new :action
alias :create :action
alias :update :action
alias :destroy :action

def with_block
respond_with(@resource) do |format|
format.html { render :text => "Success!" }
format.html { render :html => "Success!" }
end
end

Expand All @@ -36,7 +55,9 @@ def with_html

def flexible
options = params[:responder_options] || {}
respond_with(@resource, options)
flash_now, alert = options.values_at(:flash_now, :alert)

respond_with(@resource, flash_now: flash_now, alert: alert)
end

protected
Expand Down Expand Up @@ -76,7 +97,7 @@ def test_sets_success_flash_message_on_non_get_requests
end

def test_sets_failure_flash_message_on_not_get_requests
post :create, :fail => true
post :create, :params => { :fail => true }
assert_equal "Resource could not be created", flash[:failure]
end

Expand All @@ -86,7 +107,7 @@ def test_does_not_set_flash_message_on_get_requests
end

def test_sets_flash_message_for_the_current_controller
put :update, :fail => true
put :update, :params => { :fail => true }
assert_equal "Oh no! We could not update your address!", flash[:failure]
end

Expand All @@ -101,12 +122,12 @@ def test_sets_flash_message_with_interpolation_options
end

def test_does_not_set_flash_if_flash_false_is_given
post :create, :flash => false
post :create, :params => { :flash => false }
assert flash.empty?
end

def test_does_not_overwrite_the_flash_if_already_set
post :create, :set_flash => true
post :create, :params => { :set_flash => true }
assert_equal "Flash is set", flash[:success]
end

Expand All @@ -122,18 +143,18 @@ def test_sets_now_flash_message_on_javascript_requests
end

def test_sets_flash_message_can_be_set_to_now
post :create, :flash_now => true
post :create, :params => { :flash_now => true }
assert_equal "Resource created with success", @controller.flash.now[:success]
assert_flash_now :success
end

def test_sets_flash_message_can_be_set_to_now_only_on_success
post :create, :flash_now => :on_success
post :create, :params => { :flash_now => :on_success }
assert_equal "Resource created with success", @controller.flash.now[:success]
end

def test_sets_flash_message_can_be_set_to_now_only_on_failure
post :create, :flash_now => :on_failure
post :create, :params => { :flash_now => :on_failure }
assert_not_flash_now :success
end

Expand All @@ -143,7 +164,7 @@ def test_sets_message_based_on_notice_key_with_custom_keys
end

def test_sets_message_based_on_alert_key_with_custom_keys
post :another, :fail => true
post :another, :params => { :fail => true }
assert_equal "Warning, warning!", flash[:failure]
end

Expand All @@ -155,7 +176,7 @@ def test_sets_message_based_on_notice_key

def test_sets_message_based_on_alert_key
Responders::FlashResponder.flash_keys = [ :notice, :alert ]
post :another, :fail => true
post :another, :params => { :fail => true }
assert_equal "Warning, warning!", flash[:alert]
end

Expand All @@ -165,7 +186,7 @@ def test_sets_html_using_controller_scope
end

def test_sets_html_using_actions_scope
post :with_html, :fail => true
post :with_html, :params => { :fail => true }
assert_equal "<strong>OH NOES!</strong> You did it wrong!", flash[:failure]
end

Expand All @@ -176,18 +197,18 @@ def test_escapes_html_interpolations
end

def test_sets_flash_now_on_failure_by_default
post :another, :fail => true
post :another, :params => { :fail => true }
assert_flash_now :failure
end

def test_does_not_set_flash_message_to_now_with_errors_and_redirect
delete :with_html, :fail => true
delete :with_html, :params => { :fail => true }
assert_not_flash_now :failure
assert_equal "<strong>OH NOES!</strong> You did it wrong!", flash[:failure]
end

def test_never_set_flash_now
post :flexible, :fail => true, :responder_options => { :flash_now => false, :alert => "Warning" }
post :flexible, :params => { :fail => true, :responder_options => { :flash_now => false, :alert => "Warning" } }
assert_not_flash_now :failure
end

Expand Down

0 comments on commit f62fa08

Please sign in to comment.