Skip to content

Commit 84cddeb

Browse files
committed
enable slot usage per column, added specs and docs for row object access in columns
1 parent a14e988 commit 84cddeb

File tree

3 files changed

+259
-146
lines changed

3 files changed

+259
-146
lines changed

docs/api/components/smart_collection.md

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Docs in progress! Please review the examples.
1010

1111
## Examples
1212

13-
### Example 1: Table rendering with custom row actions
13+
### Table rendering with custom row actions
1414

1515
```ruby
1616
def response
@@ -72,7 +72,7 @@ def customer_delete_action_config customer
7272
end
7373
```
7474

75-
### Example 2: Table rendering with joined model and formatted col data rendering
75+
### Table rendering with joined model and formatted col data rendering
7676

7777
```ruby
7878
def response
@@ -119,7 +119,109 @@ def table_item_actions order
119119
end
120120
```
121121

122-
### Example 3: Custom collection rendering
122+
### Table rendering formatted col data rendering access the whole row instance object
123+
124+
```ruby
125+
def response
126+
#...
127+
bs_smart_collection collection_config
128+
#...
129+
end
130+
131+
def collection_config
132+
{
133+
id: 'orders',
134+
items: Order.all,
135+
paginate: 10,
136+
rerender_on: "success",
137+
columns: {
138+
self: {
139+
heading: 'Price in €',
140+
format: -> (order){ "#{order.price_in_euro}" },
141+
}
142+
}
143+
}
144+
end
145+
```
146+
147+
### Table rendering formatted col data rendering access the whole row instance object in multiple columns
148+
149+
```ruby
150+
def response
151+
#...
152+
bs_smart_collection collection_config
153+
#...
154+
end
155+
156+
def collection_config
157+
{
158+
id: 'orders',
159+
items: Order.all,
160+
paginate: 10,
161+
rerender_on: "success",
162+
# the columns hash can only have a key once, fix by specifying the attribute name
163+
columns: {
164+
price_in_euro: {
165+
heading: 'Price in €',
166+
format: -> (column_data){ "#{column_data}" },
167+
},
168+
price_in_euro_again: {
169+
attribute: :price_in_euro, # fix by specifying the attribute name
170+
heading: 'Price in €',
171+
format: -> (column_data){ "#{column_data}" },
172+
},
173+
self: {
174+
heading: 'Price in €',
175+
format: -> (order){ "#{order.price_in_euro}" },
176+
},
177+
self_again: {
178+
attribute: :self, # fix by specifying the attribute name
179+
heading: 'Price in €',
180+
format: -> (order){ "#{order.price_in_euro}" },
181+
}
182+
}
183+
}
184+
end
185+
```
186+
187+
### Table rendering via slot enabling flexible column content composition
188+
189+
```ruby
190+
def response
191+
#...
192+
bs_smart_collection collection_config
193+
#...
194+
end
195+
196+
def collection_config
197+
{
198+
id: 'orders',
199+
items: Order.all,
200+
paginate: 10,
201+
rerender_on: "success",
202+
columns: {
203+
price_in_euro: {
204+
heading: 'Price in €',
205+
slot: method(:price_in_euro_column_slot),
206+
},
207+
self: {
208+
heading: 'Price in € accessed via row object',
209+
slot: method(:whole_object_column_slot),
210+
}
211+
}
212+
}
213+
end
214+
215+
def price_in_euro_column_slot price_in_euro
216+
bs_badge price_in_euro # or whatever you want to do with all kinds of components
217+
end
218+
219+
def whole_object_column_slot order
220+
bs_badge order.price_in_euro # or whatever you want to do with all kinds of components
221+
end
222+
```
223+
224+
### Custom collection rendering
123225

124226
```ruby
125227
def response
@@ -183,4 +285,3 @@ def product_delete_action_config product
183285
}
184286
end
185287
```
186-

lib/matestack/ui/bootstrap/content/smart_collection/content.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ def table_footer
6969

7070
def cell(data, key, value)
7171
td class: cell_class(value) do
72-
plain cell_text(data, key, value)
72+
if value.is_a?(Hash) && value[:slot]
73+
value[:slot].call(data.instance_eval(key.to_s)) if value[:attribute].nil?
74+
value[:slot].call(data.instance_eval(value[:attribute].to_s)) if value[:attribute].present?
75+
else
76+
plain cell_text(data, key, value)
77+
end
7378
end
7479
end
7580

@@ -81,8 +86,17 @@ def cell_class(value)
8186
end
8287

8388
def cell_text(data, key, value)
84-
text = data.instance_eval(key.to_s)
85-
text = value[:format].call(text) if value.is_a?(Hash) && value[:format]
89+
if value.is_a?(Hash)
90+
if value[:attribute].present?
91+
text = data.instance_eval(value[:attribute].to_s)
92+
else
93+
text = data.instance_eval(key.to_s)
94+
end
95+
text = value[:format].call(text) if value[:format].present?
96+
else
97+
text = data.instance_eval(key.to_s)
98+
end
99+
86100
text
87101
end
88102

0 commit comments

Comments
 (0)