Skip to content

Commit

Permalink
Use Module.prepend when possible
Browse files Browse the repository at this point in the history
This change allows both patches in this gem to utilize Ruby 2's
`Module.prepend` instead of ActiveSupport's `alias_method_chain`
when possible.

Normally I would drop support of Ruby < 2 and just
rely on prepend, but as this gem is largely for legacy purposes
this now allows for both methods depending on the version of Ruby used.

This removes current deprecation warnings with Rails 5 and will allow
continued use of the gem once alias_method_chain is removed from
ActiveSupport.
  • Loading branch information
chrisarcand committed Sep 27, 2016
1 parent 17772e4 commit 0b45627
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
22 changes: 16 additions & 6 deletions lib/jquery-rjs/rendering.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
require 'action_view/helpers/rendering_helper'

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

alias_method_chain :render, :update
end
end


if RUBY_VERSION < '2'
ActionView::Helpers::RenderingHelper.module_eval do
include JqueryRjs::RenderingHelper
alias_method_chain :render, :update
end
else
ActionView::Helpers::RenderingHelper.prepend(JqueryRjs::RenderingHelper)
end
26 changes: 20 additions & 6 deletions lib/jquery-rjs/selector_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# Under MIT and/or CC By license.
#++

(defined?(ActionDispatch::Assertions::SelectorAssertions) ? ActionDispatch::Assertions::SelectorAssertions : Rails::Dom::Testing::Assertions::SelectorAssertions).module_eval do
module JqueryRjs::SelectorAssertions
# Selects content from the RJS response.
#
# === Narrowing down
Expand Down Expand Up @@ -158,9 +158,9 @@ def assert_select_rjs(*args, &block)
flunk args.shift || flunk_message
end
end

protected

def jquery_id(id) #:nodoc:
id.to_s.count('#.*,>+~:[/ ') == 0 ? "##{id}" : id
end
Expand Down 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.
def response_from_page_with_rjs
define_method("response_from_page#{'_with_rjs' if RUBY_VERSION < '2'}") do
content_type = @response.content_type

if content_type && Mime[:js] =~ content_type
Expand All @@ -216,10 +216,9 @@ def response_from_page_with_rjs

root
else
response_from_page_without_rjs
RUBY_VERSION < '2' ? response_from_page_without_rjs : super()
end
end
alias_method_chain :response_from_page, :rjs

# Unescapes a RJS string.
def unescape_rjs(rjs_string)
Expand All @@ -234,3 +233,18 @@ def unescape_rjs(rjs_string)
unescaped
end
end

module_to_patch = if defined?(ActionDispatch::Assertions::SelectorAssertions)
ActionDispatch::Assertions::SelectorAssertions
else
Rails::Dom::Testing::Assertions::SelectorAssertions
end

if RUBY_VERSION < '2'
module_to_patch.module_eval do
include JqueryRjs::SelectorAssertions
alias_method_chain :response_from_page, :rjs
end
else
module_to_patch.prepend(JqueryRjs::SelectorAssertions)
end

0 comments on commit 0b45627

Please sign in to comment.