Skip to content

Commit 700b8a9

Browse files
committed
closes #55 closes #56
1 parent ccd224c commit 700b8a9

File tree

10 files changed

+87
-25
lines changed

10 files changed

+87
-25
lines changed

ruby/hyper-component/Gemfile.lock

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ../hyper-spec
33
specs:
4-
hyper-spec (1.0.rc1)
4+
hyper-spec (1.0.alpha1)
55
capybara
66
chromedriver-helper (= 1.2.0)
77
libv8 (~> 6.3.0)
@@ -20,22 +20,22 @@ PATH
2020
PATH
2121
remote: ../hyper-state
2222
specs:
23-
hyper-state (1.0.rc1)
24-
hyperstack-config (= 1.0.rc1)
23+
hyper-state (1.0.alpha1)
24+
hyperstack-config (= 1.0.alpha1)
2525
opal (>= 0.11.0, < 0.12.0)
2626

2727
PATH
2828
remote: ../hyper-store
2929
specs:
30-
hyper-store (1.0.rc1)
31-
hyper-state (= 1.0.rc1)
32-
hyperstack-config (= 1.0.rc1)
30+
hyper-store (1.0.alpha1)
31+
hyper-state (= 1.0.alpha1)
32+
hyperstack-config (= 1.0.alpha1)
3333
opal (>= 0.11.0, < 0.12.0)
3434

3535
PATH
3636
remote: ../hyperstack-config
3737
specs:
38-
hyperstack-config (1.0.rc1)
38+
hyperstack-config (1.0.alpha1)
3939
libv8 (~> 6.3.0)
4040
listen (~> 3.0)
4141
mini_racer (~> 0.1.15)
@@ -47,9 +47,9 @@ PATH
4747
PATH
4848
remote: .
4949
specs:
50-
hyper-component (1.0.rc1)
51-
hyper-state (= 1.0.rc1)
52-
hyperstack-config (= 1.0.rc1)
50+
hyper-component (1.0.alpha1)
51+
hyper-state (= 1.0.alpha1)
52+
hyperstack-config (= 1.0.alpha1)
5353
libv8 (~> 6.3.0)
5454
mini_racer (~> 0.1.15)
5555
opal (>= 0.11.0, < 0.12.0)

ruby/hyper-component/lib/hyperstack/component.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,15 @@ def self.included(base)
3030
base.extend(Hyperstack::Internal::Component::ClassMethods)
3131
end
3232

33+
def self.mounted_components
34+
@__hyperstack_component_mounted_components ||= Set.new
35+
end
36+
3337
def self.force_update!
34-
components = Hyperstack::Internal::Component.mounted_components.to_a
38+
components = mounted_components.to_a # need a copy as force_update may change the list
3539
components.each do |comp|
36-
next unless Hyperstack::Internal::Component.mounted_components.include? comp
40+
# check if its still mounted
41+
next unless mounted_components.include? comp
3742
comp.force_update!
3843
end
3944
end
@@ -62,7 +67,7 @@ def component_will_mount
6267
@__hyperstack_component_params_wrapper = self.class.props_wrapper.new(self)
6368
IsomorphicHelpers.load_context(true) if IsomorphicHelpers.on_opal_client?
6469
observing(immediate_update: true) do
65-
Hyperstack::Internal::Component.mounted_components << self
70+
Hyperstack::Component.mounted_components << self
6671
run_callback(:before_mount, props)
6772
end
6873
end
@@ -96,7 +101,7 @@ def component_will_unmount
96101
observing do
97102
unmount # runs unmount callbacks as well
98103
remove
99-
Hyperstack::Internal::Component.mounted_components.delete self
104+
Hyperstack::Component.mounted_components.delete self
100105
end
101106
end
102107

ruby/hyper-component/lib/hyperstack/internal/component.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,4 @@ module Hyperstack
44

55
define_setting :prerendering, :off if RUBY_ENGINE != 'opal'
66

7-
module Internal
8-
module Component
9-
class << self
10-
def mounted_components
11-
@__hyperstack_component_mounted_components ||= Set.new
12-
end
13-
end
14-
end
15-
end
167
end

ruby/hyper-component/lib/hyperstack/internal/component/class_methods.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ def hyper_component?
1212
true
1313
end
1414

15+
def mounted_components
16+
Hyperstack::Component.mounted_components.select { |c| c.class <= self }
17+
end
18+
1519
def param_accessor_style(*args)
1620
props_wrapper.param_accessor_style(*args)
1721
end

ruby/hyper-component/spec/client_features/component_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,51 @@ def render
177177
end
178178
expect_evaluate_ruby('Foo.instance == Foo.instance.force_update!').to be_truthy
179179
end
180+
181+
it 'has a class components method' do
182+
mount 'Foo' do
183+
class Bar < HyperComponent
184+
param :id
185+
render { inspect }
186+
end
187+
class Baz < HyperComponent
188+
param :id
189+
render { inspect }
190+
end
191+
class BarChild < Bar
192+
end
193+
class Foo
194+
include Hyperstack::Component
195+
def render
196+
DIV do
197+
Bar(id: 1)
198+
Bar(id: 2)
199+
Baz(id: 3)
200+
Baz(id: 4)
201+
BarChild(id: 5)
202+
BarChild(id: 6)
203+
end
204+
end
205+
end
206+
module Hyperstack::Component
207+
def to_s
208+
"#{self.class.name}#{':' + @Id.to_s if @Id}"
209+
end
210+
end
211+
end
212+
expect_evaluate_ruby("Hyperstack::Component.mounted_components")
213+
.to contain_exactly("Hyperstack::Internal::Component::TopLevelRailsComponent", "Foo", "Bar:1", "Bar:2", "Baz:3", "Baz:4", "BarChild:5", "BarChild:6")
214+
expect_evaluate_ruby("HyperComponent.mounted_components")
215+
.to contain_exactly("Bar:1", "Bar:2", "Baz:3", "Baz:4", "BarChild:5", "BarChild:6")
216+
expect_evaluate_ruby("Bar.mounted_components")
217+
.to contain_exactly("Bar:1", "Bar:2", "BarChild:5", "BarChild:6")
218+
expect_evaluate_ruby("Baz.mounted_components")
219+
.to contain_exactly("Baz:3", "Baz:4")
220+
expect_evaluate_ruby("BarChild.mounted_components")
221+
.to contain_exactly("BarChild:5", "BarChild:6")
222+
expect_evaluate_ruby("Foo.mounted_components")
223+
.to contain_exactly("Foo")
224+
end
180225
end
181226

182227
describe 'state management' do

ruby/hyper-router/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ gem 'hyper-spec', path: '../hyper-spec'
44
gem 'hyperstack-config', path: '../hyperstack-config'
55
gem 'hyper-state', path: '../hyper-state'
66
gem 'hyper-component', path: '../hyper-component'
7+
gem 'hyper-store', path: '../hyper-store'
78
gemspec

ruby/hyper-router/hyper-router.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
2222
spec.add_development_dependency 'chromedriver-helper'
2323
spec.add_development_dependency 'database_cleaner'
2424
spec.add_development_dependency 'hyper-spec', HyperRouter::VERSION
25+
spec.add_development_dependency 'hyper-store', HyperRouter::VERSION
2526
spec.add_development_dependency 'listen'
2627
spec.add_development_dependency 'mini_racer', '~> 0.1.15'
2728
spec.add_development_dependency 'opal-rails', '~> 0.9.4'

ruby/hyper-router/lib/hyperstack/router.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def location
1818

1919
after_mount do
2020
@_react_router_unlisten = history.listen do |location, _action|
21-
Hyperstack::Internal::State::Mapper.observed! Hyperstack::Router::Location
21+
Hyperstack::Internal::State::Mapper.mutated! Hyperstack::Router::Location
2222
end
2323
end
2424

ruby/hyper-router/spec/hyper-router/basic_dsl_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
it "can route" do
66
visit '/'
7+
expect(page.find('a', id: 'isolated_nav_link')[:class]).not_to include('selected')
78
page.find('a', id: 'about_link').click
9+
expect(page.find('a', id: 'isolated_nav_link')[:class]).to include('selected')
810
expect(page).to have_content('About Page')
911
expect(page.current_path).to eq('/about')
1012
page.find('a', id: 'topics_link').click
1113
expect(page).to have_content('Topics Page')
1214
expect(page.current_path).to eq('/topics')
15+
expect(page.find('a', id: 'components_link')[:class]).not_to include('selected')
1316
page.find('a', id: 'components_link').click
17+
expect(page.find('a', id: 'components_link')[:class]).to include('selected')
1418
expect(page).to have_content('more on components...')
1519
expect(page.current_path).to eq('/topics/components')
1620
end

ruby/hyper-router/spec/test_app/app/hyperstack/components/basic_example.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class App < HyperComponent
88
LI { Link('/', id: :home_link) { 'Home' } }
99
LI { Link('/about', id: :about_link) { 'About' } }
1010
LI { Link('/topics', id: :topics_link) { 'Topics' } }
11+
LI { IsolatedNavLink() }
1112
end
1213
Route('/', exact: true, mounts: Home)
1314
Route('/about') { About() }
@@ -32,7 +33,7 @@ class Topics < HyperComponent
3233
H2 { 'Topics Page' }
3334
UL() do
3435
LI { Link("#{match.url}/rendering") { 'Rendering with React' } }
35-
LI { Link("#{match.url}/components", id: :components_link) { 'Components' } }
36+
LI { NavLink("#{match.url}/components", id: :components_link, active_class: :selected) { 'Components' } }
3637
LI { Link("#{match.url}/props-v-state") { 'Props v. State' } }
3738
end
3839
Route("#{match.url}/:topic_id", mounts: Topic)
@@ -42,6 +43,16 @@ class Topics < HyperComponent
4243
end
4344
end
4445

46+
class IsolatedNavLink
47+
include Hyperstack::Component
48+
include Hyperstack::Router::Helpers
49+
# this tests that a component does not have to be mounted directly from a Router
50+
# for the active_class functionality to work
51+
render do
52+
NavLink('/about', id: :isolated_nav_link, active_class: :selected) { 'about-face' }
53+
end
54+
end
55+
4556
class Topic < HyperComponent
4657
render(DIV) do
4758
H3 { "more on #{match.params[:topic_id]}..." }

0 commit comments

Comments
 (0)