Project

General

Profile

Hacking Workbench » History » Version 1

Tom Clegg, 04/17/2014 04:14 PM

1 1 Tom Clegg
h1. Hacking Workbench
2
3
{{toc}}
4
5
h2. Source tree layout
6
7
Everything is in @/apps/workbench@.
8
9
Key pieces to know about before going much further:
10
11
|/|Usual Rails project layout|
12
|/app/controllers/application_controller.rb|Controller superclass with authentication setup, error handling, and generic CRUD actions|
13
|/app/controllers/*.rb|Actions other than generic CRUD (users#activity, jobs#generate_provenance, ...)|
14
|/app/models/arvados_base.rb|Default Arvados model behavior and ActiveRecord-like accessors and introspection features|
15
|/app/models/arvados_resource_list.rb|ActiveRelation-like class (what you get from Model.where() etc.)|
16
17
h2. Unlike a typical Rails project...
18
19
* Most responses are JSON. Very few HTML views. We don't normally talk to browsers, except during authentication.
20
* We assign UUID strings (see lib/assign_uuid.rb and app/models/arvados_model.rb)
21
* The Arvados query API is fairly limited and doesn't accept SQL statements, so Workbench has to work harder to get what it needs. (Things will improve when Arvados accepts SPARQL queries.)
22
* Workbench itself only has the privileges of the Workbench user: when making Arvados API calls, it uses the API token provided by the user.
23
24
h2. Unlike what you might expect...
25
26
* Workbench doesn't use the Ruby SDK. It uses a sort of baked-in Rails SDK.
27
** TODO: move it out of Workbench into a gem.
28
** TODO: use the Ruby SDK under the hood.
29
30
h2. Running in development mode
31
32
You can get started quickly with SSL by generating a self-signed certificate
33
34
Alternatively if you're on debian and have root access you can make a copy that a regular user can use:
35
36
 openssl req -new -x509 -nodes -out ~/self-signed.pem -keyout ~/self-signed.key -days 3650 -subj '/CN=arvados.example.com'
37
38
Alternatively, download a set from the bottom of the [[API server]] page.
39
40
Save something like the following at @~/bin/workbench@, make it executable, make sure ~/bin is in your path:
41
42
 #!/bin/sh
43
set -e
44
cd ~/arvados/apps/workbench
45
export RAILS_ENV=development
46
rvm-exec 2.0.0 bundle install --deployment
47
exec rvm-exec 2.0.0 bundle exec passenger start -p 3001 --ssl --ssl-certificate ~/self-signed.pem --ssl-certificate-key ~/self-signed.key
48
49
The first time you run the above it will take a while to install all the ruby gems. In particular @Installing nokogiri@ takes a while
50
51
Once you see:
52
53
 =============== Phusion Passenger Standalone web server started ===============
54
55
You can visit your server at:
56
57
 @https://{ip-or-host}:3001/@
58
59
h2. Loading state from API into models
60
61
If your model makes an API call that returns the new state of an object, load the new attributes into the local model with @private_reload@:
62
63
<pre><code class="ruby">
64
  api_response = $arvados_api_client.api(...)
65
  private_reload api_response
66
</code></pre>
67
68
h2. Features
69
70
h3. Authentication
71
72
ApplicationController uses an around_filter to make sure the user is logged in, redirect to Arvados to complete the login procedure if not, and store the user's API token in Thread.current[:arvados_api_token] if so.
73
74
The @current_user@ helper returns User.current if the user is logged in, otherwise nil. (Generally, only special pages like "welcome" and "error" get displayed to users who aren't logged in.)
75
76
h3. Default filter behavior
77
78
@before_filter :find_object_by_uuid@
79
80
* This is enabled by default, @except :index, :create@.
81
* It renames the @:id@ param to @:uuid@. (The Rails default routing rules use @:id@ to accept params in path components, but @params[:uuid]@ makes more sense everywhere else in our code.)
82
* If you define a collection method (where there's no point looking up an object with the :id supplied in the request), skip this.
83
84
<pre><code class="ruby">
85
  skip_before_filter :find_object_by_uuid, only: [:action_that_takes_no_uuid_param]
86
</code></pre>
87
88
h3. Error handling
89
90
ApplicationController has a render_error method that shows a standard error page. (It's not very good, but it's better than a default Rails stack trace.)
91
92
In a controller you get there like this
93
94
<pre><code class="ruby">
95
  @errors = ['I could not achieve what you wanted.']
96
  render_error status: 500
97
</code></pre>
98
99
You can also do this, anywhere
100
101
<pre><code class="ruby">
102
  raise 'My spoon is too big.'
103
</code></pre>
104
105
The @render_error@ method sends JSON or HTML to the client according to the Accept header in the request (it sends JSON if JavaScript was requested), so reasonable things happen whether or not the request is AJAX.
106
107
h2. Development patterns
108
109
h3. Add a model
110
111
Currently, when the API provides a new model, we need to generate a corresponding model in Workbench: it's not smart enough to pick up the list of models from the API server's discovery document.
112
113
_(Need to fill in details here)_
114
# @rails generate model ....@
115
# Delete migration
116
# Change base class
117
# (probably more steps to fill in here)
118
119
Model _attributes_, on the other hand, are populated automatically.
120
121
h3. Add a configuration knob
122
123
Same situation as API server. See [[Hacking API Server]].
124
125
h3. Add an API method
126
127
Workbench is not yet smart enough to look in the discovery document for supported API methods. You need to add a method to the appropriate model class before you can use it in the Workbench app.
128
129
h3. Writing tests
130
131
(TODO)
132
133
h3. AJAX using Rails UJS (remote:true with JavaScript response)
134
135
This pattern is the best way to make a button/link that invokes an asynchronous action on the Workbench server side, i.e., before/without navigating away from the current page.
136
137
# Add <code class="ruby">remote: true</code> to a link or button. This makes Rails put a <code class="html">data-remote="true"</code> attribute in the HTML element. Say, in @app/views/fizz_buzzes/index.html.erb@:
138
<pre><code class="ruby">
139
<%= link_to "Blurfl", blurfl_fizz_buzz_url(id: @object.uuid), {class: 'btn btn-primary', remote: true} %>
140
</code></pre>
141
# Ensure the targeted action responds appropriately to both "js" and "html" requests. At minimum:
142
<pre><code class="ruby">
143
class FizzBuzzesController
144
  #...
145
  def blurfl
146
    @howmany = 1
147
    #...
148
    respond_to do |format|
149
      format.js
150
      format.html
151
    end
152
  end
153
end
154
</code></pre>
155
# The @html@ view is used if this is a normal page load (presumably this means the client has turned off JS).
156
#* @app/views/fizz_buzz/blurfl.html.erb@
157
<pre><code>
158
<p>I am <%= @howmany %></p>
159
</code></pre>
160
# The @js@ view is used if this is an AJAX request. It renders as JavaScript code which will be executed in the browser. Say, in @app/views/fizz_buzz/blurfl.js.erb@:
161
<pre><code class="javascript">
162
window.alert('I am <%= @howmany %>');
163
</code></pre>
164
# The browser opens an alert box:
165
<pre>
166
I am 1
167
</pre>
168
# A common task is to render a partial and use it to update part of the page. Say the partial is in @app/views/fizz_buzz/_latest_news.html.erb@:
169
<pre><code class="javascript">
170
var new_content = "<%= escape_javascript(render partial: 'latest_news') %>";
171
if ($('div#latest-news').html() != new_content)
172
   $('div#latest-news').html(new_content);
173
</code></pre>
174
175
*TODO: error handling*
176
177
h3. AJAX invoked from custom JavaScript (JSON response)
178
179
(and error handling)
180
181
h3. Add JavaScript triggers and fancy behavior
182
183
Some guidelines for implementing stuff nicely in JavaScript:
184
* Don't rely on the DOM being loaded before your script is loaded.
185
** If you need to inspect/alter the DOM as soon as it's loaded, make a setup function that fires on "document ready" and "ajax:complete".
186
** jQuery's delegated event pattern can help keep your code clean. See http://api.jquery.com/on/
187
<pre><code class="javascript">
188
// worse:
189
$('table.fizzbuzzer tr').
190
    on('mouseover', function(e, xhr) {
191
        console.log("This only works if the table exists when this setup script is executed.");
192
    });
193
// better:
194
$(document).
195
    on('mouseover', 'table.fizzbuzzer tr', function(e, xhr) {
196
        console.log("This works even if the table appears (or has the fizzbuzzer class added) later.");
197
    });
198
</code></pre>
199
200
* If your code really only makes sense for a particular view, rather than embedding @<script>@ tags in the middle of the page,
201
** use this:
202
<pre><code class="ruby">
203
<% content_for :js do %>
204
console.log("hurray, this goes in HEAD");
205
<% end %>
206
</code></pre>
207
** or, if your code should run after [most of] the DOM is loaded:
208
<pre><code class="ruby">
209
<% content_for :footer_js do %>
210
console.log("hurray, this runs at the bottom of the BODY element in the default layout.");
211
<% end %>
212
</code></pre>
213
214
* Don't just write JavaScript on the @fizz_buzzes/blurfl@ page and rely on the fact that the only @table@ element on the page is the one you want to attach your special behavior to. Instead, add a class to the table, and use a jQuery selector to attach special behavior to it.
215
** In @app/views/fizz_buzzes/blurfl.html.erb@
216
<pre>
217
<table class="fizzbuzzer">
218
 <tr>
219
  <td>fizz</td><td>buzz</td>
220
 </tr>
221
</table>
222
</pre>
223
** In @app/assets/javascripts/fizz_buzzes.js@
224
<pre><code class="javascript">
225
<% content_for :js do %>
226
$(document).on('mouseover', 'table.fizzbuzzer tr', function() {
227
    console.log('buzz');
228
});
229
<% end %>
230
</code></pre>
231
** Advantage: You can reuse the special behavior in other tables/pages/classes
232
** Advantage: The JavaScript can get compiled, minified, cached in the browser, etc., instead of being rendered with every page view
233
** Advantage: The JavaScript code is available regardless of how the content got into the DOM (regular page view, partial update with AJAX)
234
235
h3. Invoking selected-things picker
236
237
(TODO)
238
239
h3. Tabs/panes on index & show pages
240
241
(TODO)
242
243
h3. User notifications
244
245
(TODO)
246
247
h3. Customizing breadcrumbs
248
249
(TODO)
250
251
h3. Making a page accessible before login
252
253
(TODO)
254
255
h3. Making a page accessible to non-active users
256
257
(TODO)