reactrb-rails-generator 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8b0bf20d67944415396dd2058f4ecca1e12e4568
4
+ data.tar.gz: 5f64d143a2c040cc454bf97eb27d7420411948bc
5
+ SHA512:
6
+ metadata.gz: aa0ad96979e6db5740a2d713b92b3637eab1ad0261fa700563d98bbd43870dbfb7a252ab2c830277b763c6c0d7c73061c23e8c1f0abe617ccbb047a41bec05a7
7
+ data.tar.gz: 8243ade5a8a86c74ded42d21e6434aca58c4d4e945a180ead79eb6c8d84cb3df32d51b50c2f518a6f8e8665555ceeb395f8e0c66419cb6770a5c5484df6929e3
@@ -0,0 +1,20 @@
1
+ require 'rails/generators'
2
+ module Reactrb
3
+ class Component < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ argument :components, type: :array
7
+ def create_component_file
8
+ self.components.each do |component|
9
+ component_array = component.split('::')
10
+ @modules = component_array[0..-2]
11
+ @file_name = component_array.last
12
+ @indet = 1
13
+ template 'component_template.rb',
14
+ File.join('app/views/components',
15
+ @modules.map(&:downcase).join('/'),
16
+ "#{@file_name.underscore}.rb")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,94 @@
1
+ require 'rails/generators'
2
+ module Reactrb
3
+ class InstallGenerator < Rails::Generators::Base
4
+ class_option :'reactive-record', type: :boolean
5
+ class_option :'opal-jquery', type: :boolean
6
+ class_option :'reactrb-router', type: :boolean
7
+ class_option :all, type: :boolean
8
+
9
+ def inject_react_file_js
10
+ inject_into_file 'app/assets/javascripts/application.js',
11
+ after: "// about supported directives.\n" do
12
+ <<-'JS'
13
+ //= require 'components'
14
+ //= require 'react_ujs'
15
+ JS
16
+ end
17
+ inject_into_file 'app/assets/javascripts/application.js',
18
+ after: "//= require jquery_ujs\n" do
19
+ <<-'JS'
20
+ Opal.load('components');
21
+ JS
22
+ end
23
+ end
24
+
25
+ def inject_engine_to_routes
26
+ if options[:'reactive-record'] || options[:all]
27
+ route 'mount ReactiveRecord::Engine => \'/rr\''
28
+ end
29
+ end
30
+
31
+ def create_components_directory
32
+ create_file 'app/views/components/.keep', ''
33
+ if options[:'reactive-record'] || options[:all]
34
+ create_file 'app/models/public/.keep', ''
35
+ end
36
+ end
37
+
38
+ def create_manifests
39
+ create_file 'app/views/components.rb', <<-FILE
40
+ # app/views/components.rb
41
+ require 'opal'
42
+ require 'react'
43
+ require 'reactrb'
44
+ if React::IsomorphicHelpers.on_opal_client?
45
+ require 'opal-jquery'
46
+ require 'browser'
47
+ require 'browser/interval'
48
+ require 'browser/delay'
49
+ # add any additional requires that can ONLY run on client here
50
+ end
51
+ #{"require 'reactrb-router'\nrequire 'react_router'" if options[:'reactive-router'] || options[:all]}
52
+ #{'require \'reactive-record\'' if options[:'reactive-record'] || options[:all]}
53
+ #{'require \'models\'' if options[:'reactive-record'] || options[:all]}
54
+ require_tree './components'
55
+ FILE
56
+
57
+ if options[:'reactive-record'] || options[:all]
58
+ create_file 'app/models/models.rb', <<-FILE
59
+ # app/models/models.rb
60
+ require_tree './public'
61
+ FILE
62
+ end
63
+ end
64
+
65
+ def add_config
66
+ application 'config.assets.paths << ::Rails.root.join(\'app\', \'models\').to_s'
67
+ application 'config.autoload_paths += %W(#{config.root}/app/views/components)'
68
+ if options[:'reactive-record'] || options[:all]
69
+ application 'config.autoload_paths += %W(#{config.root}/app/models/public)'
70
+ end
71
+ application 'config.eager_load_paths += %W(#{config.root}/app/views/components)'
72
+ if options[:'reactive-record'] || options[:all]
73
+ application 'config.eager_load_paths += %W(#{config.root}/app/models/public)'
74
+ end
75
+ application 'config.watchable_files.concat Dir["#{config.root}/app/views/**/*.rb"]',
76
+ env: :development
77
+ application 'config.react.variant = :development', env: :development
78
+ end
79
+
80
+ def add_gems
81
+ gem 'reactrb'
82
+ gem 'react-rails', '>= 1.3.0'
83
+ gem 'opal-rails', '>= 0.8.1'
84
+ gem 'therubyracer', platforms: :ruby
85
+
86
+ # optional gems
87
+ if options[:'reactrb-router'] || options[:all]
88
+ gem 'react-router-rails', '~> 0.13.3'
89
+ gem 'reactrb-router'
90
+ end
91
+ gem 'reactive-record', '>= 0.8.0' if options[:'reactive-record'] || options[:all]
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,19 @@
1
+ require 'rails/generators'
2
+ module Reactrb
3
+ class Router < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+ argument :components, type: :array
6
+ def create_component_file
7
+ self.components.each do |component|
8
+ component_array = component.split('::')
9
+ @modules = component_array[0..-2]
10
+ @file_name = component_array.last
11
+ @indet = 1
12
+ template 'router_template.rb',
13
+ File.join('app/views/components',
14
+ @modules.map(&:downcase).join('/'),
15
+ "#{@file_name.underscore}.rb")
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,41 @@
1
+ module Components
2
+ <%- @modules.each do |module_name| %><%= " "* @indet %>module <%= module_name.camelize %><%- @indet += 1 %>
3
+ <%- end %><%=" "* @indet %>class <%= @file_name %> < React::Component::Base
4
+
5
+ <%=" "* @indet %># param :my_param
6
+ <%=" "* @indet %># param param_with_default: "default value"
7
+ <%=" "* @indet %># param :param_with_default2, default: "default value" # alternative syntax
8
+ <%=" "* @indet %># param :param_with_type, type: Hash
9
+ <%=" "* @indet %># param :array_of_hashes, type: [Hash]
10
+ <%=" "* @indet %># collect_all_other_params_as :attributes #collects all other params into a hash
11
+
12
+ <%=" "* @indet %># The following are the most common lifecycle call backs,
13
+ <%=" "* @indet %># the following are the most common lifecycle call backs# delete any that you are not using.
14
+ <%=" "* @indet %># call backs may also reference an instance method i.e. before_mount :my_method
15
+
16
+ <%=" "* @indet %>before_mount do
17
+ <%=" "* @indet %> # any initialization particularly of state variables goes here.
18
+ <%=" "* @indet %> # this will execute on server (prerendering) and client.
19
+ <%=" "* @indet %>end
20
+
21
+ <%=" "* @indet %>after_mount do
22
+ <%=" "* @indet %> # any client only post rendering initialization goes here.
23
+ <%=" "* @indet %> # i.e. start timers, HTTP requests, and low level jquery operations etc.
24
+ <%=" "* @indet %>end
25
+
26
+ <%=" "* @indet %>before_update do
27
+ <%=" "* @indet %> # called whenever a component will be re-rerendered
28
+ <%=" "* @indet %>end
29
+
30
+ <%=" "* @indet %>before_unmount do
31
+ <%=" "* @indet %> # cleanup any thing (i.e. timers) before component is destroyed
32
+ <%=" "* @indet %>end
33
+
34
+ <%=" "* @indet %>def render
35
+ <%=" "* @indet %> div do
36
+ <%=" "* @indet %> "<%= (@modules+[@file_name]).join('::') %>"
37
+ <%=" "* @indet %> end
38
+ <%=" "* @indet %>end
39
+ <%=" "* @indet %>end
40
+ <%- @modules.each do %><%- @indet -= 1 %><%=" "* @indet %>end
41
+ <%- end %>end
@@ -0,0 +1,44 @@
1
+ module Components
2
+ <%- @modules.each do |module_name| %><%= " "* @indet %>module <%= module_name.camelize %><%- @indet += 1 %>
3
+ <%- end %><%=" "* @indet %>class <%= @file_name %>
4
+
5
+ <%=" "* @indet %>include React::Router
6
+
7
+ <%=" "* @indet %>routes(path: "/") do # change path to be the base path
8
+ <%=" "* @indet %> # you will probably want to update your config/routes.rb file so that it matches all
9
+ <%=" "* @indet %> # subroutes: i.e. get '(*subroutes)' => "<%= (@modules.last || 'home').underscore %>#<%= @file_name.underscore %>"
10
+ <%=" "* @indet %> # basic route has:
11
+ <%=" "* @indet %> # a path
12
+ <%=" "* @indet %> # a name - used to reference the route in methods like redirect, and link)
13
+ <%=" "* @indet %> # a handler - the component that will be mounted on this route
14
+ <%=" "* @indet %> route(path: "subroute1-path", name: :subroute1, handler: Subroute1Component)
15
+ <%=" "* @indet %> route(path: "subroute2-path", name: :subroute2, handler: Subroute2Component)
16
+ <%=" "* @indet %> # routes can take parameters designated with a colon:
17
+ <%=" "* @indet %> route(path: "subroute3-path/:user_id", name: subroute3, handler: Subroute3Component)
18
+ <%=" "* @indet %> # the redirect method will transition any incoming matching routes to a new route
19
+ <%=" "* @indet %> redirect(from: "/", to: :subroute1)
20
+ <%=" "* @indet %> # the not_found method indicates which component to load if no route matches:
21
+ <%=" "* @indet %> not_found(handler: NotFound)
22
+ <%=" "* @indet %>end
23
+
24
+ <%=" "* @indet %>router_param :user_id, as: :user do |id|
25
+ <%=" "* @indet %> # Translate incoming route params to internal values.
26
+ <%=" "* @indet %> # In this case we will refer to the translated user_id as user.
27
+ <%=" "* @indet %> # The block param (id) will have the value of the param.
28
+ <%=" "* @indet %> # This is useful for looking up ActiveRecord models by ids, etc.
29
+ <%=" "* @indet %>end
30
+
31
+ <%=" "* @indet %>def show # note that the top level router has a show method NOT render
32
+ <%=" "* @indet %> div do
33
+ <%=" "* @indet %> # content to display on every route
34
+ <%=" "* @indet %> # link generates an anchor tag.
35
+ <%=" "* @indet %> link(to: :subroute3, params: {user_id: 12}, class: "link-class") { "Click to go to subroute3" }
36
+ <%=" "* @indet %> # within an event handler use transition_to to move to a new route
37
+ <%=" "* @indet %> # the route_handler method will display the current route, it can be called in the
38
+ <%=" "* @indet %> # router, or in some child component.
39
+ <%=" "* @indet %> route_handler
40
+ <%=" "* @indet %> end
41
+ <%=" "* @indet %>end
42
+ <%=" "* @indet %>end
43
+ <%- @modules.each do %><%- @indet -= 1 %><%=" "* @indet %>end
44
+ <%- end %>end
@@ -0,0 +1,4 @@
1
+ require 'rails/generators'
2
+ require 'generators/reactrb/install_generator'
3
+ require 'generators/reactrb/component_generator'
4
+ require 'generators/reactrb/router_generator'
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: reactrb-rails-generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Loic Boutet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ description: This gem provide rails generators for reactrb
28
+ email: loic@boutet.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/generators/reactrb/component_generator.rb
34
+ - lib/generators/reactrb/install_generator.rb
35
+ - lib/generators/reactrb/router_generator.rb
36
+ - lib/generators/reactrb/templates/component_template.rb
37
+ - lib/generators/reactrb/templates/router_template.rb
38
+ - lib/reactrb-rails-generator.rb
39
+ homepage: https://github.com/reactrb/reactrb-rails-generator
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.5.1
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Reactrb generators for rails
63
+ test_files: []