Skip to content

Commit d4ca858

Browse files
committed
added collection docs
1 parent 69e0c8b commit d4ca858

File tree

1 file changed

+111
-8
lines changed

1 file changed

+111
-8
lines changed

docs/components/collection.md

Lines changed: 111 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ The `collection` component is designed:
99
- paginate the displayed instances without full page reload
1010
- order the displayed instances without full page reload
1111

12-
The `collection` component should be as flexible as possible while still reducing the complexity of implementing all classic collection features by hand
12+
The `collection` component should be as flexible as possible while still reducing the complexity of implementing all classic collection features by hand.
1313

1414
## Prerequisites
1515

16-
We use a ActiveRecord Model in the following examples. This Model has two columns: `id` and `title`.
16+
We use an ActiveRecord Model in the following examples. This Model has two columns: `id` and `title`.
1717

1818
## Examples
1919

@@ -47,7 +47,12 @@ class Pages::MyApp::Collection < Matestack::Ui::Page
4747
heading size: 2, text: "My Collection"
4848

4949
partial :filter
50-
partial :content
50+
51+
# the content has to be wrapped in an `async` component
52+
# the event has to be "your_custom_collection_id" + "-update"
53+
async rerender_on: "my-first-collection-update" do
54+
partial :content
55+
end
5156
}
5257
end
5358

@@ -68,15 +73,17 @@ class Pages::MyApp::Collection < Matestack::Ui::Page
6873
end
6974

7075
def content
71-
partial {
76+
partial {
7277
collection_content @my_collection.config do
78+
7379
ul do
7480
@my_collection.data.each do |dummy|
7581
li do
7682
plain dummy.title
7783
end
7884
end
7985
end
86+
8087
end
8188
}
8289
end
@@ -117,7 +124,11 @@ class Pages::MyApp::Collection < Matestack::Ui::Page
117124
heading size: 2, text: "My Collection"
118125

119126
partial :filter
120-
partial :content
127+
128+
async rerender_on: "my-first-collection-update" do
129+
partial :content
130+
partial :paginator #has to be placed within the `async` component!
131+
end
121132
}
122133
end
123134

@@ -140,6 +151,7 @@ class Pages::MyApp::Collection < Matestack::Ui::Page
140151
def content
141152
partial {
142153
collection_content @my_collection.config do
154+
143155
ul do
144156
# now we use paginated_data!
145157
@my_collection.paginated_data.each do |dummy|
@@ -148,7 +160,7 @@ class Pages::MyApp::Collection < Matestack::Ui::Page
148160
end
149161
end
150162
end
151-
partial :paginator #has to be placed within `collection_content`!
163+
152164
end
153165
}
154166
end
@@ -216,7 +228,11 @@ class Pages::MyApp::Collection < Matestack::Ui::Page
216228

217229
partial :filter
218230
partial :ordering
219-
partial :content
231+
232+
async rerender_on: "my-first-collection-update" do
233+
partial :content
234+
partial :paginator #has to be placed within the `async` component!
235+
end
220236
}
221237
end
222238

@@ -258,14 +274,15 @@ class Pages::MyApp::Collection < Matestack::Ui::Page
258274
def content
259275
partial {
260276
collection_content @my_collection.config do
277+
261278
ul do
262279
@my_collection.paginated_data.each do |dummy|
263280
li do
264281
plain dummy.title
265282
end
266283
end
267284
end
268-
partial :paginator
285+
269286
end
270287
}
271288
end
@@ -298,3 +315,89 @@ end
298315
```
299316

300317
### Action enriched collection
318+
319+
In this example, we want to display ALL instances of `DummyModel` and filter the collection by title using a text input. Additionally we want to delete an item of the list using the `action` component.
320+
321+
```ruby
322+
class Pages::MyApp::Collection < Matestack::Ui::Page
323+
324+
include Matestack::Ui::Core::Collection::Helper
325+
326+
def prepare
327+
my_collection_id = "my-first-collection"
328+
329+
current_filter = get_collection_filter(my_collection_id)
330+
331+
my_base_query = DummyModel.all
332+
333+
my_filtered_query = my_base_query
334+
.where("title LIKE ?", "%#{current_filter[:title]}%")
335+
336+
@my_collection = set_collection({
337+
id: my_collection_id,
338+
data: my_filtered_query
339+
})
340+
end
341+
342+
def response
343+
components {
344+
heading size: 2, text: "My Collection"
345+
346+
partial :filter
347+
348+
async rerender_on: "my-first-collection-update" do
349+
partial :content
350+
end
351+
}
352+
end
353+
354+
def filter
355+
partial {
356+
collection_filter @my_collection.config do
357+
358+
collection_filter_input key: :title, type: :text, placeholder: "Filter by Title"
359+
collection_filter_submit do
360+
button text: "filter"
361+
end
362+
collection_filter_reset do
363+
button text: "reset"
364+
end
365+
366+
end
367+
}
368+
end
369+
370+
def content
371+
partial {
372+
collection_content @my_collection.config do
373+
374+
ul do
375+
@my_collection.data.each do |dummy|
376+
li do
377+
plain dummy.title
378+
action my_action_config(dummy.id) do
379+
button text: "delete"
380+
end
381+
end
382+
end
383+
end
384+
385+
end
386+
}
387+
end
388+
389+
def my_action_config id
390+
{
391+
method: :delete,
392+
path: :my_delete_path,
393+
params:{
394+
id: id
395+
},
396+
success: {
397+
emit: "my-first-collection-update"
398+
}
399+
}
400+
end
401+
402+
end
403+
```

0 commit comments

Comments
 (0)