Skip to content

Commit

Permalink
Merge pull request #108 from shakacode/application-js-fix
Browse files Browse the repository at this point in the history
Have generator create application.js file if not found
  • Loading branch information
robwise committed Nov 23, 2015
2 parents 56870c0 + f8999d0 commit c21012a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
11 changes: 6 additions & 5 deletions lib/generators/react_on_rails/base_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ def update_application_js
// bootstrap-sprockets depends on generated/vendor-bundle for jQuery.
//= require bootstrap-sprockets
DATA

application_js_path = "app/assets/javascripts/application.js"
application_js = dest_file_exists?(application_js_path) || dest_file_exists?(application_js_path + ".coffee")
if application_js
prepend_to_file(application_js, data)
app_js_path = "app/assets/javascripts/application.js"
found_app_js = dest_file_exists?(app_js_path) || dest_file_exists?(app_js_path + ".coffee")
if found_app_js
prepend_to_file(found_app_js, data)
else
puts_setup_file_error("#{application_js} or #{application_js}.coffee", data)
create_file(app_js_path, data)
end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/react_on_rails/generators/install_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,9 @@
before(:all) { run_generator_test_with_args(%w(-H)) }
include_examples "heroku_deployment"
end

context "without existing application.js or application.js.coffee file" do
before(:all) { run_generator_test_with_args([], application_js: false) }
include_examples "base_generator:base"
end
end
22 changes: 13 additions & 9 deletions spec/react_on_rails/support/generator_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ def run_generator_test_with_args(args, options = {})
simulate_existing_file("config/routes.rb", "Rails.application.routes.draw do\nend\n")
simulate_existing_file("config/application.rb", "module Gentest\nclass Application < Rails::Application\nend\nend)")
simulate_existing_file("config/initializers/assets.rb")
app_js_data = <<-DATA.strip_heredoc
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
DATA
simulate_existing_file("app/assets/javascripts/application.js", app_js_data)
application_css = "app/assets/stylesheets/application.css.scss"
if options.fetch(:application_js, true)
app_js = "app/assets/javascripts/application.js"
app_js_data = <<-DATA.strip_heredoc
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
DATA
simulate_existing_file(app_js, app_js_data)
end
if options.fetch(:application_css, true)
simulate_existing_file(application_css, " *= require_tree .\n *= require_self\n")
app_css = "app/assets/stylesheets/application.css.scss"
app_css_data = " *= require_tree .\n *= require_self\n"
simulate_existing_file(app_css, app_css_data)
end
run_generator(args)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
// bootstrap-sprockets depends on generated/vendor-bundle for jQuery.
//= require bootstrap-sprockets
//= require turbolinks
MATCH
assert_file("app/assets/javascripts/application.js", match)
assert_file("app/assets/javascripts/application.js") do |contents|
assert_match(match, contents)
end
end

it "removes incompatible sprockets require statements" do
Expand Down

0 comments on commit c21012a

Please sign in to comment.