Skip to content

Commit

Permalink
react_component should work outside of views (#636) (#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 authored and justin808 committed Dec 6, 2016
1 parent 4763d35 commit c146ba6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ Contributors: please follow the recommendations outlined at [keepachangelog.com]

## [Unreleased]

## [6.3.1]
## [6.3.2] - 2016-12-5
##### Fixed
- The `react_component` method was raising a `NameError` when `ReactOnRailsHelper` was included in a plain object. [#636](https://github.com/shakacode/react_on_rails/pull/636) by [jtibbertsma](https://github.com/jtibbertsma).

## [6.3.1] - 2016-11-30
##### Changed
- Improved generator post-install help messages. [#631](https://github.com/shakacode/react_on_rails/pull/631) by [justin808](https://github.com/justin808).

## [6.3.0]
## [6.3.0] - 2016-11-30
##### Changed
- Modified register API to allow registration of renderers, allowing a user to manually render their app to the DOM. This allows for code splitting and deferred loading. [#581](https://github.com/shakacode/react_on_rails/pull/581) by [jtibbertsma](https://github.com/jtibbertsma).

Expand Down Expand Up @@ -397,7 +401,8 @@ Best done with Object destructing:
##### Fixed
- Fix several generator related issues.
[Unreleased]: https://github.com/shakacode/react_on_rails/compare/6.3.1...master
[Unreleased]: https://github.com/shakacode/react_on_rails/compare/6.3.2...master
[6.3.2]: https://github.com/shakacode/react_on_rails/compare/6.3.0...6.3.1
[6.3.1]: https://github.com/shakacode/react_on_rails/compare/6.3.0...6.3.1
[6.3.0]: https://github.com/shakacode/react_on_rails/compare/6.2.1...6.3.0
[6.2.1]: https://github.com/shakacode/react_on_rails/compare/6.2.0...6.2.1
Expand Down
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 c146ba6

Please sign in to comment.