rafters 1.1.0 → 1.2.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
2
  SHA1:
3
- metadata.gz: ff71963c4638eaf225b8ec0cd997188ea1591a17
4
- data.tar.gz: 041941674c5c90f078d0d0ffb40a42f3cf1d1527
3
+ metadata.gz: 06d271740c2834640701158193c17c1054228825
4
+ data.tar.gz: c4cc3af9945fc8e0ece7e787f7ebbfd1fe5539ad
5
5
  SHA512:
6
- metadata.gz: 843512e43b3ef5da3a9ad3312e972f69270c5df1d4de8c45169bb9d99d74bb218ec0a106ef3a713cafefbd378a490fc85d072343bea8ce94d861aa718ebf5161
7
- data.tar.gz: 166c0648085d5e25a70e34c086dae9f21db86748091f21a371583783ff974bc5f8c96dcb24266d5e5914530cf5902d28b2c66fbe1cc150b36dcd248ae13e857f
6
+ metadata.gz: 89c94d519911b237cf4ed2d29fc5247a0cfd74d4dbd3339d0ea7dcf2a420920a117d161e053debe0e6dda6e192855e6643f3b14920126a137fd252caa4e54e4b
7
+ data.tar.gz: c86a9ab53bce55b978eea3134a08e7192a28fcec2bcfbcc8676a133bb977963944c499a6f5936d8fa933e7d9fbf1f202db85e0a23b29e83622d6b60fa31b5846
data/README.md CHANGED
@@ -45,17 +45,17 @@ When you build out a page using Rafters you're effectively breaking it down into
45
45
  To begin creating a new component, run the following generator:
46
46
 
47
47
  ```sh
48
- $ rails generate rafters:component [name]
48
+ $ rails generate rafters:component {name}
49
49
  ```
50
50
 
51
51
  This generator will create the following files:
52
52
 
53
53
  ```
54
- app/rafters/[name]
55
- app/rafters/[name]/[name]_component.rb
56
- app/rafters/[name]/assets/stylesheets/[name]_component.scss
57
- app/rafters/[name]/assets/javascripts/[name]_component.js.coffee
58
- app/rafters/[name]/views/[name]_component.html.erb
54
+ app/rafters/{name}
55
+ app/rafters/{name}/{name}_component.rb
56
+ app/rafters/{name}/assets/stylesheets/{name}_component.scss
57
+ app/rafters/{name}/assets/javascripts/{name}_component.js.coffee
58
+ app/rafters/{name}/views/{name}_component.html.erb
59
59
  ```
60
60
 
61
61
  The two most important files generated above are `app/rafters/[name]/[name]_component.rb` and `app/rafters/[name]/views/[name]_component.html.erb`, which are (respectively) our component controller and view.
@@ -184,6 +184,94 @@ defaults type: "comment", filter: "none"
184
184
  default :published, false
185
185
  ```
186
186
 
187
+ ## Creating a source option for a component
188
+
189
+ There are often times when a single, general component can be powered by multiple complex data sources. In order to do this Rafters includes to option to create "source" classes that belong to individual components. Each source should expose an interface that can be consumed by the component's template, and options that are set on the component level are accessible at the source level as well.
190
+
191
+ To create a source, run the following generator:
192
+
193
+ ```ruby
194
+ $ rails g rafters:source {component name} {source name}
195
+ ```
196
+
197
+ This generator will create a file named `{component name}_{source name}_source.rb` in the specified component's sources directory.
198
+
199
+ A source class is a simple Ruby class that includes the `Rafters::Source` module. You can do pretty much anything you want with source classes (including subclassing), as long as they all expose the same public interface methods. When a component is configured to use a specific source, the `#source` method in that component will return an instance of the specified source class.
200
+
201
+ Here's a basic example - a list-view component with one source:
202
+
203
+ The component itself:
204
+
205
+ ```ruby
206
+ class ListViewComponent
207
+ include Rafters::Component
208
+
209
+ attribute :items
210
+
211
+ private
212
+
213
+ def items
214
+ source.items
215
+ end
216
+ end
217
+ ```
218
+
219
+ A source for the component:
220
+
221
+ ```ruby
222
+ class ListViewCommentSource
223
+ include Rafters::Source
224
+
225
+ def items
226
+ @items ||= Comment.all
227
+ end
228
+ end
229
+ ```
230
+
231
+ The component's view template:
232
+
233
+ ```erb
234
+ <ul>
235
+ <% items.each do |item| %>
236
+ <li><%= item.body %></li>
237
+ <% end %>
238
+ </ul>
239
+ ```
240
+
241
+ To render the above component with the `ListViewCommentSource`:
242
+
243
+ ```erb
244
+ <div class="main">
245
+ <%= render_component :list_view, as: "comments-list", source: "ListViewCommentSource"
246
+ </div>
247
+ ```
248
+
249
+ If you want to add another source option for this component, simply create a new source class with the same interface:
250
+
251
+ ```ruby
252
+ class ListViewPostSource
253
+ include Rafters::Source
254
+
255
+ def items
256
+ @items ||= Post.where({
257
+ category_id: settings.category_id
258
+ }).all
259
+ end
260
+ end
261
+ ```
262
+
263
+ And render the component with the new source (and any settings that it may require):
264
+
265
+ ```erb
266
+ <div class="main">
267
+ <%= render_component :list_view, as: "posts-list", source: "ListViewPostSource", {
268
+ category_id: @category.id
269
+ } %>
270
+ </div>
271
+ ```
272
+
273
+ The `Rafters::Component#controller` and `Rafters::Component#settings` methods are both available within your source classes, and will return the same information that they would in the related component.
274
+
187
275
  ## Contributing
188
276
 
189
277
  1. Fork it
@@ -7,6 +7,7 @@ class Rafters::ComponentGenerator < Rails::Generators::NamedBase
7
7
  empty_directory "#{base_directory}/assets/javascripts"
8
8
  empty_directory "#{base_directory}/assets/stylesheets"
9
9
  empty_directory "#{base_directory}/assets/images"
10
+ empty_directory "#{base_directory}/sources"
10
11
  empty_directory "#{base_directory}/views"
11
12
  end
12
13
 
@@ -23,10 +24,6 @@ class Rafters::ComponentGenerator < Rails::Generators::NamedBase
23
24
 
24
25
  private
25
26
 
26
- def app_root
27
- Rails.root
28
- end
29
-
30
27
  def base_directory
31
28
  "app/components/#{file_name}"
32
29
  end
@@ -0,0 +1,26 @@
1
+ class Rafters::SourceGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path("../templates", __FILE__)
3
+ argument :source_name, type: :string
4
+
5
+ def create_directory
6
+ empty_directory "#{base_directory}"
7
+ end
8
+
9
+ def create_files
10
+ template "source.rb.erb", "#{base_directory}/#{source_file_name}_source.rb"
11
+ end
12
+
13
+ private
14
+
15
+ def base_directory
16
+ "app/components/#{file_name}/sources"
17
+ end
18
+
19
+ def source_file_name
20
+ source_name.underscore
21
+ end
22
+
23
+ def source_class_name
24
+ source_name.classify
25
+ end
26
+ end
@@ -0,0 +1,4 @@
1
+ class <%= source_class_name %>Source
2
+ include Rafters::Source
3
+
4
+ end
@@ -1,3 +1,3 @@
1
1
  module Rafters
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rafters
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Hite
@@ -173,6 +173,8 @@ files:
173
173
  - lib/generators/rafters/component/templates/views/component.html.erb
174
174
  - lib/generators/rafters/install/install_generator.rb
175
175
  - lib/generators/rafters/install/templates/initializer.rb
176
+ - lib/generators/rafters/source/source_generator.rb
177
+ - lib/generators/rafters/source/templates/source.rb.erb
176
178
  - lib/rafters.rb
177
179
  - lib/rafters/component.rb
178
180
  - lib/rafters/component_context.rb