Skip to content

Commit 5b434ae

Browse files
committed
Add ability to rendeer specific row with specific columns. bogdan#314
1 parent b703d00 commit 5b434ae

File tree

6 files changed

+86
-71
lines changed

6 files changed

+86
-71
lines changed

lib/datagrid/columns.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,8 @@ def generic_value(column, model)
446446

447447
protected
448448

449-
def append_column_preload(scope)
450-
columns.inject(scope) do |current, column|
449+
def append_column_preload(relation)
450+
columns.inject(relation) do |current, column|
451451
column.append_preload(current)
452452
end
453453
end

lib/datagrid/columns/column.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,17 @@ def generic_value(model, grid)
133133
grid.generic_value(self, model)
134134
end
135135

136-
def append_preload(scope)
137-
return scope unless preload
136+
def append_preload(relation)
137+
return relation unless preload
138138
if preload.respond_to?(:call)
139-
return scope unless preload
139+
return relation unless preload
140140
if preload.arity == 1
141-
preload.call(scope)
141+
preload.call(relation)
142142
else
143-
scope.instance_exec(&preload)
143+
relation.instance_exec(&preload)
144144
end
145145
else
146-
driver.default_preload(scope, preload)
146+
driver.default_preload(relation, preload)
147147
end
148148
end
149149

lib/datagrid/helper.rb

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def datagrid_table(grid, assets = grid.assets, **options)
5252
#
5353
# * <tt>:order</tt> - display ordering controls built-in into header
5454
# Default: true
55+
# * <tt>:columns</tt> - Array of column names to display.
56+
# Used in case when same grid class is used in different places
57+
# and needs different columns. Default: all defined columns.
5558
# * <tt>:partials</tt> - Path for partials lookup.
5659
# Default: 'datagrid'.
5760
# @param grid [Datagrid] grid object
@@ -124,12 +127,10 @@ def datagrid_form_for(grid, options = {})
124127
# <% row = datagrid_row(grid, user) %>
125128
# First Name: <%= row.first_name %>
126129
# Last Name: <%= row.last_name %>
127-
def datagrid_row(grid, asset, &block)
128-
HtmlRow.new(self, grid, asset).tap do |row|
129-
if block_given?
130-
return capture(row, &block)
131-
end
132-
end
130+
# @example
131+
# <%= datagrid_row(grid, user, columns: [:first_name, :last_name, :actions]) %>
132+
def datagrid_row(grid, asset, **options, &block)
133+
datagrid_renderer.row(grid, asset, **options, &block)
133134
end
134135

135136
# Generates an ascending or descending order url for the given column
@@ -141,51 +142,6 @@ def datagrid_order_path(grid, column, descending)
141142
datagrid_renderer.order_path(grid, column, descending, request)
142143
end
143144

144-
# Represents a datagrid row that provides access to column values for the given asset
145-
# @example
146-
# row = datagrid_row(grid, user)
147-
# row.class # => Datagrid::Helper::HtmlRow
148-
# row.first_name # => "<strong>Bogdan</strong>"
149-
# row.grid # => Grid object
150-
# row.asset # => User object
151-
# row.each do |value|
152-
# puts value
153-
# end
154-
class HtmlRow
155-
156-
include Enumerable
157-
158-
attr_reader :grid, :asset
159-
160-
# @!visibility private
161-
def initialize(context, grid, asset)
162-
@context = context
163-
@grid = grid
164-
@asset = asset
165-
end
166-
167-
# @return [Object] a column value for given column name
168-
def get(column)
169-
@context.datagrid_value(@grid, column, @asset)
170-
end
171-
172-
# Iterates over all column values that are available in the row
173-
# param block [Proc] column value iterator
174-
def each(&block)
175-
@grid.columns.each do |column|
176-
block.call(get(column))
177-
end
178-
end
179-
180-
protected
181-
def method_missing(method, *args, &blk)
182-
if column = @grid.column_by_name(method)
183-
get(column)
184-
else
185-
super
186-
end
187-
end
188-
end
189145

190146
protected
191147

lib/datagrid/renderer.rb

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,20 @@ def header(grid, options = {})
5656

5757
def rows(grid, assets = grid.assets, **options, &block)
5858
result = assets.map do |asset|
59-
if block_given?
60-
@template.capture do
61-
yield(Datagrid::Helper::HtmlRow.new(@template, grid, asset))
62-
end
63-
else
64-
_render_partial( 'row', options[:partials], {
65-
:grid => grid,
66-
:options => options,
67-
:asset => asset
68-
})
69-
end
59+
row(grid, asset, **options, &block)
7060
end.to_a.join
7161

7262
_safe(result)
7363
end
7464

65+
def row(grid, asset, **options, &block)
66+
Datagrid::Helper::HtmlRow.new(self, grid, asset, options).tap do |row|
67+
if block_given?
68+
return @template.capture(row, &block)
69+
end
70+
end
71+
end
72+
7573
def order_for(grid, column, options = {})
7674
_render_partial('order_for', options[:partials],
7775
{ :grid => grid, :column => column })
@@ -99,4 +97,61 @@ def _render_partial(partial_name, partials_path, locals = {})
9997
})
10098
end
10199
end
100+
101+
module Helper
102+
# Represents a datagrid row that provides access to column values for the given asset
103+
# @example
104+
# row = datagrid_row(grid, user)
105+
# row.class # => Datagrid::Helper::HtmlRow
106+
# row.first_name # => "<strong>Bogdan</strong>"
107+
# row.grid # => Grid object
108+
# row.asset # => User object
109+
# row.each do |value|
110+
# puts value
111+
# end
112+
class HtmlRow
113+
114+
include Enumerable
115+
116+
attr_reader :grid, :asset, :options
117+
118+
# @!visibility private
119+
def initialize(renderer, grid, asset, options)
120+
@renderer = renderer
121+
@grid = grid
122+
@asset = asset
123+
@options = options
124+
end
125+
126+
# @return [Object] a column value for given column name
127+
def get(column)
128+
@renderer.format_value(@grid, column, @asset)
129+
end
130+
131+
# Iterates over all column values that are available in the row
132+
# param block [Proc] column value iterator
133+
def each(&block)
134+
(@options[:columns] || @grid.html_columns).each do |column|
135+
block.call(get(column))
136+
end
137+
end
138+
139+
def to_s
140+
@renderer.send(:_render_partial, 'row', options[:partials], {
141+
:grid => grid,
142+
:options => options,
143+
:asset => asset
144+
})
145+
end
146+
147+
protected
148+
def method_missing(method, *args, &blk)
149+
if column = @grid.column_by_name(method)
150+
get(column)
151+
else
152+
super
153+
end
154+
end
155+
end
156+
end
102157
end

spec/datagrid/helper_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,11 @@ def param_name
613613
expect(data_row.random2).to_not eq(html_row.random1)
614614
end
615615

616+
it "converts to string using columns option" do
617+
r = subject.datagrid_row(grid, entry, columns: [:name]).to_s
618+
expect(r).to match_css_pattern('tr td.name')
619+
expect(r).to_not match_css_pattern('tr td.category')
620+
end
616621
end
617622

618623
describe ".datagrid_value" do

spec/support/matchers.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ def matches?(text)
7171
expected_amount = amount_or_pattern_or_string_or_proc
7272
amount = path.size
7373
if amount != expected_amount
74-
puts text
7574
return error!("did not find #{css.inspect} #{expected_amount.inspect} times. It was #{amount.inspect}")
7675
end
7776
elsif amount_or_pattern_or_string_or_proc.is_a? Proc

0 commit comments

Comments
 (0)