react_on_rails icon indicating copy to clipboard operation
react_on_rails copied to clipboard

Integration with Rails 5 Engines

Open abhinavmsra opened this issue 10 years ago • 16 comments

I am trying to integrate the gem in a Rails Engine, but for some reason it seems to fail giving the following error:

/Users/abhinav/.rvm/gems/ruby-2.3.1@travelbook/gems/react_on_rails-6.1.1/lib/generators/react_on_rails/install_generator.rb:63:in `installation_prerequisites_met?': uninitialized constant ReactOnRails::GitUtils (NameError)
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@travelbook/gems/react_on_rails-6.1.1/lib/generators/react_on_rails/install_generator.rb:34:in `run_generators'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `block in invoke_all'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `each'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `map'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `invoke_all'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/thor-0.19.1/lib/thor/group.rb:232:in `dispatch'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/railties-5.0.0.1/lib/rails/generators.rb:180:in `invoke'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/railties-5.0.0.1/lib/rails/commands/generate.rb:13:in `<top (required)>'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/railties-5.0.0.1/lib/rails/engine/commands_tasks.rb:68:in `require'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/railties-5.0.0.1/lib/rails/engine/commands_tasks.rb:68:in `require_command!'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/railties-5.0.0.1/lib/rails/engine/commands_tasks.rb:73:in `generate_or_destroy'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/railties-5.0.0.1/lib/rails/engine/commands_tasks.rb:44:in `generate'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/railties-5.0.0.1/lib/rails/engine/commands_tasks.rb:37:in `run_command!'
        from /Users/abhinav/.rvm/gems/ruby-2.3.1@global/gems/railties-5.0.0.1/lib/rails/engine/commands.rb:14:in `<top (required)>'
        from bin/rails:13:in `require'
        from bin/rails:13:in `<main>'

However I can integrate the gem in the core app without any errors. Only when i move the gem to the engine, I get the GitUtils error.

For now my .gemspec file is rather simple and has dependencies defined as follows:

s.add_dependency "rails", "~> 5.0.0", ">= 5.0.0.1"
s.add_dependency 'react_on_rails', '~>6'

Is there something that I am missing in the installation?

abhinavmsra avatar Oct 10 '16 08:10 abhinavmsra

I got it to work by updating the

react_on_rails-6.1.1/lib/generators/react_on_rails/install_generator.rb

by manually requiring the GItUtils module.

...
require "react_on_rails/git_utils.rb"

module ReactOnRails
  include GitUtils
  module Generators
    class InstallGenerator < Rails::Generators::Base
      include GeneratorHelper

      # fetch USAGE file for details generator description
      source_root(File.expand_path("../", __FILE__))

...

But still it doesn't feel like a right way to do.

abhinavmsra avatar Oct 10 '16 11:10 abhinavmsra

@abhinavmsra Thanks for researching this. It would be great if you can submit a documentation PR (and any associated code PR).

justin808 avatar Oct 10 '16 22:10 justin808

@justin808 I still have some issues. Trying to fix them and then will share my git repo.

abhinavmsra avatar Oct 11 '16 00:10 abhinavmsra

@justin808 I still have issues with asset pipeline in particular the js, css from the react engine doesnt get loaded. Do you have some guides for that?

abhinavmsra avatar Oct 11 '16 05:10 abhinavmsra

@abhinavmsra We generally use webpack for all the js and css creation, and just create one file for each that gets used by the asset pipeline.

justin808 avatar Oct 11 '16 06:10 justin808

Seeing this same issue. Manually requiring the module worked for the generator; but now I'm getting a failure in my engine.rb when I try to run the app that includes this engine

/apps/<appname>/config/initializers/react_on_rails.rb:2:in `<top (required)>': uninitialized constant ReactOnRails (NameError)
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/engine.rb:648:in `block in load_config_initializer'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/activesupport-4.1.14.2/lib/active_support/notifications.rb:161:in `instrument'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/engine.rb:647:in `load_config_initializer'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/engine.rb:612:in `block (2 levels) in <class:Engine>'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/engine.rb:611:in `each'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/engine.rb:611:in `block in <class:Engine>'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/initializable.rb:30:in `run'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/noah.prince/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
    from /Users/noah.prince/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /Users/noah.prince/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:420:in `block (2 levels) in each_strongly_connected_component_from'
    from /Users/noah.prince/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
    from /Users/noah.prince/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:419:in `block in each_strongly_connected_component_from'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/initializable.rb:44:in `each'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/initializable.rb:44:in `tsort_each_child'
    from /Users/noah.prince/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:413:in `call'
    from /Users/noah.prince/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:413:in `each_strongly_connected_component_from'
    from /Users/noah.prince/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /Users/noah.prince/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:345:in `each'
    from /Users/noah.prince/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:345:in `call'
    from /Users/noah.prince/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
    from /Users/noah.prince/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
    from /Users/noah.prince/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/application.rb:300:in `initialize!'
    from /apps/project_ouroboros/config/environment.rb:10:in `<top (required)>'
    from /apps/project_ouroboros/config.ru:3:in `block in <main>'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/rack-1.5.5/lib/rack/builder.rb:55:in `instance_eval'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/rack-1.5.5/lib/rack/builder.rb:55:in `initialize'
    from /apps/project_ouroboros/config.ru:in `new'
    from /apps/project_ouroboros/config.ru:in `<main>'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/rack-1.5.5/lib/rack/builder.rb:49:in `eval'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/rack-1.5.5/lib/rack/builder.rb:49:in `new_from_string'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/rack-1.5.5/lib/rack/builder.rb:40:in `parse_file'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/rack-1.5.5/lib/rack/server.rb:277:in `build_app_and_options_from_config'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/rack-1.5.5/lib/rack/server.rb:199:in `app'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/commands/server.rb:50:in `app'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/rack-1.5.5/lib/rack/server.rb:314:in `wrapped_app'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/commands/server.rb:130:in `log_to_stdout'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/commands/server.rb:67:in `start'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/commands/commands_tasks.rb:81:in `block in server'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/commands/commands_tasks.rb:76:in `tap'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/commands/commands_tasks.rb:76:in `server'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /Users/noah.prince/.rvm/gems/ruby-2.2.4/gems/railties-4.1.14.2/lib/rails/commands.rb:17:in `<top (required)>'
    from /apps/project_ouroboros/bin/rails:4:in `require'
    from /apps/project_ouroboros/bin/rails:4:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

noahprince22 avatar Oct 18 '16 21:10 noahprince22

It appears that the above error is remedied by including the gem in the parent project; but then more errors ensue. Is there information anywhere on using this gem with engines? We're on Rails 4 and still experiencing this issue. The whole purpose of this engine is to be a pilot/sandbox/green pasture for React on Rails, so I don't want to touch the main project; but it's seeming like that is going to be unavoidable.

noahprince22 avatar Oct 18 '16 21:10 noahprince22

I found this issue which relates to the errors I got after including the gem in the parent project. Adding the following:

ActiveSupport.on_load(:action_view) do
  include ReactOnRailsHelper
end

to an initializer on the parent project allows the react_component helper to work. While this is an okay workaround, I'd rather everything just work without touching the parent application.

noahprince22 avatar Oct 18 '16 22:10 noahprince22

For anyone who stumbles upon this issue, I found a solution:

In your engine

At the top of config/initializers/react_on_rails.rb

 ActiveSupport.on_load(:action_view) do
   include ReactOnRailsHelper
 end

In your <engine_name>.gemspec:

   s.add_dependency 'react_on_rails', '~> 6'

In your lib/<engine_name>.rb (the entry point for your engine)

 require "react_on_rails"

In your lib/tasks/<engine_name>_tasks.rake:

 Rake.application.remove_task('react_on_rails:assets:compile_environment')

 task 'react_on_rails:assets:compile_environment' do
   path = File.join(YourEngineName::Engine.root, 'client')
   sh "cd #{path} && #{ReactOnRails.configuration.npm_build_production_command}"
 end

In the project including your engine

Place gem 'react_on_rails', '~> 6' before the gem pointing at your engine in your gemfile.

This is necessary because React on Rails attaches itself to the rake assets:precompile task. It then uses a direct path to cd into client, which will not exist in the main app that includes your engine. Since you'll always be precompiling assets in the parent app, this will always fail. The workaround then, is to remove the task and replace it with one that goes into your Engine's root. The reason you have to include the react on rails gem before your engine is so that the react_on_rails:assets:compile_environment task is defined by the time your engine gets loaded to remove it.

Requiring react_on_rails and including the helper will get rid of any issues where react on rails or react_component is undefined.

As far as solving the assets issue, lib/tasks/assets.rake in react_on_rails would somehow have to know that react_on_rails was included in an engine, and decide the path accordingly. This might be impossible, especially in the case of multiple engines using react_on_rails in a single application. Another solution would be to detach this rake task from the rails assets:precompile task, and let people use it separately. For now, I'm just going to remove the task and then add on to it in every engine that uses react_on_rails. @justin808, thoughts on a more permanent fix?

noahprince22 avatar Nov 02 '16 15:11 noahprince22

Just in case it might be helpful I want to mention the wiki page that I created to gather all engine-related issues.

aCandidMind avatar Nov 07 '16 14:11 aCandidMind

@aCandidMind @noahprince22 Could either of you submit a PR with a doc page called rails-engines, placed in this directory: https://github.com/shakacode/react_on_rails/tree/master/docs/additional-reading

and linked from the home page?

justin808 avatar Nov 16 '16 08:11 justin808

CC: @Judahmeek

justin808 avatar Nov 16 '16 08:11 justin808

@Judahmeek can we close this issue?

justin808 avatar Nov 26 '16 23:11 justin808

As far as I'm concerned: yes.

Judahmeek avatar Dec 01 '16 03:12 Judahmeek

Hello,

There is one slight problem with this approach - the file generated is inside gems public folder and then the application cannot find it - unless we modify somehow webpack paths.

I've added simple copy to get the files into Rails.root public folder but not sure if this is a best approach

rhryniow avatar Jun 27 '17 18:06 rhryniow

For anyone who stumbles upon this issue, I found a solution:

In your engine

At the top of config/initializers/react_on_rails.rb

 ActiveSupport.on_load(:action_view) do
   include ReactOnRailsHelper
 end

In your <engine_name>.gemspec:

   s.add_dependency 'react_on_rails', '~> 6'

In your lib/<engine_name>.rb (the entry point for your engine)

 require "react_on_rails"

In your lib/tasks/<engine_name>_tasks.rake:

 Rake.application.remove_task('react_on_rails:assets:compile_environment')

 task 'react_on_rails:assets:compile_environment' do
   path = File.join(YourEngineName::Engine.root, 'client')
   sh "cd #{path} && #{ReactOnRails.configuration.npm_build_production_command}"
 end

In the project including your engine

Place gem 'react_on_rails', '~> 6' before the gem pointing at your engine in your gemfile.

This is necessary because React on Rails attaches itself to the rake assets:precompile task. It then uses a direct path to cd into client, which will not exist in the main app that includes your engine. Since you'll always be precompiling assets in the parent app, this will always fail. The workaround then, is to remove the task and replace it with one that goes into your Engine's root. The reason you have to include the react on rails gem before your engine is so that the react_on_rails:assets:compile_environment task is defined by the time your engine gets loaded to remove it.

Requiring react_on_rails and including the helper will get rid of any issues where react on rails or react_component is undefined.

As far as solving the assets issue, lib/tasks/assets.rake in react_on_rails would somehow have to know that react_on_rails was included in an engine, and decide the path accordingly. This might be impossible, especially in the case of multiple engines using react_on_rails in a single application. Another solution would be to detach this rake task from the rails assets:precompile task, and let people use it separately. For now, I'm just going to remove the task and then add on to it in every engine that uses react_on_rails. @justin808, thoughts on a more permanent fix?

I am trying to follow all this procedure but not able to start react within rails engine. Do you have any example where should I refer the changes.

vyavahare-kishor avatar Aug 13 '19 11:08 vyavahare-kishor