This repository was archived by the owner on Oct 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.html
More file actions
696 lines (548 loc) · 29.7 KB
/
index.html
File metadata and controls
696 lines (548 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv='X-UA-Compatible' content='IE=edge;chrome=1' />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="google-site-verification" content="qjqVOvRhHfsuDbLOvaAnj2mkUZJ9Xwzg84TMVXduvUc" />
<meta charset="UTF-8">
<title>Hyperloop</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://unpkg.com/react@15/dist/react.min.js"></script>
<script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.5/marked.min.js"></script>
<!-- Opal and Hyperloop -->
<script src="http://cdn.opalrb.org/opal/current/opal.min.js"></script>
<script src="https://rawgit.com/ruby-hyperloop/hyperloop-js/master/dist/opal.min.js"></script>
<script src="https://rawgit.com/ruby-hyperloop/hyperloop-js/master/dist/hyperloop.min.js"></script>
<script src="https://rawgit.com/ruby-hyperloop/hyperloop-js/master/dist/hyperloop-compiler.min.js"></script>
<!-- If you want local copies... -->
<!-- <script src="../../../javascripts/opal-compiler.js"></script> -->
<!-- <script src="../../../javascripts/hyperloop.js"></script> -->
<script src="https://npmcdn.com/tether@1.2.4/dist/js/tether.min.js"></script>
<script src="../../../javascripts/bootstrap.min.js"></script>
<script src="../../../javascripts/codemirror.js"></script>
<script src="../../../javascripts/ruby.js"></script>
<script src="../../../javascripts/matchbrackets.js"></script>
<script src="../../../javascripts/react_player.js"></script>
<script src="../../../javascripts/highlight.pack.js"></script>
<!-- Components are compiled by Hyperloop Express -->
<script type="text/ruby">
class CodeMirror < Hyperloop::Component
param :code, type: String
param :heading, default: "Code"
param :rows, type: Integer, default: 0
param :top_level_component, type: String
before_mount do
r=rand(2**256).to_s(36)[0..7]
@div_code = "code_#{r}"
@div_result = "result_#{r}"
end
after_mount do
# puts params.code
@editor = `CodeMirror(document.getElementById(#{@div_code}), {
value: #{params.code.to_s},
mode: 'text/x-ruby',
matchBrackets: true,
lineNumbers: false,
indentUnit: 2,
theme: 'github'
});`
`#{@editor}.on('change', #{lambda {on_change} })`
`#{@editor}.setSize(null, #{@editor}.defaultTextHeight()*#{params.rows})` unless params.rows == 0
execute_code
end
render(DIV) do
div.card {
div.card_header { params.heading }
div(id: @div_code)
div.card_header do
img(src: '../../../images/hyperloop-logo-small-white.png' , width:'25')
span { ' ' }
'Live editor results'
end
div.card_block(id: @div_result)
}
end
def on_change
execute_code
end
def execute_code
begin
code = `#{@editor}.getValue()`
# puts code
code += "\nElement['##{@div_result}'].render(#{params.top_level_component})"
compiled_code = Opal::Compiler.new(code).compile
`ReactDOM.unmountComponentAtNode(document.getElementById(#{@div_result}));`
# Dispatchers and Receivers example works but Steps example breaks
# Hyperloop::Context.reset!
# `eval(#{compiled_code})`
# Hyperloop::Application::Boot.run()
# Steps example works but Dispatchers and Receivers example breaks
`eval(#{compiled_code})`
Hyperloop::Context.reset!
Hyperloop::Application::Boot.run()
component = Module.const_get params.top_level_component
# we need to see if the component is valid - try checking if it can render static markup
# the following line generates: `undefined method to_n for SimpleComponent`
# if React.render_to_static_markup( component ).empty?
# invalid_component_message
# end
rescue Exception => e
@time_out = after(0.1) do
unable_to_compile_message e.message
end
end
end
def invalid_component_message
message = div.text_danger do
h3.text_danger {"Oops, invalid Component..."}
p { "Your Component has been rejected by React. A valid Component must have a render macro and return just one HTML element." }
end
Element["##{@div_result}"].render{ message }
end
def unable_to_compile_message reason
message = div.text_danger do
h3.text_danger {"Can't compile..."}
p { reason }
end
Element["##{@div_result}"].render{ message }
end
end
</script>
<script type="text/ruby">
Document.ready? do
Element.find('div.codemirror-live-edit').each do |mount_point|
heading = mount_point.attr('data-heading')
rows = mount_point.attr('data-rows')
top_level_component = mount_point.attr('data-top-level-component')
code = Element[mount_point].find('pre').text.strip
params = {code: code, top_level_component: top_level_component}
params = params.merge({heading: heading}) if heading
params = params.merge({rows: rows.to_i}) if rows
codemirror_component = Object.const_get('CodeMirror')
React.render(React.create_element(codemirror_component, params ), mount_point)
end
end
</script>
<script type="text/ruby">
class ToggleCodemirror < Hyperloop::Component
param :code, type: String
param :heading, default: "Code"
param :rows, type: Integer, default: 0
param :top_level_component, type: String
param :show_code
before_mount do
r=rand(2**256).to_s(36)[0..7]
@div_code = "code_#{r}"
@div_result = "result_#{r}"
end
after_mount do
# puts params.code
@editor = `CodeMirror(document.getElementById(#{@div_code}), {
value: #{params.code.to_s},
mode: 'text/x-ruby',
matchBrackets: true,
lineNumbers: false,
indentUnit: 2,
readOnly: true,
theme: 'github'
});`
`#{@editor}.setSize(null, #{@editor}.defaultTextHeight()*#{params.rows})` unless params.rows == 0
Element['.codediv'].hide('')
mutate.show_code false
end
render(DIV) do
div.card {
div.card_header do
div(class: 'row') do
div(class: 'col-md-6') do
img(src: '../../../images/hyperloop-logo-small-white.png' , width:'25')
span { ' ' }
span { params.heading }
end
div(class: 'col-md-6 align-right') do
toggle_link
end
end
end
div(id: @div_code, class: 'codediv')
}
end
def toggle_link
BUTTON(class: 'btn btn-info btn-sm') do
state.show_code ? "Click to hide code" : "Click to show code"
end.on(:click) do |ev|
mutate.show_code !state.show_code
state.show_code ? Element["##{@div_code}"].show('') : Element["##{@div_code}"].hide('')
end
end
end
</script>
<script type="text/ruby">
Document.ready? do
Element.find('div.togglecode').each do |mount_point|
heading = mount_point.attr('data-heading')
rows = mount_point.attr('data-rows')
top_level_component = mount_point.attr('data-top-level-component')
code = Element[mount_point].find('pre').text.strip
params = {code: code, top_level_component: top_level_component}
params = params.merge({heading: heading}) if heading
params = params.merge({rows: rows.to_i}) if rows
codemirror_component = Object.const_get('ToggleCodemirror')
React.render(React.create_element(codemirror_component, params ), mount_point)
end
end
</script>
<link href="../../../stylesheets/bootstrap.min.css" rel="stylesheet" />
<link href="../../../stylesheets/typography.css" rel="stylesheet" />
<link href="../../../stylesheets/override.css" rel="stylesheet" />
<link href="../../../stylesheets/divtable.css" rel="stylesheet" />
<!-- <link href="../../../stylesheets/code.css" rel="stylesheet" /> -->
<link href="../../../stylesheets/github.css" rel="stylesheet" />
<link href="../../../stylesheets/highlighting.css" rel="stylesheet" />
<link href="../../../stylesheets/codemirror.css" rel="stylesheet" />
<link href="../../../stylesheets/monokai-sublime.css" rel="stylesheet" />
<link rel="alternate" type="application/atom+xml" title="Atom Feed" href="/feed.xml" />
<!-- Favicons -->
<link rel="apple-touch-icon" sizes="180x180" href="../../../images/apple-touch-icon.png">
<link rel="icon" type="image/png" href="../../../images/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="../../../images/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="/images/manifest.json">
<link rel="mask-icon" href="../../../images/safari-pinned-tab.svg" color="#e81176">
<meta name="theme-color" content="#ffffff">
</head>
<body>
<div class="navbarbackgroundcolor">
<div class="container">
<nav class="navbar navbar-ligh">
<button class="navbar-toggler hidden-sm-up" type="button" data-toggle="collapse" data-target="#exCollapsingNavbar2" aria-controls="exCollapsingNavbar2" aria-expanded="false" aria-label="Toggle navigation">
☰
</button>
<div class="collapse navbar-toggleable-xs" id="exCollapsingNavbar2">
<ul class="nav navbar-nav">
<li class="nav-item">
<a href="/" class="nav-link"><span class='navfirstletter'>H</span>yperloop</a>
</li>
<li class="nav-item">
<a href="/start/components" class="nav-link active"><span class='navfirstletter'>S</span>tart</a>
</li>
<li class="nav-item">
<a href="/installation" class="nav-link"><span class='navfirstletter'>I</span>nstallation</a>
</li>
<li class="nav-item">
<a href="/tutorials" class="nav-link"><span class='navfirstletter'>T</span>utorials</a>
</li>
<li class="nav-item">
<a href="/gems" class="nav-link"><span class='navfirstletter'>G</span>ems</a>
</li>
<li class="nav-item">
<a href="https://github.com/ruby-hyperloop" class="nav-link"><span class='navfirstletter'>G</span>ithub</a>
</li>
<li class="nav-item">
<a href="/tools" class="nav-link"><span class='navfirstletter'>T</span>ools</a>
</li>
<li class="nav-item">
<a href="/docs/architecture" class="nav-link"><span class='navfirstletter'>D</span>ocs</a>
</li>
<li class="nav-item">
<a href="/help" class="nav-link"><span class='navfirstletter'>H</span>elp</a>
</li>
<li class="nav-item">
<a href="/blog" class="nav-link"><span class='navfirstletter'>B</span>log</a>
</li>
<!-- <li class="nav-item">
<input type="text" name="search" />
</li> -->
</ul>
</div>
</nav>
</div>
</div>
<div class="jumbotron page-header">
<div class="container">
<div class="row hidden-sm-down">
<div class="col-md-2">
<div class="hyperlooplogo">
</div>
</div>
<div class="col-md-8">
<h1 class="display-4 project-name">Hyperloop</h1>
<h4 class="display-7 project-tagline">
Tutorials, screencasts & videos
</h4>
</div>
</div>
<div class="row hidden-md-up">
<div class="col-md-3">
<div class="hyperlooplogo">
</div>
</div>
<div class="col-md-9">
<h1 class="h1 project-name center-text">Hyperloop</h1>
</br>
<h2 class="h5 project-tagline center-text">
Tutorials, screencasts & videos
</h2>
</div>
</div>
</div>
</div>
<div class="page-header-underline">
<div class="container">
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-3 sidenavcol">
<ul class="nav">
<li class="nav-item"><strong><a href="/tutorials/hyperloopcomps">Hyperloop COMPS</a></strong></li>
<ul class="nav">
<li class="nav-item"><a href="/tutorials/hyperloopcomps/compstoreop-first">Simple COMPS App</a></li>
</ul>
<br>
<li class="nav-item"><strong><a href="/tutorials/hyperloopjs">Hyperloop.js</a></strong></li>
<ul class="nav">
<li class="nav-item"><a href="/tutorials/hyperloopjs/helloworld">Hello World</a></li>
<li class="nav-item"><a href="/tutorials/hyperloopjs/chatapp">Chat App</a></li>
</ul>
<br>
<li class="nav-item"><strong><a href="/tutorials/hyperlooprails">Hyperloop and Rails</a></strong></li>
<ul class="nav">
<li class="nav-item"><a href="/tutorials/hyperlooprails/helloworld">Hello World</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/todo-tutorial">Todo MVC</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/chatapp">Chat App</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/hyperloop-devise-tutorial">Devise</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/eventsplanner">Events planner</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/fivelettergame">Five letter word game</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/cloud9">Hyperloop and Cloud9</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/webpack">NPM and Webpack</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/hyperloop-rails-webpackergem-helloworld">Webpacker GEM</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/opalhotreloader">Opal Hot Reloader</a></li>
<li class="nav-item"><a href="/tutorials/hyperlooprails/productionmode">Production mode</a></li>
</ul>
<br>
<li class="nav-item"><strong><a href="/tutorials/hyperloopdeploy">Hyperloop deployment</a></strong></li>
<ul class="nav">
<li class="nav-item"><a href="/tutorials/hyperloopdeploy/deployvps">Deploying on VPS</a></li>
<li class="nav-item"><a href="/tutorials/hyperloopdeploy/deployheroku">Deploying on Heroku</a></li>
</ul>
<br>
<li class="nav-item"><strong><a href="/tutorials/opal">Opal</a></strong></li>
<br>
<li class="nav-item"><strong><a href="/tutorials/videos">Videos</a></strong></li>
</ul>
<div class="getstarted">
<br>
<hr>
<p>NEWS: RubyHyperloop is to be renamed Hyperstack. Come and see the work-in-progress site: </p>
<!-- <button type="button" class="btn btn-primary btn-lg btn-hyperlooppink" onclick="https://hyperstack.org';">https://hyperstack.org</button> -->
<a href='https://hyperstack.org'>https://hyperstack.org</a>
</div>
<br>
</div>
<div class="col-md-9 main-content">
<h2 id="devise-with-hyperloop-tutorial"><i class="flaticon-professor-teaching"></i><span class="bigfirstletter">D</span>evise with Hyperloop Tutorial</h2>
<p>This tutorial will demonstrate how to use the popular <a href="https://github.com/plataformatec/devise">Devise Gem</a> with Hyperloop. In this tutorial, we will be using the standard Devise user session ERB views for all Devise related activity (creating the user, changing the password, etc), and demonstrating how Hyperloop co-exists with Devise.</p>
<p>The goal of this tutorial is to show the interactivity between Hyperloop and Devise, so we will go quickly through the setup of Hyperloop and Devise. If these technologies are new to you, then this is not the ideal tutorial to start with. Please be sure to see the <a href="http://ruby-hyperloop.org/tutorials/hyperlooprails/">Hyperloop with Rails tutorials</a> and the <a href="https://github.com/plataformatec/devise">Devise Gem</a> first.</p>
<h2 id="setup">Setup</h2>
<p>Before continuing, you will need a Rails app with Hyperloop and Devise setup and working. Either clone the source code of this tutorial or run through the setup steps detailed here.</p>
<h2 id="setup-rails-hyperloop-and-devise">Setup Rails, Hyperloop and Devise</h2>
<p>If you already have Devise and Hyperloop setup you can skip this section and go directly to <a href="#using-devise-with-hyperloop">Using Devise with Hyperloop</a></p>
<p>This chapter that you through a basic installation of Devise and Hyperloop.</p>
<h3 id="creating-a-new-rails-app">Creating a new Rails app</h3>
<p>Assuming you have Ruby and Rails installed, from the command line type:</p>
<p><code>rails new devise_hyperloop</code>
<code>cd devise_hyperloop</code>
<code>bundel install</code></p>
<p>We are going to need a User model, so let's create a simple one now:</p>
<pre class="highlight plaintext"><code>rails g model User first_name:string last_name:string
rails db:migrate
</code></pre>
<h3 id="installing-hyperloop-and-creating-a-simple-component">Installing Hyperloop and creating a simple Component</h3>
<p>Firstly add the master Hyperloop gem to your Gemfile.</p>
<pre class="highlight ruby"><code><span class="c1"># Gemfile</span>
<span class="n">gem</span> <span class="s1">'hyperloop'</span>
</code></pre>
<p>Next run bundle:</p>
<p><code>bundle install</code></p>
<p>Then run the Hyperloop generator and create a simple Component:</p>
<pre class="highlight plaintext"><code>rails g hyperloop:install
rails g hyper:component Helloworld
</code></pre>
<p>Next, create a route in <code>routes.rb</code> which points to the Helloworld Component we have just created.</p>
<blockquote>
<p>Note that this shortcut replaces the need for a Rails controller and view. There is an automagical <code>hyperloop</code> controller which will load the component specified. If you prefer not to take this shortcut, you can create a controller and view which will load your Helloworld Component instead. See the Hyperloop website for details on how to do this.</p>
</blockquote>
<p>For this tutorial we will take the shortcut:</p>
<pre class="highlight ruby"><code><span class="c1"># routes.rb</span>
<span class="n">root</span> <span class="s1">'hyperloop#helloworld'</span>
</code></pre>
<p>Next, we should check that out transport is correctly configured. This tutorial uses ActionCable. In <code>config/initializers/hyperloop.rb</code> ensure that we have a valid transport:</p>
<pre class="highlight ruby"><code><span class="c1"># config/initializers/hyperloop.rb</span>
<span class="no">Hyperloop</span><span class="p">.</span><span class="nf">configuration</span> <span class="k">do</span> <span class="o">|</span><span class="n">config</span><span class="o">|</span>
<span class="n">config</span><span class="p">.</span><span class="nf">transport</span> <span class="o">=</span> <span class="ss">:action_cable</span>
<span class="k">end</span>
</code></pre>
<p>To access our User Model on the client, we need to move it (and applciation_record.rb for Rails 5.x) to the <code>hyperloop/models</code> folder.</p>
<p><code>mv app/models/user.rb app/hyperloop/models/</code>
<code>mv app/models/application_record.rb app/hyperloop/models/</code></p>
<p>If you start your rails server and navigate to <code>localhost:3000</code>. <code>Helloworld</code> should be displayed in your browser. Admittedly this is not very exciting, but it does prove that Hyperloop is correctly installed.</p>
<h3 id="installing-devise">Installing Devise</h3>
<p>Next, we will install Devise. In your <code>Gemfile</code> add:</p>
<pre class="highlight ruby"><code><span class="c1"># Gemfile</span>
<span class="n">gem</span> <span class="s1">'devise'</span>
</code></pre>
<p>Then bundle:</p>
<p><code>bundle install</code></p>
<p>Then run the Devise generator:</p>
<p><code>rails generate devise:install</code></p>
<p>As this tutorial is not about Devise but rather its connection to Hyperloop, we will do the minimal Devise setup required. Please see the Devise website for more information on setting up Devise thoroughly.</p>
<p>Devise needs to augment our User model, so we will run a generator for that:</p>
<pre class="highlight plaintext"><code>rails g devise User
rails db:migrate
</code></pre>
<p>At this point, Devise should be setup and working. Let's test that by starting your Rails server and navigating to:</p>
<p><code>http://localhost:3000/users/sign_up</code></p>
<p>You should see a very basic looking signup page! Now we know that Devise is setup and working.</p>
<p>As a final step, we need to tell Rails to protect all our controllers with Devise. Add the following line to your <code>application_controller.rb</code></p>
<pre class="highlight ruby"><code><span class="c1"># application_controller.rb</span>
<span class="n">before_action</span> <span class="ss">:authenticate_user!</span>
</code></pre>
<p>At this stage, we have Hyperloop and Devise installed so now we will discuss how to connect them.</p>
<p>You will have one more configuration step to complete which is detailed here: <a href="#using-devise-with-hyperloop">Using Devise with Hyperloop</a></p>
<h3 id="adding-devise-or-hyperloop-to-an-existing-app">Adding Devise or Hyperloop to an existing app</h3>
<p>If you are adding Devise or Hyperloop to an existing app, please ensure that you have completed the following steps:</p>
<ul>
<li>You have a Rails app with a User model</li>
<li>Hyperloop is installed and rendering Components</li>
<li>Hyperloop's transport is configured so data can move from the server to the client</li>
<li>You have moved your User model to the <code>hyperloop/models</code> folder, so that is accessible to both the client and server code</li>
<li>Devise is installed, and you have run the generator for Devise</li>
<li>Devise is protecting all our Rail controllers through a <code>before_action</code> on your ApplicationController</li>
</ul>
<h2 id="using-devise-with-hyperloop">Using Devise with Hyperloop</h2>
<h3 id="keeping-devise-code-on-the-server">Keeping Devise code on the server</h3>
<p><strong>There is one last configuration step we need to perform. Devise has added code to our User model which we want on the server but not on the client.</strong></p>
<p>To achieve this we simply wrap the <code>devise</code> macro in an <code>unless RUBY_ENGINE == 'opal'</code> test:</p>
<pre class="highlight ruby"><code><span class="c1"># app/hyperloop/models/user.rb</span>
<span class="k">class</span> <span class="nc">User</span> <span class="o"><</span> <span class="no">ApplicationRecord</span>
<span class="n">devise</span> <span class="ss">:database_authenticatable</span><span class="p">,</span> <span class="ss">:registerable</span><span class="p">,</span>
<span class="ss">:recoverable</span><span class="p">,</span> <span class="ss">:rememberable</span><span class="p">,</span> <span class="ss">:trackable</span><span class="p">,</span> <span class="ss">:validatable</span> <span class="k">unless</span> <span class="no">RUBY_ENGINE</span> <span class="o">==</span> <span class="s1">'opal'</span>
<span class="k">end</span>
</code></pre>
<h3 id="hyperloop-and-devise-co-existing">Hyperloop and Devise co-existing</h3>
<p>If you restart your Rails server and navigate to <code>http://localhost:3000/</code> you should be redirected to a Signup page.</p>
<p>Complete the signup, and you should be redirected to Hyperloop route which renders our Helloworld component. You should see this in your browser:</p>
<pre class="highlight plaintext"><code>Welcome! You have signed up successfully.
Helloworld
</code></pre>
<p>This proves that both Devise and Hyperloop are working properly, so our next step is to link them together.</p>
<h3 id="linking-hyperloop-and-devise">Linking Hyperloop and Devise</h3>
<p>Devise makes it easy to access the currently logged in User through a <code>current_user</code> helper which is available (servers-side) in your Rails Controllers and Views.</p>
<p>To get this information client-side, we need to link Hyperloop's (server-side) <code>acting_user</code> with Devise's <code>current_user</code> by creating an <code>acting_user</code> method on our <code>ApplicationController</code> which aliases Devise's <code>current_user</code>:</p>
<pre class="highlight ruby"><code><span class="c1"># app/controllers/application_controller.rb</span>
<span class="k">def</span> <span class="nf">acting_user</span>
<span class="n">current_user</span>
<span class="k">end</span>
</code></pre>
<p>Hyperloop will then call <code>ApplicationController::acting_user</code>, and set <code>Hyperloop::Application.acting_user_id</code> to the ID of <code>current_user</code>.</p>
<p><code>Hyperloop::Application.acting_user_id</code> will then be available to your server and client-side code.</p>
<h3 id="accessing-user-current-on-the-client">Accessing <code>User.current</code> on the client</h3>
<p>Let's update our <code>Helloworld</code> Component so that it renders the current users' email.</p>
<p>To keep the concept of <code>Hyperloop::Application.acting_user_id</code> out of our client-side code, we will add a class method to <code>User</code> which will find and return the <code>current_user</code> if one exists or an empty User object if no user is current:</p>
<pre class="highlight ruby"><code><span class="c1"># app/hyperloop/models/user.rb</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">current</span>
<span class="no">Hyperloop</span><span class="o">::</span><span class="no">Application</span><span class="p">.</span><span class="nf">acting_user_id</span> <span class="p">?</span> <span class="n">find</span><span class="p">(</span><span class="no">Hyperloop</span><span class="o">::</span><span class="no">Application</span><span class="p">.</span><span class="nf">acting_user_id</span><span class="p">)</span> <span class="p">:</span> <span class="no">User</span><span class="p">.</span><span class="nf">new</span>
<span class="k">end</span>
</code></pre>
<p>Now to access out <code>current_user</code> object we simply say <code>User.current</code>, so in the Helloworld Component to render the current user's email:</p>
<pre class="highlight ruby"><code><span class="c1"># app/hyperloop/components/helloworld.rb</span>
<span class="no">DIV</span> <span class="k">do</span>
<span class="no">H1</span> <span class="p">{</span><span class="s2">"Helloworld - </span><span class="si">#{</span><span class="no">User</span><span class="p">.</span><span class="nf">current</span><span class="p">.</span><span class="nf">email</span><span class="si">}</span><span class="s2">"</span><span class="p">}</span>
<span class="k">end</span>
</code></pre>
</div>
</div>
</div>
<div class="page-footer-upperline">
<div class="container">
</div>
</div>
<footer class="nav-footer">
<section class="sitemap">
<a href="/" class="nav-home"></a>
<div>
<h6><a href="/start/components" class="hyperloop-white">Start</a></h6>
<a href="/start/components">Components</a>
<a href="/start/stores">Stores</a>
<a href="/start/models">Models</a>
<a href="/start/operations">Operations</a>
<a href="/start/policies">Policies</a>
<a href="/start/pragmatic">Pragmatic Thinking</a>
</div>
<div>
<h6><a href="/tutorials" class="hyperloop-white">Tutorials</a></h6>
<a href="/tutorials/hyperloopcomps">Hyperloop COMPS</a>
<a href="/tutorials/hyperloopjs">Hyperloop.js</a>
<a href="/tutorials/hyperlooprails">Hyperloop and Rails</a>
<a href="/tutorials/hyperloopdeploy">Hyperloop deployment</a>
<a href="/tutorials/opal">Opal</a>
<a href="/tutorials/videos">Videos</a>
</div>
<div>
<h6><a href="/installation" class="hyperloop-white">Installation</a></h6>
<a href="/installation#opal-playground">Hyperloop.js</a>
<a href="/installation#ror">With Ruby On Rails</a>
<a href="/installation#with-sinatra">With Sinatra</a>
<a href="/installation#deployment">Deployment</a>
</div>
<div>
<h6><a href="/gems" class="hyperloop-white">Gems</a></h6>
<a href="https://github.com/ruby-hyperloop/hyperloop">hyperloop</a>
<a href="https://github.com/ruby-hyperloop/hyperloop-js">hyperloop-js</a>
<a href="https://github.com/ruby-hyperloop/hyper-react">hyper-component</a>
<a href="https://github.com/ruby-hyperloop/hyper-store">hyper-store</a>
<a href="https://github.com/ruby-hyperloop/hyper-mesh">hyper-model</a>
<a href="https://github.com/ruby-hyperloop/hyper-operation">hyper-operation</a>
<a href="https://github.com/ruby-hyperloop/reactrb-router/tree/v2-4-0">hyper-router</a>
<a href="https://github.com/ruby-hyperloop/hyper-spec">hyper-spec</a>
<a href="https://github.com/ruby-hyperloop/hyper-trace">hyper-trace</a>
</div>
<div>
<h6><a href="/tools" class="hyperloop-white">Tools</a></h6>
<a href="#testing">Testing</a>
<a href="#tools">Tools</a>
<a href="#debugging">Debugging</a>
</div>
<div>
<h6><a href="/docs/architecture" class="hyperloop-white">Docs</a></h6>
<a href="/docs/architecture">Architecture</a>
<a href="/docs/components/docs#components-dsl-overview">Components</a>
<a href="/docs/stores/docs">Stores</a>
<a href="/docs/models/docs">Models</a>
<a href="/docs/operations/docs">Operations</a>
<a href="/docs/policies/docs">Policies</a>
</div>
</section>
<section class="otherlinks">
<a href="https://github.com/ruby-hyperloop" class="hyperloop-white">Github</a>
<a href="/support" class="hyperloop-white">Help</a>
<a href="/blog" class="hyperloop-white">Blog</a>
</section>
<section class="copyright">
Copyright © 2018 Hyperloop.
</section>
</footer>
<script>
(function(Opal) {
var self = Opal.top, $scope = Opal, nil = Opal.nil, $breaker = Opal.breaker, $slice = Opal.slice;
Opal.add_stubs(['$puts']);
return self.$puts("Opal ok")
})(Opal);
</script>
<!-- <script src="http://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script> -->
<script type="text/ruby">puts "Hyperloop JS ok"</script>
</body>
</html>