datagrid 0.9.2 → 0.9.3
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.
- data/VERSION +1 -1
- data/datagrid.gemspec +3 -3
- data/lib/datagrid/columns/column.rb +2 -4
- data/lib/datagrid/core.rb +29 -5
- data/lib/datagrid/filters.rb +24 -12
- data/lib/datagrid/filters/base_filter.rb +8 -6
- data/lib/datagrid/filters/enum_filter.rb +1 -1
- data/lib/datagrid/ordering.rb +1 -1
- data/lib/datagrid/utils.rb +9 -0
- data/spec/datagrid/columns_spec.rb +2 -1
- data/spec/datagrid/core_spec.rb +17 -5
- data/spec/datagrid/filters_spec.rb +9 -0
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.3
|
data/datagrid.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "datagrid"
|
8
|
-
s.version = "0.9.
|
8
|
+
s.version = "0.9.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Bogdan Gusiev"]
|
12
|
-
s.date = "2013-08-
|
12
|
+
s.date = "2013-08-26"
|
13
13
|
s.description = "This allows you to easily build datagrid aka data tables with sortable columns and filters"
|
14
14
|
s.email = "agresso@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -99,7 +99,7 @@ Gem::Specification.new do |s|
|
|
99
99
|
s.homepage = "http://github.com/bogdan/datagrid"
|
100
100
|
s.licenses = ["MIT"]
|
101
101
|
s.require_paths = ["lib"]
|
102
|
-
s.rubygems_version = "1.8.
|
102
|
+
s.rubygems_version = "1.8.24"
|
103
103
|
s.summary = "Ruby gem to create datagrids"
|
104
104
|
|
105
105
|
if s.respond_to? :specification_version then
|
@@ -108,10 +108,8 @@ class Datagrid::Columns::Column
|
|
108
108
|
end
|
109
109
|
|
110
110
|
def generic_value(model, grid)
|
111
|
-
if self.data_block.arity
|
112
|
-
|
113
|
-
elsif self.data_block.arity == 2
|
114
|
-
self.data_block.call(model, grid)
|
111
|
+
if self.data_block.arity >= 1
|
112
|
+
Datagrid::Utils.apply_args(model, grid, &data_block)
|
115
113
|
else
|
116
114
|
model.instance_eval(&self.data_block)
|
117
115
|
end
|
data/lib/datagrid/core.rb
CHANGED
@@ -69,10 +69,12 @@ module Datagrid
|
|
69
69
|
end
|
70
70
|
|
71
71
|
if block_given?
|
72
|
-
self.
|
72
|
+
self.scope(&block)
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
+
# Returns a hash of grid attributes including filter values
|
77
|
+
# and ordering values
|
76
78
|
def attributes
|
77
79
|
result = {}
|
78
80
|
self.datagrid_attributes.each do |name|
|
@@ -81,6 +83,7 @@ module Datagrid
|
|
81
83
|
result
|
82
84
|
end
|
83
85
|
|
86
|
+
# Alias for <tt>send</tt> method
|
84
87
|
def [](attribute)
|
85
88
|
self.send(attribute)
|
86
89
|
end
|
@@ -89,6 +92,7 @@ module Datagrid
|
|
89
92
|
self.send(:"#{attribute}=", value)
|
90
93
|
end
|
91
94
|
|
95
|
+
# Returns a scope(e.g ActiveRecord::Relation) with all applied filters
|
92
96
|
def assets
|
93
97
|
driver.to_scope(scope)
|
94
98
|
end
|
@@ -110,23 +114,43 @@ module Datagrid
|
|
110
114
|
attributes
|
111
115
|
end
|
112
116
|
|
113
|
-
def paginate(*args, &block)
|
117
|
+
def paginate(*args, &block) # :nodoc:
|
114
118
|
::Datagrid::Utils.warn_once("#paginate is deprecated. Call it like object.assets.paginate(...).")
|
115
119
|
self.assets.paginate(*args, &block)
|
116
120
|
end
|
117
121
|
|
122
|
+
# Redefines scope at instance level
|
123
|
+
#
|
124
|
+
# class MyGrid
|
125
|
+
# scope { Article.order('created_at desc') }
|
126
|
+
# end
|
127
|
+
#
|
128
|
+
# grid = MyGrid.new
|
129
|
+
# grid.scope do |scope|
|
130
|
+
# scope.where(:author_id => current_user.id)
|
131
|
+
# end
|
132
|
+
# grid.assets
|
133
|
+
# # => SELECT * FROM articles WHERE author_id = ?
|
134
|
+
# # ORDER BY created_at desc
|
135
|
+
#
|
118
136
|
def scope(&block)
|
119
137
|
if block_given?
|
120
|
-
|
138
|
+
current_scope = scope
|
139
|
+
self.scope_value = proc {
|
140
|
+
Datagrid::Utils.apply_args(current_scope, &block)
|
141
|
+
}
|
121
142
|
self
|
122
|
-
elsif scope_value
|
123
|
-
scope_value.call
|
124
143
|
else
|
125
144
|
check_scope_defined!
|
126
145
|
scope_value.call
|
127
146
|
end
|
128
147
|
end
|
129
148
|
|
149
|
+
# Resets current instance scope to default scope defined in a class
|
150
|
+
def reset_scope
|
151
|
+
scope(&self.class.scope_value)
|
152
|
+
end
|
153
|
+
|
130
154
|
def driver #:nodoc:
|
131
155
|
self.class.driver
|
132
156
|
end
|
data/lib/datagrid/filters.rb
CHANGED
@@ -25,7 +25,7 @@ module Datagrid
|
|
25
25
|
:float => Filters::FloatFilter,
|
26
26
|
}
|
27
27
|
|
28
|
-
def self.included(base)
|
28
|
+
def self.included(base) #:nodoc:
|
29
29
|
base.extend ClassMethods
|
30
30
|
base.class_eval do
|
31
31
|
|
@@ -40,43 +40,53 @@ module Datagrid
|
|
40
40
|
|
41
41
|
module ClassMethods
|
42
42
|
|
43
|
+
# Returns filter definition object by name
|
43
44
|
def filter_by_name(attribute)
|
44
45
|
self.filters.find do |filter|
|
45
46
|
filter.name.to_sym == attribute.to_sym
|
46
47
|
end
|
47
48
|
end
|
48
49
|
|
49
|
-
# Defines new datagrid filter
|
50
|
+
# Defines new datagrid filter.
|
51
|
+
# This method automatically generates <tt>attr_accessor</tt> for filter name
|
52
|
+
# and adds it to the list of datagrid attributes.
|
50
53
|
#
|
51
54
|
# Arguments:
|
52
55
|
#
|
53
56
|
# * <tt>name</tt> - filter name
|
57
|
+
# * <tt>type</tt> - filter type that defines type case and GUI representation of a filter
|
54
58
|
# * <tt>options</tt> - hash of options
|
55
59
|
# * <tt>block</tt> - proc to apply the filter
|
56
60
|
#
|
57
61
|
# Available options:
|
58
62
|
#
|
59
63
|
# * <tt>:header</tt> - determines the header of the filter
|
60
|
-
# * <tt>:default</tt> - the default filter value
|
64
|
+
# * <tt>:default</tt> - the default filter value. Able to accept a <tt>Proc</tt> in case default should be recalculated
|
61
65
|
# * <tt>:multiple</tt> - determines if more than one option can be selected
|
62
66
|
# * <tt>:allow_nil</tt> - determines if the value can be nil
|
63
67
|
# * <tt>:allow_blank</tt> - determines if the value can be blank
|
64
|
-
# * <tt>:before</tt> - determines the position of this filter,
|
65
|
-
#
|
68
|
+
# * <tt>:before</tt> - determines the position of this filter,
|
69
|
+
# by adding it before the filter passed here (when using datagrid_form_for helper)
|
70
|
+
# * <tt>:after</tt> - determines the position of this filter,
|
71
|
+
# by adding it after the filter passed here (when using datagrid_form_for helper)
|
72
|
+
# * <tt>:dummy</tt> - if true, this filter will not be applied automatically
|
73
|
+
# and will just be displayed in form. In case you may want to apply it manually.
|
66
74
|
#
|
67
75
|
# See: https://github.com/bogdan/datagrid/wiki/Columns for examples
|
68
|
-
def filter(
|
69
|
-
|
70
|
-
|
76
|
+
def filter(name, type = :default, options = {}, &block)
|
77
|
+
if type.is_a?(Hash)
|
78
|
+
options = type
|
79
|
+
type = :default
|
80
|
+
end
|
71
81
|
|
72
82
|
klass = type.is_a?(Class) ? type : FILTER_TYPES[type]
|
73
83
|
raise ConfigurationError, "filter class #{type.inspect} not found" unless klass
|
74
84
|
|
75
85
|
position = Datagrid::Utils.extract_position_from_options(self.filters, options)
|
76
|
-
filter = klass.new(self,
|
86
|
+
filter = klass.new(self, name, options, &block)
|
77
87
|
self.filters.insert(position, filter)
|
78
88
|
|
79
|
-
datagrid_attribute(
|
89
|
+
datagrid_attribute(name) do |value|
|
80
90
|
filter.parse_values(value)
|
81
91
|
end
|
82
92
|
|
@@ -93,14 +103,14 @@ module Datagrid
|
|
93
103
|
|
94
104
|
module InstanceMethods
|
95
105
|
|
96
|
-
def initialize(*args, &block)
|
106
|
+
def initialize(*args, &block) # :nodoc:
|
97
107
|
self.filters.each do |filter|
|
98
108
|
self[filter.name] = filter.default
|
99
109
|
end
|
100
110
|
super(*args, &block)
|
101
111
|
end
|
102
112
|
|
103
|
-
def assets
|
113
|
+
def assets # :nodoc:
|
104
114
|
result = super
|
105
115
|
self.class.filters.each do |filter|
|
106
116
|
result = filter.apply(self, result, filter_value(filter))
|
@@ -108,10 +118,12 @@ module Datagrid
|
|
108
118
|
result
|
109
119
|
end
|
110
120
|
|
121
|
+
# Returns all defined filters Array
|
111
122
|
def filters
|
112
123
|
self.class.filters
|
113
124
|
end
|
114
125
|
|
126
|
+
# Returns filter valur for given filter definition
|
115
127
|
def filter_value(filter)
|
116
128
|
self[filter.name]
|
117
129
|
end
|
@@ -75,7 +75,7 @@ class Datagrid::Filters::BaseFilter
|
|
75
75
|
def default_filter_block
|
76
76
|
filter = self
|
77
77
|
lambda do |value, scope, grid|
|
78
|
-
filter.default_filter(value, scope, grid)
|
78
|
+
filter.dummy? ? nil : filter.default_filter(value, scope, grid)
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
@@ -91,6 +91,10 @@ class Datagrid::Filters::BaseFilter
|
|
91
91
|
def format(value)
|
92
92
|
value.nil? ? nil : value.to_s
|
93
93
|
end
|
94
|
+
|
95
|
+
def dummy?
|
96
|
+
options[:dummy]
|
97
|
+
end
|
94
98
|
|
95
99
|
protected
|
96
100
|
|
@@ -99,12 +103,10 @@ class Datagrid::Filters::BaseFilter
|
|
99
103
|
end
|
100
104
|
|
101
105
|
def execute(value, scope, grid_object, &block)
|
102
|
-
if block.arity
|
103
|
-
scope.instance_exec(value, scope, grid_object, &block)
|
104
|
-
elsif block.arity == 2
|
105
|
-
scope.instance_exec(value, scope, &block)
|
106
|
-
else
|
106
|
+
if block.arity == 1
|
107
107
|
scope.instance_exec(value, &block)
|
108
|
+
else
|
109
|
+
Datagrid::Utils.apply_args(value, scope, grid_object, &block)
|
108
110
|
end
|
109
111
|
end
|
110
112
|
|
@@ -15,7 +15,7 @@ class Datagrid::Filters::EnumFilter < Datagrid::Filters::BaseFilter
|
|
15
15
|
if select.is_a?(Symbol)
|
16
16
|
object.send(select)
|
17
17
|
elsif select.respond_to?(:call)
|
18
|
-
|
18
|
+
Datagrid::Utils.apply_args(object, &select)
|
19
19
|
else
|
20
20
|
select
|
21
21
|
end
|
data/lib/datagrid/ordering.rb
CHANGED
data/lib/datagrid/utils.rb
CHANGED
@@ -46,6 +46,15 @@ module Datagrid
|
|
46
46
|
-1
|
47
47
|
end
|
48
48
|
end
|
49
|
+
|
50
|
+
def apply_args(*args, &block)
|
51
|
+
return block.call(*args) if block.arity < 0
|
52
|
+
args = args.clone
|
53
|
+
(args.size - block.arity).times do
|
54
|
+
args.pop
|
55
|
+
end
|
56
|
+
block.call(*args)
|
57
|
+
end
|
49
58
|
end
|
50
59
|
end
|
51
60
|
end
|
@@ -50,7 +50,8 @@ describe Datagrid::Columns do
|
|
50
50
|
"'#{model.name}' filtered by '#{grid.name}'"
|
51
51
|
end
|
52
52
|
end
|
53
|
-
|
53
|
+
entry = Entry.create!(:name => "Hello World")
|
54
|
+
report.row_for(entry).should == [entry.id, "'Hello World' filtered by 'Hello'"]
|
54
55
|
end
|
55
56
|
|
56
57
|
it "should generate table data" do
|
data/spec/datagrid/core_spec.rb
CHANGED
@@ -5,10 +5,9 @@ describe Datagrid::Core do
|
|
5
5
|
context 'with 2 persisted entries' do
|
6
6
|
before { 2.times { Entry.create } }
|
7
7
|
|
8
|
-
let(:limit) { Entry.limit(1) }
|
9
8
|
let(:report_class) do
|
10
9
|
test_report_class do
|
11
|
-
scope { Entry }
|
10
|
+
scope { Entry.order("id desc") }
|
12
11
|
end
|
13
12
|
end
|
14
13
|
|
@@ -22,17 +21,30 @@ describe Datagrid::Core do
|
|
22
21
|
context 'changes scope on the fly' do
|
23
22
|
let(:report) do
|
24
23
|
report_class.new.tap do |r|
|
25
|
-
r.scope { limit
|
24
|
+
r.scope { Entry.limit(1)}
|
26
25
|
end
|
27
26
|
end
|
28
27
|
|
29
28
|
it { expect(report.scope).to have(1).item }
|
30
29
|
end
|
31
30
|
|
32
|
-
context '
|
33
|
-
let(:report) { report_class.new { limit } }
|
31
|
+
context 'overriding scope by initializer' do
|
32
|
+
let(:report) { report_class.new { Entry.limit(1) } }
|
34
33
|
|
35
34
|
it { expect(report.scope).to have(1).item }
|
35
|
+
|
36
|
+
context "reset scope to default" do
|
37
|
+
before do
|
38
|
+
report.reset_scope
|
39
|
+
end
|
40
|
+
it { expect(report.scope).to have(2).item }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "appending scope by initializer " do
|
45
|
+
let(:report) { report_class.new {|scope| scope.limit(1)} }
|
46
|
+
it { expect(report.scope).to have(1).item }
|
47
|
+
it { expect(report.scope.order_values).to have(1).item }
|
36
48
|
end
|
37
49
|
end
|
38
50
|
end
|
@@ -152,4 +152,13 @@ describe Datagrid::Filters do
|
|
152
152
|
grid.filters.index {|f| f.name == :group_id}.should == 1
|
153
153
|
end
|
154
154
|
end
|
155
|
+
|
156
|
+
it "should support dummy filter" do
|
157
|
+
grid = test_report do
|
158
|
+
scope { Entry }
|
159
|
+
filter(:period, :date, :dummy => true, :default => proc { Date.today })
|
160
|
+
end
|
161
|
+
Entry.create!(:created_at => 3.days.ago)
|
162
|
+
grid.assets.should_not be_empty
|
163
|
+
end
|
155
164
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: datagrid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-08-
|
12
|
+
date: 2013-08-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -337,7 +337,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
337
337
|
version: '0'
|
338
338
|
segments:
|
339
339
|
- 0
|
340
|
-
hash:
|
340
|
+
hash: -2282688358265272109
|
341
341
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
342
342
|
none: false
|
343
343
|
requirements:
|
@@ -346,7 +346,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
346
346
|
version: '0'
|
347
347
|
requirements: []
|
348
348
|
rubyforge_project:
|
349
|
-
rubygems_version: 1.8.
|
349
|
+
rubygems_version: 1.8.24
|
350
350
|
signing_key:
|
351
351
|
specification_version: 3
|
352
352
|
summary: Ruby gem to create datagrids
|