datagrid 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -23,5 +23,6 @@ group :development do
23
23
  gem "fastercsv"
24
24
  gem "nokogiri"
25
25
  gem 'will_paginate', '2.3.15'
26
+ gem "accept_values_for"
26
27
 
27
28
  end
@@ -2,6 +2,9 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
4
  abstract (1.0.0)
5
+ accept_values_for (0.4.3)
6
+ activemodel (>= 3.0.0)
7
+ rspec
5
8
  actionmailer (3.0.7)
6
9
  actionpack (= 3.0.7)
7
10
  mail (~> 2.2.15)
@@ -97,6 +100,7 @@ PLATFORMS
97
100
  ruby
98
101
 
99
102
  DEPENDENCIES
103
+ accept_values_for
100
104
  bundler (~> 1.0.0)
101
105
  fastercsv
102
106
  jeweler (~> 1.6.0)
@@ -20,15 +20,15 @@ In order to create a report, you need to define:
20
20
 
21
21
  ### Working grid example
22
22
 
23
+ [Datagrid DEMO application](https://github.com/bogdan/datagrid-demo) is available.
24
+
23
25
  In order to create a grid:
24
26
 
25
27
  ``` ruby
26
-
27
28
  class SimpleReport
28
29
 
29
30
  include Datagrid
30
31
 
31
-
32
32
  scope do
33
33
  User.includes(:group)
34
34
  end
@@ -42,7 +42,6 @@ class SimpleReport
42
42
  self.joins(:group).where(:groups => {:name => value})
43
43
  end
44
44
 
45
-
46
45
  column(:name)
47
46
  column(:group, :order => "groups.name") do |user|
48
47
  user.name
@@ -51,11 +50,7 @@ class SimpleReport
51
50
  !user.disabled
52
51
  end
53
52
 
54
-
55
-
56
53
  end
57
-
58
-
59
54
  ```
60
55
 
61
56
  Basic grid api:
@@ -136,18 +131,18 @@ Each column is sortable.
136
131
 
137
132
 
138
133
  In order to create form for your report you can use all set of rails built-in tools.
139
-
140
134
  More over Datagrid provides you two additional form helpers:
141
135
 
142
136
  * datagrid\_label
143
137
  * datagrid\_filter
144
138
 
145
- The easiest way to create a report form:
146
139
 
140
+ The easiest way to create a report form:
147
141
  (haml for readablity)
148
142
 
149
143
  ``` haml
150
- - form_for @report do |f|
144
+ # Method `GET` is recommended for all report forms by default.
145
+ - form_for @report, :html => {:method => :get} do |f|
151
146
  - @report.filters.each do |filter|
152
147
  %div
153
148
  = f.datagrid_label filter
@@ -155,14 +150,25 @@ The easiest way to create a report form:
155
150
  = f.submit
156
151
  ```
157
152
 
158
- There is a simple helper set of helpers that allows you display report:
153
+ Your controller:
159
154
 
155
+ ``` ruby
156
+ map.resources :simple_reports, :only => [:index]
157
+
158
+ class SimpleReportsController < ApplicationController
159
+ def index
160
+ @report = SimpleReport.new(params[:simple_report])
161
+ end
162
+ end
163
+ ```
164
+
165
+ There is a simple helper set of helpers that allows you display report:
160
166
  (require will_paginate)
161
167
 
162
168
  ``` haml
163
169
  - assets = @report.assets.paginate(:page => params[:page])
164
170
 
165
- %div== Total #{assets.total}
171
+ %div== Total #{assets.total_entries}
166
172
  = datagrid_table(@report, assets)
167
173
  = will_paginate assets
168
174
  ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.4
1
+ 0.3.5
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{datagrid}
8
- s.version = "0.3.4"
8
+ s.version = "0.3.5"
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 = %q{2011-08-10}
12
+ s.date = %q{2011-08-15}
13
13
  s.description = %q{This allows you to easily build datagrid aka data tables with sortable columns and filters}
14
14
  s.email = %q{agresso@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -26,9 +26,9 @@ Gem::Specification.new do |s|
26
26
  "VERSION",
27
27
  "datagrid.gemspec",
28
28
  "lib/datagrid.rb",
29
+ "lib/datagrid/active_model.rb",
29
30
  "lib/datagrid/columns.rb",
30
31
  "lib/datagrid/columns/column.rb",
31
- "lib/datagrid/conversion.rb",
32
32
  "lib/datagrid/core.rb",
33
33
  "lib/datagrid/filters.rb",
34
34
  "lib/datagrid/filters/base_filter.rb",
@@ -46,10 +46,13 @@ Gem::Specification.new do |s|
46
46
  "lib/datagrid/ordering.rb",
47
47
  "lib/datagrid/rspec.rb",
48
48
  "lib/datagrid/utils.rb",
49
+ "spec/datagrid/active_model_spec.rb",
49
50
  "spec/datagrid/columns_spec.rb",
51
+ "spec/datagrid/filters/enum_filter_spec.rb",
50
52
  "spec/datagrid/filters_spec.rb",
51
53
  "spec/datagrid/form_builder_spec.rb",
52
54
  "spec/datagrid/helper_spec.rb",
55
+ "spec/datagrid/ordering_spec.rb",
53
56
  "spec/datagrid_spec.rb",
54
57
  "spec/spec_helper.rb",
55
58
  "spec/support/equal_to_dom.rb",
@@ -79,6 +82,7 @@ Gem::Specification.new do |s|
79
82
  s.add_development_dependency(%q<fastercsv>, [">= 0"])
80
83
  s.add_development_dependency(%q<nokogiri>, [">= 0"])
81
84
  s.add_development_dependency(%q<will_paginate>, ["= 2.3.15"])
85
+ s.add_development_dependency(%q<accept_values_for>, [">= 0"])
82
86
  else
83
87
  s.add_dependency(%q<rails>, [">= 0"])
84
88
  s.add_dependency(%q<rake>, ["= 0.8.7"])
@@ -92,6 +96,7 @@ Gem::Specification.new do |s|
92
96
  s.add_dependency(%q<fastercsv>, [">= 0"])
93
97
  s.add_dependency(%q<nokogiri>, [">= 0"])
94
98
  s.add_dependency(%q<will_paginate>, ["= 2.3.15"])
99
+ s.add_dependency(%q<accept_values_for>, [">= 0"])
95
100
  end
96
101
  else
97
102
  s.add_dependency(%q<rails>, [">= 0"])
@@ -106,6 +111,7 @@ Gem::Specification.new do |s|
106
111
  s.add_dependency(%q<fastercsv>, [">= 0"])
107
112
  s.add_dependency(%q<nokogiri>, [">= 0"])
108
113
  s.add_dependency(%q<will_paginate>, ["= 2.3.15"])
114
+ s.add_dependency(%q<accept_values_for>, [">= 0"])
109
115
  end
110
116
  end
111
117
 
@@ -1,5 +1,5 @@
1
1
  require "datagrid/core"
2
- require "datagrid/conversion"
2
+ require "datagrid/active_model"
3
3
  require "datagrid/filters"
4
4
  require "datagrid/columns"
5
5
  require "datagrid/ordering"
@@ -14,7 +14,7 @@ module Datagrid
14
14
  base.class_eval do
15
15
 
16
16
  include ::Datagrid::Core
17
- include ::Datagrid::Conversion
17
+ include ::Datagrid::ActiveModel
18
18
  include ::Datagrid::Filters
19
19
  include ::Datagrid::Columns
20
20
  include ::Datagrid::Ordering
@@ -1,12 +1,17 @@
1
+
1
2
  module Datagrid
2
3
 
3
4
  # Required to be ActiveModel compatible
4
- module Conversion
5
+ module ActiveModel
5
6
 
6
7
  def self.included(base)
7
8
  base.extend ClassMethods
8
9
  base.class_eval do
9
-
10
+ begin
11
+ require 'active_model/naming'
12
+ extend ::ActiveModel::Naming
13
+ rescue LoadError
14
+ end
10
15
  end
11
16
  base.send :include, InstanceMethods
12
17
  end # self.included
@@ -17,9 +22,6 @@ module Datagrid
17
22
  self.to_s.underscore.split('/').last
18
23
  end
19
24
 
20
- def model_name
21
- self.param_name
22
- end
23
25
 
24
26
  end # ClassMethods
25
27
 
@@ -29,6 +31,10 @@ module Datagrid
29
31
  self.class.param_name
30
32
  end
31
33
 
34
+ def param_key
35
+ param_name
36
+ end
37
+
32
38
  def to_key
33
39
  [self.class.param_name]
34
40
  end
@@ -5,7 +5,7 @@ class Datagrid::Columns::Column
5
5
 
6
6
  def initialize(grid, name, options = {}, &block)
7
7
  self.grid = grid
8
- self.name = name
8
+ self.name = name.to_sym
9
9
  self.options = options
10
10
  self.block = block
11
11
  end
@@ -45,9 +45,9 @@ class Datagrid::Columns::Column
45
45
  end
46
46
  end
47
47
 
48
- def desc_order
48
+ def order_desc
49
49
  return nil unless order
50
- self.options[:desc_order]
50
+ self.options[:order_desc]
51
51
  end
52
52
 
53
53
  end
@@ -16,8 +16,11 @@ class Datagrid::Filters::BaseFilter
16
16
  end
17
17
 
18
18
  def apply(scope, value)
19
- return scope if value.nil? && !options[:allow_nil]
20
- return scope if value.blank? && !options[:allow_blank]
19
+ if value.nil?
20
+ return scope if !allow_nil?
21
+ else
22
+ return scope if value.blank? && !allow_blank?
23
+ end
21
24
  ::Datagrid::Filters::FilterEval.new(self, scope, value).run
22
25
  end
23
26
 
@@ -25,7 +28,7 @@ class Datagrid::Filters::BaseFilter
25
28
  if !self.multiple && value.is_a?(Array)
26
29
  raise Datagrid::ArgumentError, "#{grid.class}.#{name} filter can not accept Array argument. Use :multiple option."
27
30
  end
28
- values = Array(value)
31
+ values = Array.wrap(value)
29
32
  values.map! do |value|
30
33
  self.format(value)
31
34
  end
@@ -45,5 +48,14 @@ class Datagrid::Filters::BaseFilter
45
48
  def multiple
46
49
  self.options[:multiple]
47
50
  end
51
+
52
+ def allow_nil?
53
+ options.has_key?(:allow_nil) ? options[:allow_nil] : options[:allow_blank]
54
+ end
55
+
56
+ def allow_blank?
57
+ options[:allow_blank]
58
+ end
59
+
48
60
  end
49
61
 
@@ -11,7 +11,8 @@ class Datagrid::Filters::EnumFilter < Datagrid::Filters::BaseFilter
11
11
  end
12
12
 
13
13
  def select
14
- self.options[:select]
14
+ option = self.options[:select]
15
+ option.respond_to?(:call) ? option.call : option
15
16
  end
16
17
 
17
18
 
@@ -1,5 +1,5 @@
1
1
  class Datagrid::Filters::StringFilter < Datagrid::Filters::BaseFilter
2
2
  def format(value)
3
- value.to_s
3
+ value.nil? ? nil : value.to_s
4
4
  end
5
5
  end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Datagrid::ActiveModel do
4
+
5
+ class ActiveReport
6
+ include Datagrid::ActiveModel
7
+ end
8
+
9
+ describe ".model_name" do
10
+ it "should be generate from class name " do
11
+ ActiveReport.model_name.should == "ActiveReport"
12
+ end
13
+ it "should have ActiveModel naming conventions" do
14
+ ActiveReport.model_name.i18n_key.should == :active_report
15
+ end
16
+ end
17
+
18
+
19
+ end
@@ -49,37 +49,5 @@ describe Datagrid::Columns do
49
49
  report.rows.last.first.should be_false
50
50
  end
51
51
 
52
- describe "ordering" do
53
-
54
-
55
- it "should raise error if ordered by not existing column" do
56
- lambda {
57
- test_report(:order => :hello)
58
- }.should raise_error(Datagrid::OrderUnsupported)
59
- end
60
-
61
- it "should raise error if ordered by column without order" do
62
- lambda do
63
- test_report(:order => :category) do
64
- filter(:category, :default, :order => false) do |value|
65
- self
66
- end
67
- end
68
- end.should raise_error(Datagrid::OrderUnsupported)
69
- end
70
-
71
- it "should override default order" do
72
- Entry.all.should be_empty
73
- e1 = Entry.create!
74
- e2 = Entry.create!
75
- test_report(:order => :id) do
76
- scope { Entry.order("id desc")}
77
- column(:id) do
78
- self.order("id asc")
79
- end
80
- end.assets.all.should == [e1, e2]
81
- end
82
-
83
- end
84
52
 
85
53
  end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Datagrid::Filters::EnumFilter do
4
+
5
+ it "should support select option" do
6
+ test_report do
7
+ scope {Entry}
8
+ filter(:group_id, :enum, :select => [1,2] )
9
+ end.class.filter_by_name(:group_id).select.should == [1,2]
10
+ end
11
+
12
+ it "should support select option as proc" do
13
+ test_report do
14
+ scope {Entry}
15
+ filter(:group_id, :enum, :select => proc { [1,2] })
16
+ end.class.filter_by_name(:group_id).select.should == [1,2]
17
+ end
18
+
19
+ end
@@ -18,7 +18,7 @@ describe Datagrid::Filters do
18
18
  report.group_id = [1,2]
19
19
  }.should raise_error(Datagrid::ArgumentError)
20
20
  end
21
-
21
+
22
22
 
23
23
  it "should initialize report Scope table not exists" do
24
24
  class ModelWithoutTable < ActiveRecord::Base; end
@@ -31,7 +31,40 @@ describe Datagrid::Filters do
31
31
  filter(:name)
32
32
  end
33
33
  TheReport.new(:name => 'hello')
34
+ end
35
+
36
+ describe "allow_blank and allow_nil options" do
37
+ before(:each) do
38
+ $FILTER_PERFORMED = false
39
+ end
40
+
41
+ def check_performed(value, result, options)
42
+ report = test_report(:name => value) do
43
+ scope {Entry}
44
+ filter(:name, options) do |value|
45
+ $FILTER_PERFORMED = true
46
+ self
47
+ end
48
+ end
49
+ report.name.should == value
50
+ report.assets
51
+ $FILTER_PERFORMED.should == result
52
+ end
34
53
 
54
+ it "should support allow_blank argument" do
55
+ [nil, "", " "].each do |value|
56
+ check_performed(value, true, :allow_blank => true)
57
+ end
58
+ end
59
+
60
+ it "should support allow_blank argument" do
61
+ check_performed(nil, true, :allow_nil => true)
62
+ end
63
+
64
+ it "should support combination on allow_nil and allow_blank" do
65
+ check_performed(nil, false, :allow_nil => false, :allow_blank => true)
66
+ check_performed("", true, :allow_nil => false, :allow_blank => true)
67
+ check_performed(nil, true, :allow_nil => true, :allow_blank => false)
68
+ end
35
69
  end
36
-
37
70
  end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe Datagrid::Ordering do
4
+
5
+
6
+ let!(:first) { Entry.create!(:name => "aa")}
7
+ let!(:second) { Entry.create!(:name => "bb")}
8
+ let!(:third) { Entry.create!(:name => "cc")}
9
+
10
+
11
+ it "should support order" do
12
+ test_report(:order => "name") do
13
+ scope do
14
+ Entry
15
+ end
16
+ column :name
17
+ end.assets.should discover(first, second, third).with_exact_order
18
+
19
+ end
20
+
21
+ it "should support desc order" do
22
+ test_report(:order => "name", :descending => true) do
23
+ scope do
24
+ Entry
25
+ end
26
+ column :name
27
+ end.assets.should discover(third, second, first).with_exact_order
28
+
29
+
30
+ end
31
+
32
+
33
+ it "should raise error if ordered by not existing column" do
34
+ lambda {
35
+ test_report(:order => :hello)
36
+ }.should raise_error(Datagrid::OrderUnsupported)
37
+ end
38
+
39
+ it "should raise error if ordered by column without order" do
40
+ lambda do
41
+ test_report(:order => :category) do
42
+ filter(:category, :default, :order => false) do |value|
43
+ self
44
+ end
45
+ end
46
+ end.should raise_error(Datagrid::OrderUnsupported)
47
+ end
48
+
49
+ it "should override default order" do
50
+
51
+ test_report(:order => :id) do
52
+ scope { Entry.order("id desc")}
53
+ column(:id) do
54
+ self.order("id asc")
55
+ end
56
+ end.assets.should discover(first, second, third).with_exact_order
57
+ end
58
+
59
+ end
@@ -13,6 +13,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
13
  require "active_record"
14
14
  require "will_paginate"
15
15
  require 'datagrid'
16
+ require "discover"
16
17
  begin
17
18
  #require 'ruby-debug'
18
19
  rescue
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datagrid
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 4
10
- version: 0.3.4
9
+ - 5
10
+ version: 0.3.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bogdan Gusiev
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-10 00:00:00 +03:00
18
+ date: 2011-08-15 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -196,6 +196,20 @@ dependencies:
196
196
  type: :development
197
197
  name: will_paginate
198
198
  prerelease: false
199
+ - !ruby/object:Gem::Dependency
200
+ version_requirements: &id013 !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - ">="
204
+ - !ruby/object:Gem::Version
205
+ hash: 3
206
+ segments:
207
+ - 0
208
+ version: "0"
209
+ requirement: *id013
210
+ type: :development
211
+ name: accept_values_for
212
+ prerelease: false
199
213
  description: This allows you to easily build datagrid aka data tables with sortable columns and filters
200
214
  email: agresso@gmail.com
201
215
  executables: []
@@ -215,9 +229,9 @@ files:
215
229
  - VERSION
216
230
  - datagrid.gemspec
217
231
  - lib/datagrid.rb
232
+ - lib/datagrid/active_model.rb
218
233
  - lib/datagrid/columns.rb
219
234
  - lib/datagrid/columns/column.rb
220
- - lib/datagrid/conversion.rb
221
235
  - lib/datagrid/core.rb
222
236
  - lib/datagrid/filters.rb
223
237
  - lib/datagrid/filters/base_filter.rb
@@ -235,10 +249,13 @@ files:
235
249
  - lib/datagrid/ordering.rb
236
250
  - lib/datagrid/rspec.rb
237
251
  - lib/datagrid/utils.rb
252
+ - spec/datagrid/active_model_spec.rb
238
253
  - spec/datagrid/columns_spec.rb
254
+ - spec/datagrid/filters/enum_filter_spec.rb
239
255
  - spec/datagrid/filters_spec.rb
240
256
  - spec/datagrid/form_builder_spec.rb
241
257
  - spec/datagrid/helper_spec.rb
258
+ - spec/datagrid/ordering_spec.rb
242
259
  - spec/datagrid_spec.rb
243
260
  - spec/spec_helper.rb
244
261
  - spec/support/equal_to_dom.rb