Skip to content

Commit

Permalink
Merge pull request #509 from eacaps/feature/no-request-context
Browse files Browse the repository at this point in the history
Feature/no request context
  • Loading branch information
justin808 authored Aug 8, 2016
2 parents 4cc17e6 + 60afe39 commit c1a67f0
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Contributors: please follow the recommendations outlined at [keepachangelog.com]

## [Unreleased]

- React on Rails server rendering now supports contexts outside of browser rendering, such as ActionMailer templates [#486](https://github.com/shakacode/react_on_rails/pull/486) by [eacaps](https://github.com/eacaps)
- React on Rails now correctly parses single-digit version strings from package.json [#491](https://github.com/shakacode/react_on_rails/pull/491)

## [6.0.5]
Expand Down
38 changes: 21 additions & 17 deletions app/helpers/react_on_rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,29 +341,33 @@ def initialize_redux_stores

# This is the definitive list of the default values used for the rails_context, which is the
# second parameter passed to both component and store generator functions.
# rubocop:disable Metrics/AbcSize
def rails_context(server_side:)
@rails_context ||= begin
# Using Addressable instead of standard URI to better deal with
# non-ASCII characters (see https://github.com/shakacode/react_on_rails/pull/405)
uri = Addressable::URI.parse(request.original_url)
# uri = Addressable::URI.parse("http://foo.com:3000/posts?id=30&limit=5#time=1305298413")

result = {
# URL settings
href: request.original_url,
location: "#{uri.path}#{uri.query.present? ? "?#{uri.query}" : ''}",
scheme: uri.scheme, # http
host: uri.host, # foo.com
port: uri.port,
pathname: uri.path, # /posts
search: uri.query, # id=30&limit=5

inMailer: controller.present? && controller.is_a?(ActionMailer::Base),
# Locale settings
i18nLocale: I18n.locale,
i18nDefaultLocale: I18n.default_locale,
httpAcceptLanguage: request.env["HTTP_ACCEPT_LANGUAGE"]
i18nDefaultLocale: I18n.default_locale
}

if request.present?
# Using Addressable instead of standard URI to better deal with
# non-ASCII characters (see https://github.com/shakacode/react_on_rails/pull/405)
uri = Addressable::URI.parse(request.original_url)
# uri = Addressable::URI.parse("http://foo.com:3000/posts?id=30&limit=5#time=1305298413")

result.merge!(
# URL settings
href: request.original_url,
location: "#{uri.path}#{uri.query.present? ? "?#{uri.query}" : ''}",
scheme: uri.scheme, # http
host: uri.host, # foo.com
port: uri.port,
pathname: uri.path, # /posts
search: uri.query, # id=30&limit=5
httpAcceptLanguage: request.env["HTTP_ACCEPT_LANGUAGE"]
)
end
if ReactOnRails.configuration.rendering_extension
custom_context = ReactOnRails.configuration.rendering_extension.custom_context(self)
result.merge!(custom_context) if custom_context
Expand Down
8 changes: 8 additions & 0 deletions spec/dummy/app/mailers/dummy_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class DummyMailer < ActionMailer::Base
add_template_helper(ReactOnRailsHelper)
default from: "nobody@nope.com"

def hello_email
mail(to: "otherperson@nope.com", subject: "you've got mail")
end
end
7 changes: 7 additions & 0 deletions spec/dummy/app/views/dummy_mailer/hello_email.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Someone emailed this to you:

<%= react_component("HelloWorld", props: {
helloWorldData: {
name: "Mr. Mailing Server Side Rendering"
}
}, prerender: true) %>
10 changes: 7 additions & 3 deletions spec/dummy/config/initializers/react_on_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ module RenderingExtension
# Return a Hash that contains custom values from the view context that will get passed to
# all calls to react_component and redux_store for rendering
def self.custom_context(view_context)
{
somethingUseful: view_context.session[:something_useful]
}
if view_context.controller.is_a?(ActionMailer::Base)
{}
else
{
somethingUseful: view_context.session[:something_useful]
}
end
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/dummy/spec/requests/server_render_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@
end
end

describe "server render mailer" do
it "sends email okay" do
mail = DummyMailer.hello_email
expect(mail.subject).to match "mail"
expect(mail.body).to match "Mr. Mailing Server Side Rendering"
expect(mail.body).to match "inMailer&quot;:true"
end

it "sets inMailer properly" do
get client_side_hello_world_path
html_nodes = Nokogiri::HTML(response.body)
expect(html_nodes.css("div#js-react-on-rails-context").attr("data-rails-context").value)
.to match('inMailer\":false')
end
end

describe "server rendering railsContext" do
let(:http_accept_language) { "en-US,en;q=0.8" }

Expand Down

0 comments on commit c1a67f0

Please sign in to comment.