datagrid 1.0.5 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/VERSION +1 -1
- data/datagrid.gemspec +5 -5
- data/lib/datagrid/columns.rb +64 -15
- data/lib/datagrid/columns/column.rb +23 -7
- data/lib/datagrid/core.rb +5 -5
- data/lib/datagrid/drivers/abstract_driver.rb +9 -1
- data/lib/datagrid/drivers/active_record.rb +17 -7
- data/lib/datagrid/drivers/array.rb +3 -3
- data/lib/datagrid/drivers/mongo_mapper.rb +1 -1
- data/lib/datagrid/drivers/mongoid.rb +1 -1
- data/lib/datagrid/filters.rb +8 -3
- data/lib/datagrid/filters/base_filter.rb +1 -1
- data/spec/datagrid/columns_spec.rb +65 -1
- data/spec/datagrid/drivers/active_record_spec.rb +6 -1
- data/spec/datagrid/filters/date_filter_spec.rb +20 -9
- data/spec/datagrid/form_builder_spec.rb +11 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89f8357208d93d460caef785e2ebaa7ebbb36280
|
4
|
+
data.tar.gz: 32723d7e2ca88ff415c6686d073672e78cb32742
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 111f4e212572c21a899ab428e0a4fea23b7a106bcec089508c36b68349384d76e6bdb94fe9ebf1045791a9b9b9f5f0a2423f328853bff0b5df5b47f0ec6c3626
|
7
|
+
data.tar.gz: d396415dd9f0d3743ed552fddccb1926e3737f7b5a0a7264414fa51a73abe9c8e43520e5e941a201d2093fd7ded8e16e8f67efc814cc2a36d732f736983bc820
|
data/Gemfile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.1.0
|
data/datagrid.gemspec
CHANGED
@@ -2,15 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: datagrid 1.0
|
5
|
+
# stub: datagrid 1.1.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "datagrid"
|
9
|
-
s.version = "1.0
|
9
|
+
s.version = "1.1.0"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib"]
|
12
13
|
s.authors = ["Bogdan Gusiev"]
|
13
|
-
s.date = "2014-
|
14
|
+
s.date = "2014-03-07"
|
14
15
|
s.description = "This allows you to easily build datagrid aka data tables with sortable columns and filters"
|
15
16
|
s.email = "agresso@gmail.com"
|
16
17
|
s.extra_rdoc_files = [
|
@@ -110,8 +111,7 @@ Gem::Specification.new do |s|
|
|
110
111
|
]
|
111
112
|
s.homepage = "http://github.com/bogdan/datagrid"
|
112
113
|
s.licenses = ["MIT"]
|
113
|
-
s.
|
114
|
-
s.rubygems_version = "2.1.10"
|
114
|
+
s.rubygems_version = "2.2.2"
|
115
115
|
s.summary = "Ruby gem to create datagrids"
|
116
116
|
|
117
117
|
if s.respond_to? :specification_version then
|
data/lib/datagrid/columns.rb
CHANGED
@@ -88,38 +88,46 @@ module Datagrid
|
|
88
88
|
end
|
89
89
|
|
90
90
|
# Defines new datagrid column
|
91
|
-
#
|
91
|
+
#
|
92
92
|
# Arguments:
|
93
93
|
#
|
94
94
|
# * <tt>name</tt> - column name
|
95
|
+
# * <tt>query</tt> - a string representing the query to select this column (supports only ActiveRecord)
|
95
96
|
# * <tt>options</tt> - hash of options
|
96
97
|
# * <tt>block</tt> - proc to calculate a column value
|
97
98
|
#
|
98
99
|
# Available options:
|
99
|
-
#
|
100
|
+
#
|
100
101
|
# * <tt>:html</tt> - determines if current column should be present in html table and how is it formatted
|
101
|
-
# * <tt>:order</tt> - determines if this column could be sortable and how.
|
102
|
-
# The value of order is explicitly passed to ORM ordering method.
|
102
|
+
# * <tt>:order</tt> - determines if this column could be sortable and how.
|
103
|
+
# The value of order is explicitly passed to ORM ordering method.
|
103
104
|
# Ex: <tt>"created_at, id"</tt> for ActiveRecord, <tt>[:created_at, :id]</tt> for Mongoid
|
104
|
-
# * <tt>:order_desc</tt> - determines a descending order for given column
|
105
|
+
# * <tt>:order_desc</tt> - determines a descending order for given column
|
105
106
|
# (only in case when <tt>:order</tt> can not be easily reversed by ORM)
|
106
|
-
# * <tt>:order_by_value</tt> - used in case it is easier to perform ordering at ruby level not on database level.
|
107
|
-
# Warning: using ruby to order large datasets is very unrecommended.
|
107
|
+
# * <tt>:order_by_value</tt> - used in case it is easier to perform ordering at ruby level not on database level.
|
108
|
+
# Warning: using ruby to order large datasets is very unrecommended.
|
108
109
|
# If set to true - datagrid will use column value to order by this column
|
109
110
|
# If block is given - datagrid will use value returned from block
|
110
111
|
# * <tt>:mandatory</tt> - if true, column will never be hidden with #column_names selection
|
111
112
|
# * <tt>:url</tt> - a proc with one argument, pass this option to easily convert the value into an URL
|
112
113
|
# * <tt>:before</tt> - determines the position of this column, by adding it before the column passed here
|
113
114
|
# * <tt>:after</tt> - determines the position of this column, by adding it after the column passed here
|
115
|
+
# * <tt>:if</tt> - the column is shown if the reult of calling this argument is true
|
116
|
+
# * <tt>:unless</tt> - the column is shown unless the reult of calling this argument is true
|
114
117
|
#
|
115
118
|
# See: https://github.com/bogdan/datagrid/wiki/Columns for examples
|
116
|
-
def column(name, options = {}, &block)
|
119
|
+
def column(name, options_or_query = {}, options = {}, &block)
|
120
|
+
if options_or_query.is_a?(String)
|
121
|
+
query = options_or_query
|
122
|
+
else
|
123
|
+
options = options_or_query
|
124
|
+
end
|
117
125
|
check_scope_defined!("Scope should be defined before columns")
|
118
126
|
block ||= lambda do |model|
|
119
127
|
model.send(name)
|
120
128
|
end
|
121
129
|
position = Datagrid::Utils.extract_position_from_options(columns_array, options)
|
122
|
-
column = Datagrid::Columns::Column.new(self, name, default_column_options.merge(options), &block)
|
130
|
+
column = Datagrid::Columns::Column.new(self, name, query, default_column_options.merge(options), &block)
|
123
131
|
columns_array.insert(position, column)
|
124
132
|
end
|
125
133
|
|
@@ -148,7 +156,7 @@ module Datagrid
|
|
148
156
|
end
|
149
157
|
end
|
150
158
|
else
|
151
|
-
# Ruby Object#format exists.
|
159
|
+
# Ruby Object#format exists.
|
152
160
|
# We don't want to change the behaviour and overwrite it.
|
153
161
|
super
|
154
162
|
end
|
@@ -163,6 +171,10 @@ module Datagrid
|
|
163
171
|
|
164
172
|
module InstanceMethods
|
165
173
|
|
174
|
+
def assets
|
175
|
+
driver.append_column_queries(super, columns.map(&:query).compact)
|
176
|
+
end
|
177
|
+
|
166
178
|
# Returns <tt>Array</tt> of human readable column names. See also "Localization" section
|
167
179
|
#
|
168
180
|
# Arguments:
|
@@ -212,7 +224,7 @@ module Datagrid
|
|
212
224
|
self.rows(*column_names).unshift(self.header(*column_names))
|
213
225
|
end
|
214
226
|
|
215
|
-
# Return Array of Hashes where keys are column names and values are column values
|
227
|
+
# Return Array of Hashes where keys are column names and values are column values
|
216
228
|
# for each row in filtered datagrid relation.
|
217
229
|
#
|
218
230
|
# Example:
|
@@ -267,13 +279,13 @@ module Datagrid
|
|
267
279
|
# Returns all columns selected in grid instance
|
268
280
|
#
|
269
281
|
# Examples:
|
270
|
-
#
|
282
|
+
#
|
271
283
|
# MyGrid.new.columns # => all defined columns
|
272
284
|
# grid = MyGrid.new(:column_names => [:id, :name])
|
273
285
|
# grid.columns # => id and name columns
|
274
286
|
# grid.columns(:id, :category) # => id and category column
|
275
287
|
def columns(*args)
|
276
|
-
self.class.columns(*args)
|
288
|
+
self.class.columns(*args).select {|column| column.enabled?(self)}
|
277
289
|
end
|
278
290
|
|
279
291
|
# Returns all columns that can be represented in plain data(non-html) way
|
@@ -301,7 +313,7 @@ module Datagrid
|
|
301
313
|
# Gives ability to have a different formatting for CSV and HTML column value.
|
302
314
|
#
|
303
315
|
# Example:
|
304
|
-
#
|
316
|
+
#
|
305
317
|
# column(:name) do |model|
|
306
318
|
# format(model.name) do |value|
|
307
319
|
# content_tag(:strong, value)
|
@@ -309,7 +321,7 @@ module Datagrid
|
|
309
321
|
# end
|
310
322
|
#
|
311
323
|
# column(:company) do |model|
|
312
|
-
# format(model.company.name) do
|
324
|
+
# format(model.company.name) do
|
313
325
|
# render :partial => "company_with_logo", :locals => {:company => model.company }
|
314
326
|
# end
|
315
327
|
# end
|
@@ -317,10 +329,32 @@ module Datagrid
|
|
317
329
|
if block_given?
|
318
330
|
self.class.format(value, &block)
|
319
331
|
else
|
332
|
+
# don't override Object#format method
|
320
333
|
super
|
321
334
|
end
|
322
335
|
end
|
323
336
|
|
337
|
+
# Returns an object representing a table row.
|
338
|
+
# Allows to access column values
|
339
|
+
#
|
340
|
+
# Example:
|
341
|
+
#
|
342
|
+
# class MyGrid
|
343
|
+
# scope { User }
|
344
|
+
# column(:id)
|
345
|
+
# column(:name)
|
346
|
+
# column(:number_of_purchases) do |user|
|
347
|
+
# user.purchases.count
|
348
|
+
# end
|
349
|
+
# end
|
350
|
+
#
|
351
|
+
# row = MyGrid.new.data_row(User.last)
|
352
|
+
# row.id # => user.id
|
353
|
+
# row.number_of_purchases # => user.purchases.count
|
354
|
+
def data_row(asset)
|
355
|
+
::Datagrid::Columns::DataRow.new(self, asset)
|
356
|
+
end
|
357
|
+
|
324
358
|
protected
|
325
359
|
|
326
360
|
def map_with_batches(&block)
|
@@ -333,5 +367,20 @@ module Datagrid
|
|
333
367
|
|
334
368
|
end # InstanceMethods
|
335
369
|
|
370
|
+
class DataRow
|
371
|
+
|
372
|
+
def initialize(grid, model)
|
373
|
+
@grid = grid
|
374
|
+
@model = model
|
375
|
+
end
|
376
|
+
|
377
|
+
def method_missing(meth, *args, &blk)
|
378
|
+
if column = @grid.column_by_name(meth)
|
379
|
+
column.data_value(@model, @grid)
|
380
|
+
else
|
381
|
+
super
|
382
|
+
end
|
383
|
+
end
|
384
|
+
end
|
336
385
|
end
|
337
386
|
end
|
@@ -25,9 +25,9 @@ class Datagrid::Columns::Column
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
attr_accessor :grid_class, :options, :data_block, :name, :html_block
|
28
|
+
attr_accessor :grid_class, :options, :data_block, :name, :html_block, :query
|
29
29
|
|
30
|
-
def initialize(grid_class, name, options = {}, &block)
|
30
|
+
def initialize(grid_class, name, query, options = {}, &block)
|
31
31
|
self.grid_class = grid_class
|
32
32
|
self.name = name.to_sym
|
33
33
|
self.options = options
|
@@ -40,9 +40,13 @@ class Datagrid::Columns::Column
|
|
40
40
|
self.html_block = options[:html]
|
41
41
|
end
|
42
42
|
end
|
43
|
+
self.query = query
|
44
|
+
options[:if] = convert_option_to_proc(options[:if])
|
45
|
+
options[:unless] = convert_option_to_proc(options[:unless])
|
43
46
|
end
|
44
47
|
|
45
48
|
def data_value(model, grid)
|
49
|
+
raise "no data value for #{name} column" unless data?
|
46
50
|
result = generic_value(model,grid)
|
47
51
|
result.is_a?(ResponseFormat) ? result.data_value : result
|
48
52
|
end
|
@@ -53,7 +57,7 @@ class Datagrid::Columns::Column
|
|
53
57
|
end
|
54
58
|
|
55
59
|
def header
|
56
|
-
self.options[:header] ||
|
60
|
+
self.options[:header] ||
|
57
61
|
I18n.translate(self.name, :scope => "datagrid.#{self.grid_class.param_name}.columns", :default => self.name.to_s.humanize )
|
58
62
|
end
|
59
63
|
|
@@ -83,21 +87,25 @@ class Datagrid::Columns::Column
|
|
83
87
|
|
84
88
|
def order_desc
|
85
89
|
return nil unless order
|
86
|
-
self.options[:order_desc]
|
90
|
+
self.options[:order_desc]
|
87
91
|
end
|
88
92
|
|
89
93
|
def html?
|
90
94
|
options[:html] != false
|
91
95
|
end
|
92
|
-
|
96
|
+
|
93
97
|
def data?
|
94
98
|
self.data_block != nil
|
95
99
|
end
|
96
|
-
|
100
|
+
|
97
101
|
def mandatory?
|
98
102
|
!! options[:mandatory]
|
99
103
|
end
|
100
104
|
|
105
|
+
def enabled?(grid)
|
106
|
+
(!options[:if] || (options[:if] && options[:if].call(grid))) && !options[:unless] || (options[:unless] && !options[:unless].call(grid))
|
107
|
+
end
|
108
|
+
|
101
109
|
def inspect
|
102
110
|
"#<Datagird::Columns::Column #{grid_class}##{name} #{options.inspect}>"
|
103
111
|
end
|
@@ -137,10 +145,18 @@ class Datagrid::Columns::Column
|
|
137
145
|
|
138
146
|
def generic_value(model, grid)
|
139
147
|
if self.data_block.arity >= 1
|
140
|
-
Datagrid::Utils.apply_args(model, grid, &data_block)
|
148
|
+
Datagrid::Utils.apply_args(model, grid, grid.data_row(model), &data_block)
|
141
149
|
else
|
142
150
|
model.instance_eval(&self.data_block)
|
143
151
|
end
|
144
152
|
end
|
145
153
|
|
154
|
+
private
|
155
|
+
def convert_option_to_proc(option)
|
156
|
+
if option.is_a?(Proc)
|
157
|
+
option
|
158
|
+
elsif option
|
159
|
+
proc {|object| object.send(option.to_sym) }
|
160
|
+
end
|
161
|
+
end
|
146
162
|
end
|
data/lib/datagrid/core.rb
CHANGED
@@ -73,7 +73,7 @@ module Datagrid
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
-
# Returns a hash of grid attributes including filter values
|
76
|
+
# Returns a hash of grid attributes including filter values
|
77
77
|
# and ordering values
|
78
78
|
def attributes
|
79
79
|
result = {}
|
@@ -120,17 +120,17 @@ module Datagrid
|
|
120
120
|
end
|
121
121
|
|
122
122
|
# Redefines scope at instance level
|
123
|
-
#
|
123
|
+
#
|
124
124
|
# class MyGrid
|
125
125
|
# scope { Article.order('created_at desc') }
|
126
126
|
# end
|
127
|
-
#
|
127
|
+
#
|
128
128
|
# grid = MyGrid.new
|
129
129
|
# grid.scope do |scope|
|
130
130
|
# scope.where(:author_id => current_user.id)
|
131
131
|
# end
|
132
|
-
# grid.assets
|
133
|
-
# # => SELECT * FROM articles WHERE author_id = ?
|
132
|
+
# grid.assets
|
133
|
+
# # => SELECT * FROM articles WHERE author_id = ?
|
134
134
|
# # ORDER BY created_at desc
|
135
135
|
#
|
136
136
|
def scope(&block)
|
@@ -53,7 +53,7 @@ module Datagrid
|
|
53
53
|
def less_equal(scope, field, value)
|
54
54
|
raise NotImplementedError
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
def has_column?(scope, column_name)
|
58
58
|
raise NotImplementedError
|
59
59
|
end
|
@@ -82,6 +82,14 @@ module Datagrid
|
|
82
82
|
raise NotImplementedError
|
83
83
|
end
|
84
84
|
|
85
|
+
def append_column_queries(assets, columns)
|
86
|
+
if columns.present?
|
87
|
+
raise NotImplementedError
|
88
|
+
else
|
89
|
+
assets
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
85
93
|
protected
|
86
94
|
def timestamp_class?(klass)
|
87
95
|
TIMESTAMP_CLASSES.include?(klass)
|
@@ -4,20 +4,20 @@ module Datagrid
|
|
4
4
|
|
5
5
|
def self.match?(scope)
|
6
6
|
return false unless defined?(::ActiveRecord)
|
7
|
-
if scope.is_a?(Class)
|
7
|
+
if scope.is_a?(Class)
|
8
8
|
scope.ancestors.include?(::ActiveRecord::Base)
|
9
9
|
else
|
10
|
-
scope.is_a?(::ActiveRecord::Relation)
|
10
|
+
scope.is_a?(::ActiveRecord::Relation)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
def to_scope(scope)
|
15
15
|
return scope if scope.is_a?(::ActiveRecord::Relation)
|
16
16
|
# Model class or Active record association
|
17
|
-
# ActiveRecord association class hides itself under an Array
|
17
|
+
# ActiveRecord association class hides itself under an Array
|
18
18
|
# We can only reveal it by checking if it respond to some specific
|
19
19
|
# to ActiveRecord method like #scoped
|
20
|
-
if scope.is_a?(Class)
|
20
|
+
if scope.is_a?(Class)
|
21
21
|
Rails.version >= "4.0" ? scope.all : scope.scoped({})
|
22
22
|
elsif scope.respond_to?(:scoped)
|
23
23
|
scope.scoped
|
@@ -26,6 +26,16 @@ module Datagrid
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
def append_column_queries(assets, columns)
|
30
|
+
if columns.present?
|
31
|
+
if assets.select_values.empty?
|
32
|
+
assets = assets.select(Arel.respond_to?(:star) ? assets.klass.arel_table[Arel.star] : "#{assets.quoted_table_name}.*")
|
33
|
+
end
|
34
|
+
assets = assets.select(*columns)
|
35
|
+
end
|
36
|
+
assets
|
37
|
+
end
|
38
|
+
|
29
39
|
def where(scope, attribute, value)
|
30
40
|
scope.where(attribute => value)
|
31
41
|
end
|
@@ -97,7 +107,7 @@ module Datagrid
|
|
97
107
|
end
|
98
108
|
result
|
99
109
|
end
|
100
|
-
|
110
|
+
|
101
111
|
protected
|
102
112
|
|
103
113
|
def prefix_table_name(scope, field)
|
@@ -105,8 +115,8 @@ module Datagrid
|
|
105
115
|
end
|
106
116
|
|
107
117
|
def contains_predicate
|
108
|
-
defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) &&
|
109
|
-
::ActiveRecord::Base.connection.is_a?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) ?
|
118
|
+
defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) &&
|
119
|
+
::ActiveRecord::Base.connection.is_a?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) ?
|
110
120
|
'ilike' : 'like'
|
111
121
|
end
|
112
122
|
|
@@ -34,14 +34,14 @@ module Datagrid
|
|
34
34
|
|
35
35
|
def greater_equal(scope, field, value)
|
36
36
|
scope.select do |object|
|
37
|
-
compare_value = object.send(field)
|
37
|
+
compare_value = object.send(field)
|
38
38
|
compare_value.respond_to?(:>=) && compare_value >= value
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
42
|
def less_equal(scope, field, value)
|
43
43
|
scope.select do |object|
|
44
|
-
compare_value = object.send(field)
|
44
|
+
compare_value = object.send(field)
|
45
45
|
compare_value.respond_to?(:<=) && compare_value <= value
|
46
46
|
end
|
47
47
|
end
|
@@ -51,7 +51,7 @@ module Datagrid
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def is_timestamp?(scope, column_name)
|
54
|
-
has_column?(scope, column_name) &&
|
54
|
+
has_column?(scope, column_name) &&
|
55
55
|
timestamp_class?(scope.first.send(column_name).class)
|
56
56
|
end
|
57
57
|
|
data/lib/datagrid/filters.rb
CHANGED
@@ -48,6 +48,7 @@ module Datagrid
|
|
48
48
|
|
49
49
|
# Returns filter definition object by name
|
50
50
|
def filter_by_name(attribute)
|
51
|
+
return attribute if attribute.is_a?(Datagrid::Filters::BaseFilter)
|
51
52
|
self.filters.find do |filter|
|
52
53
|
filter.name.to_sym == attribute.to_sym
|
53
54
|
end
|
@@ -133,10 +134,14 @@ module Datagrid
|
|
133
134
|
end
|
134
135
|
|
135
136
|
# Returns string representation of filter value
|
136
|
-
def filter_value_as_string(
|
137
|
+
def filter_value_as_string(name)
|
138
|
+
filter = filter_by_name(name)
|
137
139
|
value = filter_value(filter)
|
138
|
-
|
139
|
-
|
140
|
+
if value.is_a?(Array)
|
141
|
+
value.map {|v| filter.format(v) }.join(filter.separator)
|
142
|
+
else
|
143
|
+
filter.format(value)
|
144
|
+
end
|
140
145
|
end
|
141
146
|
|
142
147
|
# Returns filter object with the given name
|
@@ -31,7 +31,7 @@ describe Datagrid::Columns do
|
|
31
31
|
it "should generate header" do
|
32
32
|
subject.header.should == ["Shipping date", "Group", "Name", "Access level", "Pet"]
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
it "should return html_columns" do
|
36
36
|
report = test_report do
|
37
37
|
scope {Entry}
|
@@ -82,6 +82,29 @@ describe Datagrid::Columns do
|
|
82
82
|
it "should support csv export options" do
|
83
83
|
subject.to_csv(:col_sep => ";").should == "Shipping date;Group;Name;Access level;Pet\n#{date};Pop;Star;admin;ROTTWEILER\n"
|
84
84
|
end
|
85
|
+
|
86
|
+
it "should support defining a query for a column" do
|
87
|
+
report = test_report do
|
88
|
+
scope {Entry}
|
89
|
+
filter(:name)
|
90
|
+
column(:id)
|
91
|
+
column(:sum_group_id, 'sum(group_id) sum_group_id')
|
92
|
+
end
|
93
|
+
report.assets.first.sum_group_id.should == group.id
|
94
|
+
end
|
95
|
+
it "should support hidding columns through if and unless" do
|
96
|
+
report = test_report do
|
97
|
+
scope {Entry}
|
98
|
+
column(:id, :if => :show?)
|
99
|
+
column(:name, :unless => proc {|grid| !grid.show? })
|
100
|
+
|
101
|
+
def show?
|
102
|
+
false
|
103
|
+
end
|
104
|
+
end
|
105
|
+
report.columns(:id).should == []
|
106
|
+
report.columns(:name).should == []
|
107
|
+
end
|
85
108
|
end
|
86
109
|
|
87
110
|
it "should support columns with model and report arguments" do
|
@@ -206,4 +229,45 @@ describe Datagrid::Columns do
|
|
206
229
|
report.rows
|
207
230
|
end
|
208
231
|
end
|
232
|
+
|
233
|
+
describe ".data_row" do
|
234
|
+
it "should give access to column values via an object" do
|
235
|
+
grid = test_report do
|
236
|
+
scope { Entry }
|
237
|
+
column(:id)
|
238
|
+
column(:name) do
|
239
|
+
name.capitalize
|
240
|
+
end
|
241
|
+
column(:actions, html: true) do
|
242
|
+
"some link here"
|
243
|
+
end
|
244
|
+
end
|
245
|
+
entry = Entry.create!(name: 'hello')
|
246
|
+
row = grid.data_row(entry)
|
247
|
+
row.id.should == entry.id
|
248
|
+
row.name.should == "Hello"
|
249
|
+
proc {
|
250
|
+
row.actions
|
251
|
+
}.should raise_error
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
describe "column value" do
|
256
|
+
it "should support conversion" do
|
257
|
+
group = Group.create!
|
258
|
+
Entry.create(:group => group)
|
259
|
+
Entry.create(:group => group)
|
260
|
+
grid = test_report do
|
261
|
+
scope { Group }
|
262
|
+
column(:entries_count) do |g|
|
263
|
+
g.entries.count
|
264
|
+
end
|
265
|
+
column(:odd_entries) do |_, _, row|
|
266
|
+
row.entries_count.odd?
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
grid.row_for(group).should == [2, false]
|
271
|
+
end
|
272
|
+
end
|
209
273
|
end
|
@@ -15,5 +15,10 @@ describe Datagrid::Drivers::ActiveRecord do
|
|
15
15
|
subject.to_scope(Entry.limit(5)).should be_a(ActiveRecord::Relation)
|
16
16
|
subject.to_scope(Group.create!.entries).should be_a(ActiveRecord::Relation)
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
|
+
it "should support append_column_queries" do
|
20
|
+
scope = subject.append_column_queries(Entry.scoped, ['sum(entries.group_id) sum_group_id'])
|
21
|
+
scope.select_values.length.should == 2
|
22
|
+
scope.select_values.should == ["#{Entry.quoted_table_name}.*", 'sum(entries.group_id) sum_group_id']
|
23
|
+
end
|
19
24
|
end
|
@@ -7,7 +7,7 @@ describe Datagrid::Filters::DateFilter do
|
|
7
7
|
e2 = Entry.create!(:created_at => 4.days.ago)
|
8
8
|
e3 = Entry.create!(:created_at => 1.day.ago)
|
9
9
|
report = test_report(:created_at => 5.day.ago..3.days.ago) do
|
10
|
-
scope { Entry }
|
10
|
+
scope { Entry }
|
11
11
|
filter(:created_at, :date)
|
12
12
|
end
|
13
13
|
report.assets.should_not include(e1)
|
@@ -21,7 +21,7 @@ describe Datagrid::Filters::DateFilter do
|
|
21
21
|
let(:klass) { klass }
|
22
22
|
subject do
|
23
23
|
test_report(:created_at => _created_at) do
|
24
|
-
scope { klass }
|
24
|
+
scope { klass }
|
25
25
|
filter(:created_at, :date, :range => true)
|
26
26
|
end.assets.to_a
|
27
27
|
end
|
@@ -57,7 +57,7 @@ describe Datagrid::Filters::DateFilter do
|
|
57
57
|
e2 = Entry.create!(:created_at => 4.days.ago)
|
58
58
|
e3 = Entry.create!(:created_at => 1.day.ago)
|
59
59
|
report = test_report(:created_at => [5.day.ago.to_date.to_s, 3.days.ago.to_date.to_s]) do
|
60
|
-
scope { Entry }
|
60
|
+
scope { Entry }
|
61
61
|
filter(:created_at, :date, :range => true)
|
62
62
|
end
|
63
63
|
report.assets.should_not include(e1)
|
@@ -70,7 +70,7 @@ describe Datagrid::Filters::DateFilter do
|
|
70
70
|
e2 = Entry.create!(:created_at => 4.days.ago)
|
71
71
|
e3 = Entry.create!(:created_at => 1.day.ago)
|
72
72
|
report = test_report(:created_at => [5.day.ago.to_date.to_s, nil]) do
|
73
|
-
scope { Entry }
|
73
|
+
scope { Entry }
|
74
74
|
filter(:created_at, :date, :range => true)
|
75
75
|
end
|
76
76
|
report.assets.should_not include(e1)
|
@@ -83,7 +83,7 @@ describe Datagrid::Filters::DateFilter do
|
|
83
83
|
e2 = Entry.create!(:created_at => 4.days.ago)
|
84
84
|
e3 = Entry.create!(:created_at => 1.day.ago)
|
85
85
|
report = test_report(:created_at => [nil, 3.days.ago.to_date.to_s]) do
|
86
|
-
scope { Entry }
|
86
|
+
scope { Entry }
|
87
87
|
filter(:created_at, :date, :range => true)
|
88
88
|
end
|
89
89
|
report.assets.should include(e1)
|
@@ -97,7 +97,7 @@ describe Datagrid::Filters::DateFilter do
|
|
97
97
|
e2 = Entry.create!(:created_at => 4.days.ago)
|
98
98
|
e3 = Entry.create!(:created_at => 1.day.ago)
|
99
99
|
report = test_report(:created_at => (4.days.ago.to_date..4.days.ago.to_date)) do
|
100
|
-
scope { Entry }
|
100
|
+
scope { Entry }
|
101
101
|
filter(:created_at, :date, :range => true)
|
102
102
|
end
|
103
103
|
report.assets.should_not include(e1)
|
@@ -110,7 +110,7 @@ describe Datagrid::Filters::DateFilter do
|
|
110
110
|
e2 = Entry.create!(:created_at => 4.days.ago)
|
111
111
|
e3 = Entry.create!(:created_at => 1.day.ago)
|
112
112
|
report = test_report(:created_at => (1.days.ago.to_date..7.days.ago.to_date)) do
|
113
|
-
scope { Entry }
|
113
|
+
scope { Entry }
|
114
114
|
filter(:created_at, :date, :range => true)
|
115
115
|
end
|
116
116
|
report.assets.should_not include(e1)
|
@@ -121,7 +121,7 @@ describe Datagrid::Filters::DateFilter do
|
|
121
121
|
|
122
122
|
it "should support block" do
|
123
123
|
report = test_report(:created_at => Date.today) do
|
124
|
-
scope { Entry }
|
124
|
+
scope { Entry }
|
125
125
|
filter(:created_at, :date, :range => true) do |value|
|
126
126
|
where("created_at >= ?", value)
|
127
127
|
end
|
@@ -129,7 +129,7 @@ describe Datagrid::Filters::DateFilter do
|
|
129
129
|
report.assets.should_not include(Entry.create!(:created_at => 1.day.ago))
|
130
130
|
report.assets.should include(Entry.create!(:created_at => DateTime.now))
|
131
131
|
end
|
132
|
-
|
132
|
+
|
133
133
|
|
134
134
|
context "when date format is configured" do
|
135
135
|
around(:each) do |example|
|
@@ -170,4 +170,15 @@ describe Datagrid::Filters::DateFilter do
|
|
170
170
|
end
|
171
171
|
report.created_at.should == [Date.new(2012, 01, 01), Date.new(2013, 01, 01)]
|
172
172
|
end
|
173
|
+
|
174
|
+
it "should properly format date in filter_value_as_string" do
|
175
|
+
with_date_format do
|
176
|
+
report = test_report(:created_at => "2012-01-02") do
|
177
|
+
scope {Entry}
|
178
|
+
filter(:created_at, :date)
|
179
|
+
end
|
180
|
+
report.filter_value_as_string(:created_at).should == "01/02/2012"
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
173
184
|
end
|
@@ -63,6 +63,17 @@ describe Datagrid::FormBuilder do
|
|
63
63
|
it { should equal_to_dom(
|
64
64
|
'<input class="created_at date_filter" id="report_created_at" name="report[created_at]" size="30" type="text"/>'
|
65
65
|
)}
|
66
|
+
context "when special date format specified" do
|
67
|
+
around(:each) do |example|
|
68
|
+
_grid.created_at = Date.parse('2012-01-02')
|
69
|
+
with_date_format do
|
70
|
+
example.run
|
71
|
+
end
|
72
|
+
end
|
73
|
+
it { should equal_to_dom(
|
74
|
+
'<input class="created_at date_filter" id="report_created_at" name="report[created_at]" size="30" type="text" value="01/02/2012"/>'
|
75
|
+
)}
|
76
|
+
end
|
66
77
|
end
|
67
78
|
|
68
79
|
context "with integer filter type and range option" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: datagrid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bogdan Gusiev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -324,7 +324,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
324
324
|
version: '0'
|
325
325
|
requirements: []
|
326
326
|
rubyforge_project:
|
327
|
-
rubygems_version: 2.
|
327
|
+
rubygems_version: 2.2.2
|
328
328
|
signing_key:
|
329
329
|
specification_version: 4
|
330
330
|
summary: Ruby gem to create datagrids
|