guide 0.4.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b1880a99ac7ddb8b33051eb45a4f02096a0affe9
4
- data.tar.gz: 477329ba3897fdb49a3c25393e92b3413e71930f
2
+ SHA256:
3
+ metadata.gz: e16bc443fc8ef63b16a46d8ccdfde8659f7d6fc6285539e402af99d140f84571
4
+ data.tar.gz: 38f412d0da66483df051b5e6a8be761f7748e08dbf2f2944beba96687ce54b40
5
5
  SHA512:
6
- metadata.gz: 647ec8841fbb76fc0df14ce1b90054d45c99a9abf1a6faa84386996b1dfd62627132e3e1154e1fd426c5a576865e1e489b54a0e3d8443b70ef55d5d201e9070d
7
- data.tar.gz: '0962edb4f391940861098758494021c1df6f5cc269192c0461922d348c5404a3a8dcbb91c48312b8eead87a4dd68766d2cd8caa8a5092f99402e2702fc90f715'
6
+ metadata.gz: 958c597a15af220c11de53c8f9e3edc73d5a09676ba685aef95262d98dd667e2161c101bfb617db7c91e7f6776bcf9d0967bb0f93e1856d7fa07f2b7b98169b8
7
+ data.tar.gz: 298143a1c3dfb085d017a63fe3a0454b722abc5347a370c283a03af5b772d9e4b29f90a9f7bd6d06bf49f7c352eb77c52bfd5621dae70f36e0a67f07d632affc
data/README.md ADDED
@@ -0,0 +1,440 @@
1
+ # Guide gem
2
+ Document your entire user interface, not just your styles.
3
+
4
+ # Features
5
+
6
+ - Create a component library using real templates from your application
7
+ - Organise your components into a dynamic tree structure
8
+ - Fake out your backend at the view model layer
9
+ - See what each template looks like when you vary the data it receives
10
+
11
+ ## Development Status [![tests](https://github.com/envato/guide/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/envato/guide/actions/workflows/test.yml)
12
+ Guide runs in production at [Envato](https://market.styleguide.envato.com). While it is moderately mature at this stage, its API and features are still subject to changes.
13
+
14
+ ## Installation
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'guide'
19
+ ```
20
+
21
+ And then execute:
22
+ ```shell
23
+ $ bundle install
24
+ ```
25
+
26
+ ## Configuration
27
+ Add `/config/initializers/guide.rb`
28
+
29
+ ```Ruby
30
+ # Available options with example usage
31
+
32
+ Guide.configure do |config|
33
+ config.asset_path_for_logo = "guide/aweosme-guide-logo.svg"
34
+ config.company_name = "Your Awesome Company"
35
+ config.controller_class_to_inherit = "Guide::ControllerInjection"
36
+ config.default_stylesheets_for_structures = ['application/core/index', 'application/pages/default/index']
37
+ config.guide_name = "Your Awesome Guide"
38
+ config.helper_module_to_globally_include = 'Guide::HelperInjection'
39
+ config.local_variable_for_view_model = :view_model
40
+ config.supported_locales = {
41
+ "English" => "en",
42
+ "Portuguese" => "pt",
43
+ "Spanish" => "es",
44
+ }
45
+ end
46
+ ```
47
+
48
+ **Note:** If you are having asset compiling issues you may need to add Guide's assets to your asset precompile config e.g.
49
+
50
+ ```Ruby
51
+ # config/application.rb
52
+
53
+ config.assets.precompile += [
54
+ 'guide/application.js',
55
+ 'guide/scenario.js',
56
+ 'guide/application.css',
57
+ 'guide/scenario.css'
58
+ ]
59
+ ```
60
+
61
+
62
+ ## Build your Guide(s)
63
+
64
+ ### Architecture & Navigation
65
+
66
+ Each Guide is essentially a tree of nodes with pages that are either a `Document` or a `Structure`.
67
+
68
+ - Use `Node` for organising things in the tree without adding pages
69
+ - Use `Document` for static pages
70
+ - Use `Structure` for dynamic pages with scenarios. Each `Structure` represents a template in your application.
71
+
72
+ `content.rb` is the root node of that tree and defines your top level navigation structure.
73
+ To add child nodes, use the following DSL:
74
+
75
+ `contains :child_node_name`
76
+
77
+ This example declares that the tree contains a child node named:
78
+
79
+ `Guide::Content::ChildNodeName`
80
+
81
+ You will need to create a class for it.
82
+
83
+ All of your content should live in the `Guide::Content` namespace so that Guide can find it easily.
84
+
85
+ Feel free to redeclare the base node class in your system at the following path:
86
+
87
+ `app/<whatever_you_want>/guide/content.rb`
88
+
89
+ It needs to be a `Document` or a `Structure`, otherwise your Guide will not have a working homepage.
90
+
91
+ The convention for the subdirectory in app/ is `documentation`,
92
+ but if you don't like that, you can use something else.
93
+
94
+ We don't recommend putting it inside the standard rails directories though.
95
+
96
+ Here's an example of what `app/documentation/guide/content.rb` might look like:
97
+
98
+ ``` Ruby
99
+ # app/documentation/guide/content.rb
100
+
101
+ class Guide::Content < Guide::Document
102
+ contains :structures
103
+ contains :ui_library
104
+ contains :branding
105
+ end
106
+ ```
107
+
108
+ To specify options such as visibility, append them to the declaration:
109
+
110
+ ```Ruby
111
+ contains :structures # no visbility specified = public
112
+ contains :ui_library, :visibility => :unpublished
113
+ contains :branding, :visibility => :restricted
114
+ ```
115
+
116
+ ### Node
117
+ > A node is a point on the content tree. Everything in the content folder is a node.
118
+
119
+ ```Ruby
120
+ # app/documentation/guide/ui_library/typography
121
+
122
+ class Guide::Content::UILibrary::Typography < Guide::Node
123
+ contains :body
124
+ contains :currency
125
+ contains :heading
126
+ contains :new_experiment, :visibility => :unpublished
127
+ contains :link
128
+ contains :list
129
+ contains :preformatted
130
+ end
131
+ ```
132
+
133
+ ### Document
134
+ > This type of node has no scenarios, but is still renderable. It corresponds to a static template, usually with the same name, in the same folder.
135
+
136
+ **Supported formats**
137
+ - `html`
138
+ - `text`
139
+
140
+ ```Ruby
141
+ # app/documentation/guide/content/ui_library/typography/heading.rb
142
+
143
+ class Guide::Content::UILibrary::Typography::Heading < Guide::Document
144
+ end
145
+ ```
146
+
147
+ `app/documentation/guide/content/ui_library/typography/heading.html`
148
+ ```HTML
149
+ <div><p>Whatever you like!</p></div>
150
+ ```
151
+
152
+ ### Structure
153
+ > This type of node can manage a list of Scenarios, so that we can render a piece of the UI as it would look in lots of different situations.
154
+
155
+ For more info on how to add Structures and Scenarios, see the [wiki](https://github.com/envato/guide/wiki/Adding-Structures)
156
+
157
+ ### Scenario
158
+ > Each Scenario represents a set of data passed into the template via its view model. These let us see what our templates look like under various conditions.
159
+
160
+ ### Homepage
161
+ The homepage of your Guide is a special snowflake. Edit the contents here:
162
+ `app/documentation/guide/_content.html.erb`
163
+
164
+
165
+ ## Advanced setup
166
+ ### Linking
167
+
168
+ In order to link to other Guide pages from within your content, you will need to use the `node_path` url helper. Here are a couple of examples:
169
+
170
+ ```Ruby
171
+ <%= link_to "root level link", guide.node_path('documents') %>
172
+ <%= link_to "nested link", guide.node_path('documents/restricted') %>
173
+ ```
174
+
175
+ ### Fixtures
176
+ Fixtures are reusable data for your Structures. They can be defined once and then reused across multiple structures and scenarios.
177
+
178
+ Tip: Try to organise your fixtures in a similar way to your real view models.
179
+
180
+ Guide defines the `Guide::Fixtures` module, so if your put your fixtures in `app/documentation/guide/fixtures/` they should be autoloaded correctly.
181
+
182
+ ```Ruby
183
+ # app/documentation/guide/fixtures/common.rb
184
+
185
+ class Guide::Fixtures::Common < Guide::Fixture
186
+ def self.alert_box_view_model(options = {})
187
+ Guide::ViewModel.new(
188
+ {
189
+ :type => :notice,
190
+ :message => "You need to set a message",
191
+ }, options
192
+ )
193
+ end
194
+ end
195
+ ```
196
+
197
+ ### Injections
198
+ Injections can be used when you need to supply code to help Guide work within the context of your application.
199
+
200
+
201
+ #### Controller base class
202
+
203
+ Guide allows you to push code directly into its controllers through dependency injection. You will need to do this to be able to use most of the other code injections that Guide supports, as you'll see a bit later on.
204
+
205
+ When configuring Guide, one of the options available to you is to specify a `controller_class_to_inherit`. This does what it says. Think of it like an ApplicationController, but specific to your Guide.
206
+
207
+ The class that you supply needs to be a Rails controller, so you'll need to inherit from `ActionController::Base`.
208
+
209
+ If you choose to use this feature, remember to use unique method names. It may be helpful to take a glance at the code for Guide's controllers to make sure that you haven't got any clashes.
210
+
211
+ ```Ruby
212
+ # Controller base class injection
213
+
214
+ Guide.configure do |config|
215
+ config.controller_class_to_inherit = "Guide::ControllerInjection"
216
+ end
217
+
218
+ class Guide::ControllerInjection < ActionController::Base
219
+ # Your code here
220
+ end
221
+ ```
222
+
223
+ #### Authentication system
224
+
225
+ Unless you're running a totally public Guide, you will need some way to determine who is viewing it so that you can choose what they are allowed to see.
226
+
227
+ Since the means of authentication varies across different applications, Guide allows you to pass in an authentication system instead of providing its own.
228
+
229
+ If you inject an authentication system, Guide will call the `#user_signed_in?`, `#url_for_sign_in` and `#url_for_sign_out` methods on it. It's recommended that you inherit from `Guide::DefaultAuthenticationSystem`.
230
+
231
+ Here's an example of how you might set this up:
232
+
233
+ ```Ruby
234
+ # Authentication system injection
235
+
236
+ class Guide::ControllerInjection < ActionController::Base
237
+ private
238
+
239
+ def authentication_system
240
+ Guide::AuthenticationSystemInjection.new(request)
241
+ end
242
+ end
243
+
244
+ class Guide::AuthenticationSystem < Guide::DefaultAuthenticationSystem
245
+ def initialize(request)
246
+ @request = request
247
+ end
248
+
249
+ def user_signed_in?
250
+ RealAuthenticationSystemForYourApplication.new(request).signed_in?
251
+ end
252
+
253
+ def url_for_sign_in
254
+ # Where do you want to send people when they click the 'sign in' link?
255
+ end
256
+
257
+ def url_for_sign_out
258
+ # This is where people will end up when they click 'sign out'
259
+ end
260
+ end
261
+ ```
262
+
263
+ #### Authorisation system
264
+
265
+ Once you've figured out who is looking at your Guide, you're ready to decide what they're allowed to see.
266
+
267
+ As with the authentication system, most applications have some way of determining this sort of thing. Instead of presuming to know how your system works, Guide lets you implement this yourself.
268
+
269
+ If you inject an authorisation system, Guide will call the `#allow?(action)`, `#user_is_privileged?` and `#valid_visibility_options` methods on it. Inheriting from `Guide::DefaultAuthorisatinSystem` will get you some sensible defaults for the latter two of these methods.
270
+
271
+ You can set this up however you like, as long as you hook into the controller via `#authorisation_system` and implement the `#allow?(action)` method. Here's an example of how you might do it:
272
+
273
+ ```Ruby
274
+ # Authorisation system injection
275
+
276
+ class Guide::ControllerInjection < ActionController::Base
277
+ private
278
+
279
+ def authorisation_system
280
+ Guide::AuthorisationSystemInjection.new(real_authorisation_system)
281
+ end
282
+
283
+ def real_authorisation_system
284
+ RealAuthorisationSystemForYourApplication.new(signed_in_user)
285
+ end
286
+ end
287
+
288
+ class Guide::AuthorisationSystemInjection < Guide::DefaultAuthorisationSystem
289
+ def initialize(real_system)
290
+ @real_system = real_system
291
+ end
292
+
293
+ def allow?(action)
294
+ if Rails.env.development?
295
+ true
296
+ else
297
+ @real_system.allow?(action)
298
+ end
299
+ end
300
+
301
+ # Optional methods for if you want to use custom visibility options
302
+
303
+ def user_is_privileged?
304
+ allow?(:view_guide_not_ready_yet) ||
305
+ allow?(:view_guide_top_secret_feature)
306
+ end
307
+
308
+ def valid_visibility_options
309
+ [
310
+ nil,
311
+ :not_ready_yet,
312
+ :restricted,
313
+ ]
314
+ end
315
+ end
316
+ ```
317
+
318
+ #### HTML Injection
319
+
320
+ If you would like to push some HTML directly onto every page in Guide, you can do so using an HTML injection.
321
+
322
+ A good example of HTML that you might like to inject is a stylesheet link tag:
323
+
324
+ ```Ruby
325
+ # HTML injection for a stylesheet link tag
326
+
327
+ class Guide::ControllerInjection
328
+ private
329
+
330
+ def html_injection
331
+ Guide::HtmlInjection.new.prepare_injection(view_context)
332
+ end
333
+ end
334
+
335
+ class Guide::HtmlInjection
336
+ include ActionView::Helpers::AssetTagHelper
337
+
338
+ def prepare_injection(view_context)
339
+ [
340
+ view_context.stylesheet_link_tag("application/core/index"),
341
+ ].join(" ")
342
+ end
343
+ end
344
+ ```
345
+
346
+ #### Helper Injection
347
+
348
+ While the Guide development team recommends the use of view models instead of helpers, sometimes you're working on an application that already relies on them. Guide allows you to inject helpers so that you can get started without first having to retire them all.
349
+
350
+ Here's how to do it:
351
+
352
+ ```Ruby
353
+ # Injecting helper modules
354
+
355
+ Guide.configure do |config|
356
+ config.helper_module_to_globally_include = "Guide::HelperInjection"
357
+ end
358
+
359
+ module Guide::HelperInjection
360
+ include ::RegretHelper
361
+ include ::SoonToBeRetiredHelper
362
+ end
363
+ ```
364
+
365
+ If you _really_ need to, you can add `include ::ApplicationHelper` to this list.
366
+
367
+ ## Browser tests
368
+ Guide does not come packaged with browser tests, but it's a great idea to write some. Here's an example of how you might create one:
369
+
370
+ ```ruby
371
+ content = Guide::Content.new
372
+ authorisation_system = Guide::DefaultAuthorisationSystem.new
373
+ bouncer = Guide::Bouncer.new(authorisation_system: authorisation_system)
374
+ cartographer = Guide::Cartographer.new(bouncer)
375
+
376
+ cartographer.draw_paths_to_visible_renderable_nodes(starting_node: content).each do |node_path, node_title|
377
+ aggregate_failures do
378
+ begin
379
+ visit Guide::Engine.routes.url_helpers.node_path(:node_path => node_path)
380
+ with_scope('.sg-header') do
381
+ expect(page).to have_content("<put something recognisable here>")
382
+ end
383
+ puts "Successfully visited #{node_title}"
384
+ rescue StandardError, RSpec::Expectations::ExpectationNotMetError => e
385
+ raise [
386
+ "Could not load the guide page for #{node_path},",
387
+ "To open this in your browser, visit <Root path to your guide>/#{node_path}",
388
+ "You can find the file for this at app/documentation/guide/content/#{node_path}.rb",
389
+ "Here's what I saw when I tried to go there:",
390
+ "#{e.message}",
391
+ "#{page.body.split('Full backtrace').first}",
392
+ ].join("\n\n")
393
+ end
394
+ end
395
+ end
396
+ ```
397
+
398
+ ### Consistency Specs
399
+ These specs ensure that your fake view models (Guide::ViewModel) in Guide have the same public interfaces as the real view models in your application
400
+ `spec/documentation/guide/content`
401
+
402
+ ### Step 3: Access your Guide(s)
403
+ When you mount the gem in your routes file, you can specify a route to mount it to. If you want it mounted at the root of your application, you'd use:
404
+
405
+ ```ruby
406
+ mount Guide::Engine => "/"
407
+ ```
408
+
409
+ Or if you want it at, say, `/guide/`, you could use:
410
+
411
+ ```ruby
412
+ mount Guide::Engine => '/guide/'
413
+ ```
414
+
415
+ Any routes defined by the Guide gem will be prefixed with the path you specify when you mount it.
416
+
417
+ ## Maintainer
418
+ - [Luke Arndt](https://github.com/lukearndt)
419
+
420
+ ## License
421
+ Guide uses the MIT license. See [LICENSE.txt](https://github.com/envato/guide/blob/master/LICENSE.txt) for details.
422
+
423
+ ## Contact
424
+ - [github project](https://github.com/envato/guide)
425
+ - Bug reports and feature requests are via [github issues](https://github.com/envato/guide/issues)
426
+
427
+ ## Code of conduct
428
+ We welcome contribution from everyone. Read more about it in
429
+ [`CODE_OF_CONDUCT.md`](https://github.com/envato/guide/blob/master/CODE_OF_CONDUCT.md)
430
+
431
+ ## Contributing
432
+ 1. Fork it ( http://github.com/envato/guide/fork )
433
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
434
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
435
+ 4. Push to the branch (`git push origin my-new-feature`)
436
+ 5. Create new Pull Request
437
+
438
+ For larger new features: do everything as above, but first also make contact with the project maintainers to be sure your change fits with the project direction and you won't be wasting effort going in the wrong direction.
439
+
440
+ Please see the [Wiki](https://github.com/envato/guide/wiki) for indepth instructions on developing and understanding the Guide gem.