Skip to content

Commit

Permalink
Add React.js
Browse files Browse the repository at this point in the history
Converts one simple view to React. Remaining .erb views will be converted in a
subsequent commit.

Includes a polyfill for `Function.prototype.bind` in Poltergeist/PhantomJS
(apparently it will be addressed in PhantomJS v2.0).

Source: ariya/phantomjs#10522.
  • Loading branch information
Josh Prince committed Jun 26, 2015
1 parent fa966ba commit f041098
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 19 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gem 'httparty'
gem 'jbuilder', '~> 2.0'
gem 'jquery-rails'
gem 'rails', '4.2.1'
gem 'react-rails', '~> 1.0'
gem 'sqlite3'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'turbolinks'
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ GEM
execjs
json
columnize (0.9.0)
connection_pool (2.2.0)
database_cleaner (1.2.0)
debug_inspector (0.0.2)
devise (3.4.1)
Expand Down Expand Up @@ -191,6 +192,13 @@ GEM
rake (10.4.2)
rdoc (4.2.0)
json (~> 1.4)
react-rails (1.0.0)
coffee-script-source (~> 1.8)
connection_pool
execjs
rails (>= 3.1)
react-source (~> 0.13)
react-source (0.13.3)
responders (2.1.0)
railties (>= 4.2.0, < 5)
rspec (2.14.1)
Expand Down Expand Up @@ -304,6 +312,7 @@ DEPENDENCIES
quiet_assets
rails (= 4.2.1)
rails_12factor
react-rails (~> 1.0)
rspec-instafail
rspec-rails
rubocop
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require react
//= require react_ujs
//= require components
//= require_tree .
1 change: 1 addition & 0 deletions app/assets/javascripts/components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//= require_tree ./components
11 changes: 11 additions & 0 deletions app/assets/javascripts/components/teams/index.js.jsx.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@Team = React.createClass
render: ->
`<h3>{this.props.name}</h3>`

@TeamsIndex = React.createClass
render: ->
teams = this.props.teams.map (team, i) -> `<Team key={i} name={team.name} />`
`<div>
<h2>Teams</h2>
{teams}
</div>`
5 changes: 0 additions & 5 deletions app/views/home/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,3 @@

<%= link_to('New League', new_league_path) %>
<%= link_to('View Existing Leagues', leagues_path) %>

<script type="text/javascript">
pTag = $('<p>').text('Injected via JS.')
$('body').append(pTag)
</script>
5 changes: 1 addition & 4 deletions app/views/teams/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
<h2>Teams</h2>
<% @teams.each do |team| %>
<h3><%= team.name %></h3>
<% end %>
<%= react_component('TeamsIndex', render(template: 'teams/index.json.jbuilder')) %>
3 changes: 3 additions & 0 deletions app/views/teams/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.teams(@teams) do |team|
json.name team.name
end
3 changes: 3 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@
# config.action_view.raise_on_missing_translations = true

config.action_mailer.default_url_options = { host: ENV.fetch('APP_NAME', 'localhost:3000') }

# Which React.js build (development, production, with or without add-ons) to serve
config.react.variant = :development
end
3 changes: 3 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,7 @@

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

# Which React.js build (development, production, with or without add-ons) to serve
config.react.variant = :production
end
3 changes: 3 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@
# config.action_view.raise_on_missing_translations = true

config.action_mailer.default_url_options = { host: ENV.fetch('APP_NAME', 'localhost:3000') }

# Which React.js build (development, production, with or without add-ons) to serve
config.react.variant = :development
end
1 change: 1 addition & 0 deletions lib/tasks/coffeelint.rake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ task :coffeelint do
level: 'error',
value: 99
},
no_backticks: { level: 'ignore' },
no_empty_param_list: { level: 'error' },
prefer_english_operator: { level: 'error' }
}
Expand Down
8 changes: 0 additions & 8 deletions spec/features/sample_user_visit_spec.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/features/user_creates_team_in_league_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
expect(page).to have_no_link 'New Team'
end

scenario 'can view a list of teams' do
scenario 'can view a list of teams', js: true do
@first_owner_team = create(:team, league: @league, user: @first_owner)
@second_owner = create(:user)
@second_owner_team = create(:team, league: @league, user: @second_owner)
Expand Down
5 changes: 4 additions & 1 deletion spec/support/capybara.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
require 'capybara-screenshot/rspec'

Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, timeout: 10_000)
Capybara::Poltergeist::Driver.new(
app,
extensions: ['vendor/assets/javascripts/poltergeist/bind_polyfill.js']
)
end

Capybara.javascript_driver = :poltergeist
Expand Down
27 changes: 27 additions & 0 deletions vendor/assets/javascripts/poltergeist/bind_polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// https://gist.github.com/jacomyal/4b7ae101a1cf6b985c60
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}

var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};

// test this.prototype in case of native functions binding:
if (this.prototype)
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();

return fBound;
};
}

0 comments on commit f041098

Please # to comment.