Skip to content

Commit

Permalink
Merge branch 'master' into feature/node
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeuler committed Apr 21, 2016
2 parents e0775b7 + bf834bc commit 7c3c314
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 46 deletions.
1 change: 1 addition & 0 deletions PROJECTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* **[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/).
* **[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!
* **[Confident Financial Solutions](https://www.mycfsapp.com/)**: Auto Repair Financing to help people get back on the road and back to life.
* **[Apprentus](https://www.apprentus.com/)**: A marketplace to find the best private teachers. Using react-on-rails from the homepage to infinity!

--------

Expand Down
58 changes: 13 additions & 45 deletions app/helpers/react_on_rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,7 @@ def env_stylesheet_link_tag(args = {})
# raise_on_prerender_error: <true/false> Default to false. True will raise exception on server
# if the JS code throws
# Any other options are passed to the content tag, including the id.
def react_component(component_name,
props: {},
prerender: nil,
trace: nil,
replay_console: nil,
raise_on_prerender_error: nil,
id: nil,
html_options: {})
def react_component(component_name, raw_options = {})
# Create the JavaScript and HTML to allow either client or server rendering of the
# react_component.
#
Expand All @@ -104,44 +97,31 @@ def react_component(component_name,
# We use this react_component_index in case we have the same component multiple times on the page.

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

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

props = {} if props.nil?

prerender = prerender_option(prerender)
trace = trace_option(trace)
replay_console = replay_console_option(replay_console)
raise_on_prerender_error = raise_on_prerender_error_option(raise_on_prerender_error)

data = {
component_name: react_component_name,
props: props,
trace: trace,
dom_id: dom_id
}

component_specification_tag =
content_tag(:div,
"",
class: "js-react-on-rails-component",
style: ReactOnRails.configuration.skip_display_none ? nil : "display:none",
data: data)
style: options.style,
data: options.data)

# Create the HTML rendering part
result = server_rendered_react_component_html(props, react_component_name, dom_id,
prerender: prerender,
trace: trace,
raise_on_prerender_error: raise_on_prerender_error)
result = server_rendered_react_component_html(options.props, options.name, options.dom_id,
prerender: options.prerender,
trace: options.trace,
raise_on_prerender_error: options.raise_on_prerender_error)

server_rendered_html = result["html"]
console_script = result["consoleReplayScript"]

content_tag_options = html_options
content_tag_options[:id] = dom_id
content_tag_options = options.html_options
content_tag_options[:id] = options.dom_id

rendered_output = content_tag(:div,
server_rendered_html.html_safe,
Expand All @@ -151,7 +131,7 @@ def react_component(component_name,
result = <<-HTML.html_safe
#{component_specification_tag}
#{rendered_output}
#{replay_console ? console_script : ''}
#{options.replay_console ? console_script : ''}
HTML

prepend_render_rails_context(result)
Expand Down Expand Up @@ -387,18 +367,6 @@ def rails_context(server_side:)
@rails_context.merge(serverSide: server_side)
end

def raise_on_prerender_error_option(val)
val.nil? ? ReactOnRails.configuration.raise_on_prerender_error : val
end

def trace_option(val)
val.nil? ? ReactOnRails.configuration.trace : val
end

def prerender_option(val)
val.nil? ? ReactOnRails.configuration.prerender : val
end

def replay_console_option(val)
val.nil? ? ReactOnRails.configuration.replay_console : val
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<%- if options.server_rendering? -%>
"build:production:server": "(cd client && npm run build:production:server --silent)",
<%- end -%>
"build:client": "(cd client && npm run build:client --silent",
"build:client": "(cd client && npm run build:client --silent)",
<%- if options.server_rendering? -%>
"build:server": "(cd client && npm run build:server --silent)",
<%- end -%>
Expand Down
1 change: 1 addition & 0 deletions lib/react_on_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "react_on_rails/configuration"
require "react_on_rails/server_rendering_pool"
require "react_on_rails/engine"
require "react_on_rails/react_component/options"
require "react_on_rails/version_syntax_converter"
require "react_on_rails/test_helper"
require "react_on_rails/git_utils"
Expand Down
76 changes: 76 additions & 0 deletions lib/react_on_rails/react_component/options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module ReactOnRails
module ReactComponent
class Options
NO_PROPS = {}.freeze
HIDDEN = "display:none".freeze

attr_reader :index

def initialize(name:, index:, options:)
@name = name
@index = index
@options = options
end

def props
options.fetch(:props) { NO_PROPS }
end

def name
@name.camelize
end

def dom_id
options.fetch(:id) { default_dom_id }
end

def html_options
options[:html_options].to_h
end

def prerender
retrieve_key(:prerender)
end

def trace
retrieve_key(:trace)
end

def replay_console
retrieve_key(:replay_console)
end

def raise_on_prerender_error
retrieve_key(:raise_on_prerender_error)
end

def data
{
component_name: name,
props: props,
trace: trace,
dom_id: dom_id
}
end

def style
return nil if ReactOnRails.configuration.skip_display_none
HIDDEN
end

private

attr_reader :options

def default_dom_id
"#{@name}-react-component-#{@index}"
end

def retrieve_key(key)
options.fetch(key) do
ReactOnRails.configuration.public_send(key)
end
end
end
end
end
Loading

0 comments on commit 7c3c314

Please sign in to comment.