Skip to content

Commit d86e5d0

Browse files
authored
Merge pull request #17 from matestack/20210603_issue#11_fix_custom_rendering_per_column
enable slot usage per column, added specs and docs for row object …
2 parents 8fe1dd1 + d576115 commit d86e5d0

File tree

3 files changed

+250
-146
lines changed

3 files changed

+250
-146
lines changed

docs/api/components/smart_collection.md

Lines changed: 97 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,101 @@ 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 € accessed via row object',
205+
slot: method(:price_in_euro_column_slot) # slots ALWAYS get the whole row object passed in!
206+
}
207+
}
208+
}
209+
end
210+
211+
def price_in_euro_column_slot order
212+
bs_badge order.price_in_euro # or whatever you want to do with all kinds of components
213+
end
214+
```
215+
216+
### Custom collection rendering
123217

124218
```ruby
125219
def response
@@ -183,4 +277,3 @@ def product_delete_action_config product
183277
}
184278
end
185279
```
186-

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ 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)
74+
else
75+
plain cell_text(data, key, value)
76+
end
7377
end
7478
end
7579

@@ -81,8 +85,17 @@ def cell_class(value)
8185
end
8286

8387
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]
88+
if value.is_a?(Hash)
89+
if value[:attribute].present?
90+
text = data.instance_eval(value[:attribute].to_s)
91+
else
92+
text = data.instance_eval(key.to_s)
93+
end
94+
text = value[:format].call(text) if value[:format].present?
95+
else
96+
text = data.instance_eval(key.to_s)
97+
end
98+
8699
text
87100
end
88101

0 commit comments

Comments
 (0)