Skip to content

Commit 7c3c314

Browse files
committed
Merge branch 'master' into feature/node
2 parents e0775b7 + bf834bc commit 7c3c314

File tree

6 files changed

+290
-46
lines changed

6 files changed

+290
-46
lines changed

PROJECTS.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* **[Madrone Analytics](http://madroneco.com/)**: The original client of [ShakaCode](http://www.shakacode.com) that led to the development of React on Rails, as described in [Fast Rich Client Rails Development With Webpack and the ES6 Transpiler](http://www.railsonmaui.com/blog/2014/10/03/integrating-webpack-and-the-es6-transpiler-into-an-existing-rails-project/).
1010
* **[Pivotal Tracker](http://www.pivotaltracker.com/)**: The first (and most-loved) agile project management tool built on Rails. React on Rails has greatly simplified integration and workflow for our React components in Rails!
1111
* **[Confident Financial Solutions](https://www.mycfsapp.com/)**: Auto Repair Financing to help people get back on the road and back to life.
12+
* **[Apprentus](https://www.apprentus.com/)**: A marketplace to find the best private teachers. Using react-on-rails from the homepage to infinity!
1213

1314
--------
1415

app/helpers/react_on_rails_helper.rb

+13-45
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,7 @@ def env_stylesheet_link_tag(args = {})
8787
# raise_on_prerender_error: <true/false> Default to false. True will raise exception on server
8888
# if the JS code throws
8989
# Any other options are passed to the content tag, including the id.
90-
def react_component(component_name,
91-
props: {},
92-
prerender: nil,
93-
trace: nil,
94-
replay_console: nil,
95-
raise_on_prerender_error: nil,
96-
id: nil,
97-
html_options: {})
90+
def react_component(component_name, raw_options = {})
9891
# Create the JavaScript and HTML to allow either client or server rendering of the
9992
# react_component.
10093
#
@@ -104,44 +97,31 @@ def react_component(component_name,
10497
# We use this react_component_index in case we have the same component multiple times on the page.
10598

10699
react_component_index = next_react_component_index
107-
react_component_name = component_name.camelize # Not sure if we should be doing this (JG)
108-
dom_id = id.presence || "#{component_name}-react-component-#{react_component_index}"
100+
options = ReactOnRails::ReactComponent::Options.new(name: component_name,
101+
index: react_component_index,
102+
options: raw_options)
109103

110104
# Setup the page_loaded_js, which is the same regardless of prerendering or not!
111105
# The reason is that React is smart about not doing extra work if the server rendering did its job.
112106

113-
props = {} if props.nil?
114-
115-
prerender = prerender_option(prerender)
116-
trace = trace_option(trace)
117-
replay_console = replay_console_option(replay_console)
118-
raise_on_prerender_error = raise_on_prerender_error_option(raise_on_prerender_error)
119-
120-
data = {
121-
component_name: react_component_name,
122-
props: props,
123-
trace: trace,
124-
dom_id: dom_id
125-
}
126-
127107
component_specification_tag =
128108
content_tag(:div,
129109
"",
130110
class: "js-react-on-rails-component",
131-
style: ReactOnRails.configuration.skip_display_none ? nil : "display:none",
132-
data: data)
111+
style: options.style,
112+
data: options.data)
133113

134114
# Create the HTML rendering part
135-
result = server_rendered_react_component_html(props, react_component_name, dom_id,
136-
prerender: prerender,
137-
trace: trace,
138-
raise_on_prerender_error: raise_on_prerender_error)
115+
result = server_rendered_react_component_html(options.props, options.name, options.dom_id,
116+
prerender: options.prerender,
117+
trace: options.trace,
118+
raise_on_prerender_error: options.raise_on_prerender_error)
139119

140120
server_rendered_html = result["html"]
141121
console_script = result["consoleReplayScript"]
142122

143-
content_tag_options = html_options
144-
content_tag_options[:id] = dom_id
123+
content_tag_options = options.html_options
124+
content_tag_options[:id] = options.dom_id
145125

146126
rendered_output = content_tag(:div,
147127
server_rendered_html.html_safe,
@@ -151,7 +131,7 @@ def react_component(component_name,
151131
result = <<-HTML.html_safe
152132
#{component_specification_tag}
153133
#{rendered_output}
154-
#{replay_console ? console_script : ''}
134+
#{options.replay_console ? console_script : ''}
155135
HTML
156136

157137
prepend_render_rails_context(result)
@@ -387,18 +367,6 @@ def rails_context(server_side:)
387367
@rails_context.merge(serverSide: server_side)
388368
end
389369

390-
def raise_on_prerender_error_option(val)
391-
val.nil? ? ReactOnRails.configuration.raise_on_prerender_error : val
392-
end
393-
394-
def trace_option(val)
395-
val.nil? ? ReactOnRails.configuration.trace : val
396-
end
397-
398-
def prerender_option(val)
399-
val.nil? ? ReactOnRails.configuration.prerender : val
400-
end
401-
402370
def replay_console_option(val)
403371
val.nil? ? ReactOnRails.configuration.replay_console : val
404372
end

lib/generators/react_on_rails/templates/base/base/package.json.tt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<%- if options.server_rendering? -%>
1313
"build:production:server": "(cd client && npm run build:production:server --silent)",
1414
<%- end -%>
15-
"build:client": "(cd client && npm run build:client --silent",
15+
"build:client": "(cd client && npm run build:client --silent)",
1616
<%- if options.server_rendering? -%>
1717
"build:server": "(cd client && npm run build:server --silent)",
1818
<%- end -%>

lib/react_on_rails.rb

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require "react_on_rails/configuration"
77
require "react_on_rails/server_rendering_pool"
88
require "react_on_rails/engine"
9+
require "react_on_rails/react_component/options"
910
require "react_on_rails/version_syntax_converter"
1011
require "react_on_rails/test_helper"
1112
require "react_on_rails/git_utils"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
module ReactOnRails
2+
module ReactComponent
3+
class Options
4+
NO_PROPS = {}.freeze
5+
HIDDEN = "display:none".freeze
6+
7+
attr_reader :index
8+
9+
def initialize(name:, index:, options:)
10+
@name = name
11+
@index = index
12+
@options = options
13+
end
14+
15+
def props
16+
options.fetch(:props) { NO_PROPS }
17+
end
18+
19+
def name
20+
@name.camelize
21+
end
22+
23+
def dom_id
24+
options.fetch(:id) { default_dom_id }
25+
end
26+
27+
def html_options
28+
options[:html_options].to_h
29+
end
30+
31+
def prerender
32+
retrieve_key(:prerender)
33+
end
34+
35+
def trace
36+
retrieve_key(:trace)
37+
end
38+
39+
def replay_console
40+
retrieve_key(:replay_console)
41+
end
42+
43+
def raise_on_prerender_error
44+
retrieve_key(:raise_on_prerender_error)
45+
end
46+
47+
def data
48+
{
49+
component_name: name,
50+
props: props,
51+
trace: trace,
52+
dom_id: dom_id
53+
}
54+
end
55+
56+
def style
57+
return nil if ReactOnRails.configuration.skip_display_none
58+
HIDDEN
59+
end
60+
61+
private
62+
63+
attr_reader :options
64+
65+
def default_dom_id
66+
"#{@name}-react-component-#{@index}"
67+
end
68+
69+
def retrieve_key(key)
70+
options.fetch(key) do
71+
ReactOnRails.configuration.public_send(key)
72+
end
73+
end
74+
end
75+
end
76+
end

0 commit comments

Comments
 (0)