Skip to content

Commit 7954c2a

Browse files
committed
updated install instructions and generators
1 parent 06b2053 commit 7954c2a

File tree

12 files changed

+650
-1050
lines changed

12 files changed

+650
-1050
lines changed

docs/installation/installation.md

Lines changed: 503 additions & 273 deletions
Large diffs are not rendered by default.

docs/installation/man-installation.md

Lines changed: 0 additions & 709 deletions
This file was deleted.
Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
1-
require 'rails/generators'
21
module Hyper
3-
class Component < Rails::Generators::Base
4-
source_root File.expand_path('../templates', __FILE__)
5-
argument :components, type: :array
6-
class_option 'base-class', :default => 'HyperComponent'
7-
class_option 'add-route', :default => nil
8-
9-
def create_component_file
10-
clear_cache
11-
insure_hyperstack_loader_installed
12-
insure_base_component_class_exists unless options['base-class'] == 'skip'
13-
self.components.each do |component|
14-
component_array = component.split('::')
15-
@modules = component_array[0..-2]
16-
@file_name = component_array.last
17-
@indent = 0
18-
template 'component_template.rb',
19-
File.join('app', 'hyperstack', 'components',
20-
*@modules.map(&:downcase),
21-
"#{@file_name.underscore}.rb")
22-
end
23-
add_route
2+
class Component < GeneratorBase
3+
def add_component
4+
create_component_file 'component_template.rb'
245
end
256
end
267
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'rails/generators'
2+
module Hyper
3+
class GeneratorBase < Rails::Generators::Base
4+
class << self
5+
alias rails_inherited inherited
6+
def inherited(child)
7+
rails_inherited(child)
8+
child.class_eval do
9+
source_root File.expand_path('../templates', __FILE__)
10+
argument :components, type: :array
11+
class_option 'base-class', :default => nil # will pull in value from config setting
12+
class_option 'add-route', :default => nil
13+
class_option 'no-help', :default => nil
14+
end
15+
end
16+
end
17+
end
18+
end
Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
1-
require 'rails/generators'
21
module Hyper
3-
class Router < Rails::Generators::Base
4-
source_root File.expand_path('../templates', __FILE__)
5-
6-
argument :component, type: :string
7-
class_option :path, type: :string, default: '/(*other)'
8-
def create_component_file
9-
component_array = component.split('::')
10-
@modules = component_array[0..-2]
11-
@file_name = component_array.last
12-
13-
@indent = 0
14-
template 'router_template.rb',
15-
File.join('app/hyperstack/components',
16-
@modules.map(&:downcase).join('/'),
17-
"#{@file_name.underscore}.rb")
18-
end
19-
20-
def add_route
21-
route "get '#{options['path']}', to: 'hyperstack##{@file_name.underscore}'"
2+
class Router < GeneratorBase
3+
def add_router_component
4+
create_component_file 'router_template.rb'
225
end
236
end
247
end

ruby/rails-hyperstack/lib/generators/hyper/templates/component_template.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<%- @modules.each do |module_name| %><%= " "* @indent %>module <%= module_name.camelize %><%- @indent += 1 %>
22
<%- end %><%=" "* @indent %>class <%= @file_name %> < <%= @component_base_class %>
3-
3+
<%- unless @no_help %>
44
<%=" "* @indent %> # param :my_param
55
<%=" "* @indent %> # param param_with_default: "default value"
66
<%=" "* @indent %> # param :param_with_default2, default: "default value" # alternative syntax
@@ -9,8 +9,16 @@
99
<%=" "* @indent %> # other :attributes # collects all other params into a hash
1010
<%=" "* @indent %> # fires :callback # creates a callback param
1111

12-
<%=" "* @indent %> # The following are the most common lifecycle call backs,
13-
<%=" "* @indent %> # the following are the most common lifecycle call backs# delete any that you are not using.
12+
<%=" "* @indent %> # access params using the param name
13+
<%=" "* @indent %> # fire a callback using the callback name followed by a !
14+
15+
<%=" "* @indent %> # state is kept and read as normal instance variables
16+
<%=" "* @indent %> # but when changing state prefix the statement with `mutate`
17+
<%=" "* @indent %> # i.e. mutate @my_state = 12
18+
<%=" "* @indent %> # mutate @my_other_state[:bar] = 17
19+
20+
<%=" "* @indent %> # the following are the most common lifecycle call backs,
21+
<%=" "* @indent %> # delete any that you are not using.
1422
<%=" "* @indent %> # call backs may also reference an instance method i.e. before_mount :my_method
1523

1624
<%=" "* @indent %> before_mount do
@@ -28,12 +36,14 @@
2836
<%=" "* @indent %> end
2937

3038
<%=" "* @indent %> before_unmount do
31-
<%=" "* @indent %> # cleanup any thing (i.e. timers) before component is destroyed
39+
<%=" "* @indent %> # cleanup any thing before component is destroyed
40+
<%=" "* @indent %> # note timers are broadcast receivers are cleaned up
41+
<%=" "* @indent %> # automatically
3242
<%=" "* @indent %> end
3343

34-
<%=" "* @indent %> render do
44+
<%- end %><%=" "* @indent %> render do
3545
<%=" "* @indent %> DIV do
36-
<%=" "* @indent %> "<%= (@modules+[@file_name]).join('::') %>"
46+
<%=" "* @indent %> '<%= (@modules+[@file_name]).join('::') %>'
3747
<%=" "* @indent %> end
3848
<%=" "* @indent %> end
3949
<%=" "* @indent %>end

ruby/rails-hyperstack/lib/generators/hyper/templates/router_template.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<%- @modules.each do |module_name| %><%= " "* @indent %>module <%= module_name.camelize %><%- @indent += 1 %>
2-
<%- end %><%=" "* @indent %>class <%= @file_name %> < HyperComponent
2+
<%- end %><%=" "* @indent %>class <%= @file_name %> < <%= @component_base_class %>
33
<%=" "* @indent %> include Hyperstack::Router
44
<%=" "* @indent %> render do
55
<%=" "* @indent %> DIV do
66
<%=" "* @indent %> '<%= (@modules+[@file_name]).join('::') %>'
7-
<%=" "* @indent %> # define routes using the Route psuedo component. Examples:
7+
<%- unless @no_help %><%=" "* @indent %> # define routes using the Route psuedo component. Examples:
88
<%=" "* @indent %> # Route('/foo', mounts: Foo) : match the path beginning with /foo and mount component Foo here
99
<%=" "* @indent %> # Route('/foo') { Foo(...) } : display the contents of the block
1010
<%=" "* @indent %> # Route('/', exact: true, mounts: Home) : match the exact path / and mount the Home component
1111
<%=" "* @indent %> # Route('/user/:id/name', mounts: UserName) : path segments beginning with a colon will be captured in the match param
1212
<%=" "* @indent %> # see the hyper-router gem documentation for more details
13-
<%=" "* @indent %> end
13+
<%- end %><%=" "* @indent %> end
1414
<%=" "* @indent %> end
1515
<%=" "* @indent %>end
1616
<%- @modules.each do %><%- @indent -= 1 %><%=" "* @indent %>end

ruby/rails-hyperstack/lib/generators/hyperstack/install_generator.rb

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ class InstallGenerator < Rails::Generators::Base
1010
class_option 'hyper-model-only', type: :boolean
1111

1212
def add_component
13-
generate 'hyper:component App -add-router' unless skip_adding_component?
13+
if skip_adding_component?
14+
# normally this is handled by the hyper:component
15+
# generator, but if we are skipping it we will check it
16+
# now.
17+
insure_hyperstack_loader_installed
18+
else
19+
generate 'hyper:router App --add-route'
20+
end
1421
end
1522

1623
def add_hotloader
@@ -149,27 +156,29 @@ def move_and_update_application_record
149156

150157
def add_engine_route
151158
return if skip_hyper_model?
152-
159+
route 'mount Hyperstack::Engine => \'/hyperstack\' # this route should be first in the routes file so it always matches'
160+
end
153161

154162
def report
155163
say "\n\n"
156164
unless skip_adding_component?
157165
say "🎢 Top Level App Component successfully installed at app/hyperstack/components/app.rb 🎢", :green
158166
end
159-
unless skip_hotloader?
160-
say "🚒 Hyperstack Hotloader installed - use bundle exec foreman start and visit localhost:5000 🚒", :green
161-
end
162167
unless skip_webpack?
163-
say "🚀 Webpack integrated with Hyperstack. "\
164-
"Add javascript assets to app/javascript/packs/client_only.js and /client_and_server.js 🚀", :green
168+
say "📦 Webpack integrated with Hyperstack. "\
169+
"Add javascript assets to app/javascript/packs/client_only.js and /client_and_server.js 📦", :green
165170
end
166171
unless skip_hyper_model?
167172
say "👩‍✈️ Basic development policy defined. See app/policies/application_policy.rb 👨🏽‍✈️", :green
168-
say "💽 Move any Active Record models to the app/hyperstack/models to access them from the client 📀", :green
173+
say "💽 HyperModel installed. Move any Active Record models to the app/hyperstack/models to access them from the client 📀", :green
169174
end
170175
if File.exists?(init = File.join('config', 'initializers', 'hyperstack.rb'))
171176
say "☑️ Check #{init} for other configuration options. ☑️", :green
172177
end
178+
unless skip_hotloader?
179+
say "🚒 Hyperstack Hotloader installed - use bundle exec foreman start and visit localhost:5000 🚒", :green
180+
end
181+
173182
say "\n\n"
174183
end
175184

@@ -198,9 +207,27 @@ def inject_into_initializer(s)
198207
else
199208
create_file file_name, <<-RUBY
200209
#{s}
210+
# set the component base class
211+
212+
Hyperstack.component_base_class = 'HyperComponent' # i.e. 'ApplicationComponent'
213+
214+
# prerendering is default :off, you should wait until your
215+
# application is relatively well debugged before turning on.
216+
201217
Hyperstack.prerendering = :off # or :on
218+
219+
# transport controls how push (websocket) communications are
220+
# implemented. The default is :action_cable.
221+
# Other possibilities are :pusher (see www.pusher.com) or
222+
# :simple_poller which is sometimes handy during system debug.
223+
224+
Hyperstack.transport = :action_cable # or :none, :pusher, :simple_poller
225+
202226
# add this line if you need jQuery AND ARE NOT USING WEBPACK
203227
# Hyperstack.import 'hyperstack/component/jquery', client_only: true
228+
229+
# change definition of on_error to control how errors such as validation
230+
# exceptions are reported on the server
204231
module Hyperstack
205232
def self.on_error(operation, err, params, formatted_error_message)
206233
::Rails.logger.debug(

ruby/rails-hyperstack/lib/generators/hyperstack/install_generator_base.rb

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ class Base < Thor::Group
66

77
protected
88

9+
def create_component_file(template)
10+
clear_cache
11+
insure_hyperstack_loader_installed
12+
insure_base_component_class_exists
13+
@no_help = options.key?('no-help')
14+
self.components.each do |component|
15+
component_array = component.split('::')
16+
@modules = component_array[0..-2]
17+
@file_name = component_array.last
18+
@indent = 0
19+
template template,
20+
File.join('app', 'hyperstack', 'components',
21+
*@modules.map(&:downcase),
22+
"#{@file_name.underscore}.rb")
23+
end
24+
add_route
25+
end
26+
27+
928
def clear_cache
1029
run 'rm -rf tmp/cache' unless Dir.exists?(File.join('app', 'hyperstack'))
1130
end
@@ -37,7 +56,7 @@ def insure_hyperstack_loader_installed
3756

3857

3958
def insure_base_component_class_exists
40-
@component_base_class = options['base-class']
59+
@component_base_class = options['base-class'] || Hyperstack.component_base_class
4160
file_name = File.join(
4261
'app', 'hyperstack', 'components', "#{@component_base_class.underscore}.rb"
4362
)
@@ -63,10 +82,15 @@ def add_route
6382
end
6483
action_name = (@modules+[@file_name.underscore]).join('__')
6584
path = options['add-route'] == 'add-route' ? '/(*others)' : options['add-route']
66-
route "get '#{path}', to: 'hyperstack##{action_name}'"
85+
routing_code = "get '#{path}', to: 'hyperstack##{action_name}'"
86+
log :route, routing_code
87+
[/mount\s+Hyperstack::Engine[^\n]+\n/m, /\.routes\.draw do\s*\n/m].each do |sentinel|
88+
in_root do
89+
x = inject_into_file "config/routes.rb", optimize_indentation(routing_code, 2), after: sentinel, verbose: false, force: false
90+
end
91+
end
6792
end
6893

69-
7094
def yarn(package, version = nil)
7195
return if system("yarn add #{package}#{'@' + version if version}")
7296
raise Thor::Error.new("yarn failed to install #{package} with version #{version}")

ruby/rails-hyperstack/lib/rails-hyperstack.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
Hyperstack.import 'hyper-state'
1515

1616
require 'generators/hyperstack/install_generator'
17+
require 'generators/hyper/generator_base'
1718
require 'generators/hyper/component_generator'
1819
require 'generators/hyper/router_generator'
1920
begin
@@ -32,3 +33,5 @@ class RailsHyperstack < Rails::Railtie
3233
Dir[File.join(File.dirname(__FILE__),'tasks/hyperstack/*.rake')].each { |f| puts "loading #{f}"; load f }
3334
end
3435
end
36+
37+
Hyperstack.define_setting :component_base_class, 'HyperComponent'

0 commit comments

Comments
 (0)