datagrid 1.5.4 → 1.5.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e27c90403d5aa7a2c924c000961747a6f8aad08
4
- data.tar.gz: dc805468469c8c967b06f69b33e875038d606ae2
3
+ metadata.gz: 012d1468059f7ed2bc98a58b26d982960eb40c9c
4
+ data.tar.gz: 6bd2b6019015f43c84e372fd27a5472e7f72d115
5
5
  SHA512:
6
- metadata.gz: 033dd01f968f8af3360e92623c2921d6b08cc5032eac39d46e948bc9fc14b6f5d988c21e9c3aea4493aebfb59c30f986770b2cee35beb2a1aa0423fd51f91004
7
- data.tar.gz: 609622339821f30c85f9d8abcb603ee592763ac0a57c78f0d0e0e00d9691292d224ab443d369ef4672336debb5bab9cf2392e0c83ce2854d2019c4affbb2bcb4
6
+ metadata.gz: 430016e9d03c4f09f0d8e0efec3faac6ec8c9428aa77a98679de546e3c52f7325aad22aae6a7aaed04b5bdb5be1048f2a9df73df43101bdb3178cdf1a57888c9
7
+ data.tar.gz: bdeb1423e1bdbf3ef66d2e4656ff565c50ec1a3e2cd18dc5d076e3b364528618cec623b20b528b8b98399a9f15ebbd1cb9f0bdbb7de8a35993ccc174f389ca08
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.5.4
1
+ 1.5.5
@@ -1,10 +1,10 @@
1
1
  <div class="order">
2
2
  <%= link_to(
3
3
  I18n.t("datagrid.table.order.asc").html_safe,
4
- url_for(grid.param_name => grid.as_query.merge(:order => column.name, :descending => false)),
4
+ datagrid_order_path(grid, column, false),
5
5
  :class => "asc") %>
6
6
  <%= link_to(
7
7
  I18n.t("datagrid.table.order.desc").html_safe,
8
- url_for(grid.param_name => grid.as_query.merge(:order => column.name, :descending => true )),
8
+ datagrid_order_path(grid, column, true),
9
9
  :class => "desc") %>
10
10
  </div>
@@ -2,16 +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.5.4 ruby lib
5
+ # stub: datagrid 1.5.5 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "datagrid".freeze
9
- s.version = "1.5.4"
9
+ s.version = "1.5.5"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["Bogdan Gusiev".freeze]
14
- s.date = "2017-06-16"
14
+ s.date = "2017-10-03"
15
15
  s.description = "This allows you to easily build datagrid aka data tables with sortable columns and filters".freeze
16
16
  s.email = "agresso@gmail.com".freeze
17
17
  s.extra_rdoc_files = [
@@ -20,8 +20,6 @@ module Datagrid
20
20
  class_attribute :columns_array
21
21
  self.columns_array = []
22
22
 
23
- class_attribute :dynamic_block, :instance_writer => false
24
-
25
23
  class_attribute :cached
26
24
  self.cached = false
27
25
 
@@ -163,42 +161,6 @@ module Datagrid
163
161
  end
164
162
  end
165
163
 
166
- # Allows dynamic columns definition, that could not be defined at class level
167
- #
168
- # class MerchantsGrid
169
- #
170
- # scope { Merchant }
171
- #
172
- # column(:name)
173
- #
174
- # dynamic do
175
- # PurchaseCategory.all.each do |category|
176
- # column(:"#{category.name.underscore}_sales") do |merchant|
177
- # merchant.purchases.where(:category_id => category.id).count
178
- # end
179
- # end
180
- # end
181
- # end
182
- #
183
- # grid = MerchantsGrid.new
184
- # grid.data # => [
185
- # # [ "Name", "Swimwear Sales", "Sportswear Sales", ... ]
186
- # # [ "Reebok", 2083382, 8382283, ... ]
187
- # # [ "Nike", 8372283, 18734783, ... ]
188
- # # ]
189
- def dynamic(&block)
190
- previous_block = dynamic_block
191
- self.dynamic_block =
192
- if previous_block
193
- proc {
194
- instance_eval(&previous_block)
195
- instance_eval(&block)
196
- }
197
- else
198
- block
199
- end
200
- end
201
-
202
164
  # Defines a model decorator that will be used to define a column value.
203
165
  # All column blocks will be given a decorated version of the model.
204
166
  #
@@ -450,7 +412,6 @@ module Datagrid
450
412
  def initialize(*) #:nodoc:
451
413
  self.columns_array = self.class.columns_array.clone
452
414
  super
453
- instance_eval(&dynamic_block) if dynamic_block
454
415
  end
455
416
 
456
417
  # Returns all columns available for current grid configuration.
@@ -8,8 +8,14 @@ module Datagrid
8
8
  base.extend ClassMethods
9
9
  base.class_eval do
10
10
  class_attribute :scope_value
11
+
11
12
  class_attribute :datagrid_attributes
12
13
  self.datagrid_attributes = []
14
+
15
+ class_attribute :dynamic_block, :instance_writer => false
16
+ if defined?(::ActiveModel::AttributeAssignment)
17
+ include ::ActiveModel::AttributeAssignment
18
+ end
13
19
  end
14
20
  base.send :include, InstanceMethods
15
21
  end # self.included
@@ -50,6 +56,42 @@ module Datagrid
50
56
  @driver ||= Drivers::AbstractDriver.guess_driver(scope).new
51
57
  end
52
58
 
59
+ # Allows dynamic columns definition, that could not be defined at class level
60
+ #
61
+ # class MerchantsGrid
62
+ #
63
+ # scope { Merchant }
64
+ #
65
+ # column(:name)
66
+ #
67
+ # dynamic do
68
+ # PurchaseCategory.all.each do |category|
69
+ # column(:"#{category.name.underscore}_sales") do |merchant|
70
+ # merchant.purchases.where(:category_id => category.id).count
71
+ # end
72
+ # end
73
+ # end
74
+ # end
75
+ #
76
+ # grid = MerchantsGrid.new
77
+ # grid.data # => [
78
+ # # [ "Name", "Swimwear Sales", "Sportswear Sales", ... ]
79
+ # # [ "Reebok", 2083382, 8382283, ... ]
80
+ # # [ "Nike", 8372283, 18734783, ... ]
81
+ # # ]
82
+ def dynamic(&block)
83
+ previous_block = dynamic_block
84
+ self.dynamic_block =
85
+ if previous_block
86
+ proc {
87
+ instance_eval(&previous_block)
88
+ instance_eval(&block)
89
+ }
90
+ else
91
+ block
92
+ end
93
+ end
94
+
53
95
  protected
54
96
  def check_scope_defined!(message = nil)
55
97
  message ||= "#{self}.scope is not defined"
@@ -72,6 +114,7 @@ module Datagrid
72
114
  self.attributes = attributes
73
115
  end
74
116
 
117
+ instance_eval(&dynamic_block) if dynamic_block
75
118
  if block_given?
76
119
  self.scope(&block)
77
120
  end
@@ -105,7 +105,6 @@ module Datagrid
105
105
  datagrid_attribute(name) do |value|
106
106
  filter.parse_values(value)
107
107
  end
108
-
109
108
  end
110
109
 
111
110
  def default_filter
@@ -156,6 +156,8 @@ class Datagrid::Filters::BaseFilter #:nodoc:
156
156
  when String
157
157
  #TODO: write tests and doc
158
158
  value.split(separator)
159
+ when Range
160
+ [value.first, value.last]
159
161
  when Array
160
162
  value
161
163
  else
@@ -12,7 +12,7 @@ module Datagrid::Filters::RangedFilter
12
12
  result = super(value)
13
13
  return result if !range? || result.nil?
14
14
  # Simulate single point range
15
- return result..result unless result.is_a?(Array)
15
+ return [result, result] unless result.is_a?(Array)
16
16
 
17
17
  case result.size
18
18
  when 0
@@ -116,6 +116,11 @@ module Datagrid
116
116
  end
117
117
  end
118
118
 
119
+ # Generates an ascending or descending order url for the given column
120
+ def datagrid_order_path(grid, column, descending)
121
+ datagrid_renderer.order_path(grid, column, descending, request)
122
+ end
123
+
119
124
  # Represents a datagrid row that provides access to column values for the given asset
120
125
  #
121
126
  # row = datagrid_row(grid, user)
@@ -78,6 +78,16 @@ module Datagrid
78
78
  { :grid => grid, :column => column })
79
79
  end
80
80
 
81
+ def order_path(grid, column, descending, request)
82
+ column = grid.column_by_name(column)
83
+ query = request ? request.query_parameters : {}
84
+ order_parameter = {grid.param_name => grid.as_query.merge(:order => column.name, :descending => descending)}
85
+ ActionDispatch::Http::URL.path_for(
86
+ path: request ? request.path : '/',
87
+ params: query.merge(order_parameter)
88
+ )
89
+ end
90
+
81
91
  private
82
92
 
83
93
  def _safe(string)
@@ -459,38 +459,6 @@ describe Datagrid::Columns do
459
459
  end
460
460
  end
461
461
 
462
- describe 'dynamic helper' do
463
- it "should work" do
464
- grid = test_report do
465
- scope {Entry}
466
- column(:id)
467
- dynamic {
468
- column(:name)
469
- column(:category)
470
- }
471
- end
472
-
473
- expect(grid.columns.map(&:name)).to eq([:id, :name, :category])
474
- expect(grid.class.columns.map(&:name)).to eq([:id])
475
-
476
- expect(grid.column_by_name(:id)).not_to be_nil
477
- expect(grid.column_by_name(:name)).not_to be_nil
478
- end
479
-
480
- it "has access to attributes" do
481
- grid = test_report(:attribute_name => 'value') do
482
- scope {Entry}
483
- datagrid_attribute :attribute_name
484
- dynamic {
485
- value = attribute_name
486
- column(:name) { value }
487
- }
488
- end
489
-
490
- expect(grid.data_value(:name, Entry.create!)).to eq('value')
491
- end
492
- end
493
-
494
462
  describe ".data_value" do
495
463
  it "should return value" do
496
464
  grid = test_report do
@@ -109,4 +109,53 @@ describe Datagrid::Core do
109
109
  expect(EqualTest.new).to_not eq(EqualTest.new {|s| s.reorder(:name)})
110
110
  end
111
111
  end
112
+
113
+ describe 'dynamic helper' do
114
+ it "should work" do
115
+ grid = test_report do
116
+ scope {Entry}
117
+ column(:id)
118
+ dynamic {
119
+ column(:name)
120
+ column(:category)
121
+ }
122
+ end
123
+
124
+ expect(grid.columns.map(&:name)).to eq([:id, :name, :category])
125
+ expect(grid.class.columns.map(&:name)).to eq([:id])
126
+
127
+ expect(grid.column_by_name(:id)).not_to be_nil
128
+ expect(grid.column_by_name(:name)).not_to be_nil
129
+ end
130
+
131
+ it "has access to attributes" do
132
+ grid = test_report(:attribute_name => 'value') do
133
+ scope {Entry}
134
+ datagrid_attribute :attribute_name
135
+ dynamic {
136
+ value = attribute_name
137
+ column(:name) { value }
138
+ }
139
+ end
140
+
141
+ expect(grid.data_value(:name, Entry.create!)).to eq('value')
142
+ end
143
+
144
+ it "applies before instance scope" do
145
+ klass = test_report_class do
146
+ scope {Entry}
147
+ dynamic do
148
+ scope do |s|
149
+ s.limit(1)
150
+ end
151
+ end
152
+ end
153
+
154
+ grid = klass.new do |s|
155
+ s.limit(2)
156
+ end
157
+
158
+ expect(grid.assets.limit_value).to eq(2)
159
+ end
160
+ end
112
161
  end
@@ -104,18 +104,20 @@ describe Datagrid::Filters::DateFilter do
104
104
  expect(report.assets).to include(e2)
105
105
  expect(report.assets).not_to include(e3)
106
106
  end
107
- it "should support invalid range" do
108
107
 
108
+ it "should invert invalid range" do
109
+ range = 1.days.ago..7.days.ago
109
110
  e1 = Entry.create!(:created_at => 7.days.ago)
110
111
  e2 = Entry.create!(:created_at => 4.days.ago)
111
112
  e3 = Entry.create!(:created_at => 1.day.ago)
112
- report = test_report(:created_at => (1.days.ago.to_date..7.days.ago.to_date)) do
113
+ report = test_report(:created_at => range) do
113
114
  scope { Entry }
114
115
  filter(:created_at, :date, :range => true)
115
116
  end
116
- expect(report.assets).not_to include(e1)
117
- expect(report.assets).not_to include(e2)
118
- expect(report.assets).not_to include(e3)
117
+ expect(report.created_at).to eq([range.last.to_date, range.first.to_date])
118
+ expect(report.assets).to include(e1)
119
+ expect(report.assets).to include(e2)
120
+ expect(report.assets).to include(e3)
119
121
  end
120
122
 
121
123
 
@@ -85,7 +85,7 @@ describe Datagrid::Filters::DateTimeFilter do
85
85
  e1 = Entry.create!(:created_at => Time.new(2013, 1, 1, 1, 0))
86
86
  e2 = Entry.create!(:created_at => Time.new(2013, 1, 1, 2, 0))
87
87
  e3 = Entry.create!(:created_at => Time.new(2013, 1, 1, 3, 0))
88
- report = test_report(:created_at => (Time.new(2013, 1, 1, 2, 0)..Time.new(2013, 1, 1, 2, 0))) do
88
+ report = test_report(:created_at => Time.new(2013, 1, 1, 2, 0)..Time.new(2013, 1, 1, 2, 0)) do
89
89
  scope { Entry }
90
90
  filter(:created_at, :datetime, :range => true)
91
91
  end
@@ -93,18 +93,20 @@ describe Datagrid::Filters::DateTimeFilter do
93
93
  expect(report.assets).to include(e2)
94
94
  expect(report.assets).not_to include(e3)
95
95
  end
96
- it "should support invalid range" do
96
+ it "should reverse invalid range" do
97
97
 
98
+ range = Time.new(2013, 1, 1, 3, 0)..Time.new(2013, 1, 1, 1, 0)
98
99
  e1 = Entry.create!(:created_at => Time.new(2013, 1, 1, 1, 0))
99
100
  e2 = Entry.create!(:created_at => Time.new(2013, 1, 1, 2, 0))
100
101
  e3 = Entry.create!(:created_at => Time.new(2013, 1, 1, 3, 0))
101
- report = test_report(:created_at => (Time.new(2013, 1, 1, 3, 0)..Time.new(2013, 1, 1, 1, 0))) do
102
+ report = test_report(:created_at => range) do
102
103
  scope { Entry }
103
104
  filter(:created_at, :datetime, :range => true)
104
105
  end
105
- expect(report.assets).not_to include(e1)
106
- expect(report.assets).not_to include(e2)
107
- expect(report.assets).not_to include(e3)
106
+ expect(report.created_at).to eq([range.last, range.first])
107
+ expect(report.assets).to include(e1)
108
+ expect(report.assets).to include(e2)
109
+ expect(report.assets).to include(e3)
108
110
  end
109
111
 
110
112
 
@@ -59,15 +59,17 @@ describe Datagrid::Filters::IntegerFilter do
59
59
  expect(report.assets).to include(entry4)
60
60
  expect(report.assets).not_to include(entry1)
61
61
  end
62
+
62
63
  it "should support invalid range" do
63
64
 
64
65
  report = test_report(:group_id => (7..1)) do
65
66
  scope { Entry }
66
67
  filter(:group_id, :integer, :range => true)
67
68
  end
68
- expect(report.assets).not_to include(entry7)
69
- expect(report.assets).not_to include(entry4)
70
- expect(report.assets).not_to include(entry1)
69
+ expect(report.group_id).to eq([1,7])
70
+ expect(report.assets).to include(entry7)
71
+ expect(report.assets).to include(entry4)
72
+ expect(report.assets).to include(entry1)
71
73
  end
72
74
 
73
75
 
@@ -131,9 +131,9 @@ describe Datagrid::FormBuilder do
131
131
  context "with invalid range value" do
132
132
  let(:_range) { 2..1 }
133
133
  it { should equal_to_dom(
134
- '<input class="group_id integer_filter from" multiple value="2" type="text" name="report[group_id][]"/>' +
134
+ '<input class="group_id integer_filter from" multiple value="1" type="text" name="report[group_id][]"/>' +
135
135
  '<span class="separator integer"> - </span>' +
136
- '<input class="group_id integer_filter to" multiple value="1" type="text" name="report[group_id][]"/>'
136
+ '<input class="group_id integer_filter to" multiple value="2" type="text" name="report[group_id][]"/>'
137
137
  )}
138
138
  end
139
139
 
@@ -243,9 +243,9 @@ describe Datagrid::FormBuilder do
243
243
  context "with invalid range value" do
244
244
  let(:_range) { Date.parse('2012-01-02')..Date.parse('2012-01-01') }
245
245
  it { should equal_to_dom(
246
- '<input class="created_at date_filter from" multiple value="2012-01-02" type="text" name="report[created_at][]"/>' +
246
+ '<input class="created_at date_filter from" multiple value="2012-01-01" type="text" name="report[created_at][]"/>' +
247
247
  '<span class="separator date"> - </span>' +
248
- '<input class="created_at date_filter to" multiple value="2012-01-01" type="text" name="report[created_at][]"/>'
248
+ '<input class="created_at date_filter to" multiple value="2012-01-02" type="text" name="report[created_at][]"/>'
249
249
  )}
250
250
  end
251
251
  context "with blank range value" do
@@ -15,6 +15,9 @@ describe Datagrid::Helper do
15
15
 
16
16
  before(:each) do
17
17
  allow(subject).to receive(:params).and_return({})
18
+ allow(subject).to receive(:request) do
19
+ Struct.new(:path, :query_parameters).new("/location", {})
20
+ end
18
21
  allow(subject).to receive(:url_for) do |options|
19
22
  options.is_a?(String) ? options : ["/location", options.to_param.presence].compact.join('?')
20
23
  end
@@ -70,7 +70,7 @@ class CssPattern
70
70
  if !html.match(pattern_or_string)
71
71
  return error!("#{css.inspect} did not match #{pattern_or_string.inspect}. It was \n:#{html.inspect}")
72
72
  end
73
- elsif amount_or_pattern_or_string_or_proc.is_a? Fixnum
73
+ elsif amount_or_pattern_or_string_or_proc.is_a? Numeric
74
74
  expected_amount = amount_or_pattern_or_string_or_proc
75
75
  amount = path.size
76
76
  if amount != expected_amount
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.5.4
4
+ version: 1.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bogdan Gusiev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-16 00:00:00.000000000 Z
11
+ date: 2017-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails