Skip to content

Commit

Permalink
JqueryRjs.use_alias_method_chain?
Browse files Browse the repository at this point in the history
Cache alias_method_chain conditional to a single spot
  • Loading branch information
chrisarcand committed Sep 28, 2016
1 parent 0b45627 commit 5ed5e95
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions lib/jquery-rjs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
end

module JqueryRjs
def use_alias_method_chain?
@use_alias_method_chain ||= RUBY_VERSION < '2'
end
module_function :use_alias_method_chain?

class Engine < Rails::Engine
initializer 'jquery-rjs.initialize' do
ActiveSupport.on_load(:action_controller) do
Expand Down
6 changes: 3 additions & 3 deletions lib/jquery-rjs/rendering.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
require 'action_view/helpers/rendering_helper'

module JqueryRjs::RenderingHelper
method_name = "render#{'_with_update' if RUBY_VERSION < '2'}"
method_name = "render#{'_with_update' if JqueryRjs.use_alias_method_chain?}"
define_method(method_name) do |options = {}, locals = {}, &block|
if options == :update
update_page(&block)
else
args = options, locals, block
RUBY_VERSION < '2' ? render_without_update(*args) : super(*args)
JqueryRjs.use_alias_method_chain? ? render_without_update(*args) : super(*args)
end
end
end


if RUBY_VERSION < '2'
if JqueryRjs.use_alias_method_chain?
ActionView::Helpers::RenderingHelper.module_eval do
include JqueryRjs::RenderingHelper
alias_method_chain :render, :update
Expand Down
6 changes: 3 additions & 3 deletions lib/jquery-rjs/selector_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def response_from_page

# +assert_select+ and +css_select+ call this to obtain the content in the HTML
# page, or from all the RJS statements, depending on the type of response.
define_method("response_from_page#{'_with_rjs' if RUBY_VERSION < '2'}") do
define_method("response_from_page#{'_with_rjs' if JqueryRjs.use_alias_method_chain?}") do
content_type = @response.content_type

if content_type && Mime[:js] =~ content_type
Expand All @@ -216,7 +216,7 @@ def response_from_page

root
else
RUBY_VERSION < '2' ? response_from_page_without_rjs : super()
JqueryRjs.use_alias_method_chain? ? response_from_page_without_rjs : super()
end
end

Expand All @@ -240,7 +240,7 @@ def unescape_rjs(rjs_string)
Rails::Dom::Testing::Assertions::SelectorAssertions
end

if RUBY_VERSION < '2'
if JqueryRjs.use_alias_method_chain?
module_to_patch.module_eval do
include JqueryRjs::SelectorAssertions
alias_method_chain :response_from_page, :rjs
Expand Down

0 comments on commit 5ed5e95

Please sign in to comment.