Skip to content

Commit

Permalink
react_component should work outside of views (#636)
Browse files Browse the repository at this point in the history
The rails_context method previously assumed that the controller
and request view helper methods were present. This commit checks
that these methods are present before they're called.

Resolves: #634
  • Loading branch information
jtibbertsma committed Dec 6, 2016
1 parent 71dc61a commit 785ebac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/helpers/react_on_rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def rails_context(server_side:)
i18nLocale: I18n.locale,
i18nDefaultLocale: I18n.default_locale
}
if request.present?
if defined?(request) && request.present?
# Check for encoding of the request's original_url and try to force-encoding the
# URLs as UTF-8. This situation can occur in browsers that do not encode the
# entire URL as UTF-8 already, mostly on the Windows platform (IE11 and lower).
Expand Down Expand Up @@ -402,7 +402,7 @@ def send_tag_method(tag_method_name, args)
end

def in_mailer?
return false unless controller.present?
return false unless defined?(controller)
return false unless defined?(ActionMailer::Base)

controller.is_a?(ActionMailer::Base)
Expand Down
19 changes: 19 additions & 0 deletions spec/dummy/spec/helpers/react_on_rails_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,23 @@
it { is_expected.to be_an_instance_of ActiveSupport::SafeBuffer }
it { is_expected.to eq hello_world }
end

describe "#rails_context" do
before do
@rendering_extension = ReactOnRails.configuration.rendering_extension
ReactOnRails.configuration.rendering_extension = nil
end

it "should not throw an error if not in a view" do
class PlainClass
include ReactOnRailsHelper
end

ob = PlainClass.new
expect { ob.send(:rails_context, server_side: true) }.to_not raise_error
expect { ob.send(:rails_context, server_side: false) }.to_not raise_error
end

after { ReactOnRails.configuration.rendering_extension = @rendering_extension }
end
end

0 comments on commit 785ebac

Please sign in to comment.