datagrid 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  # Datagrid
2
2
 
3
- Ruby library that helps you to build and repsend table like data with:
3
+ Ruby library that helps you to build and represent table-like data with:
4
4
 
5
5
  * Customizable filtering
6
6
  * Columns
@@ -9,18 +9,13 @@ Ruby library that helps you to build and repsend table like data with:
9
9
  * Export to CSV
10
10
 
11
11
 
12
- *NOTE:* This gem is still under heavy development. If you find a bug don't consider this a peace of shit, just report it and I'll fix it shortly.
13
-
14
- *This is not trivial staff, so a really need your help guys.*
15
-
16
-
17
12
  ### Grid DSL
18
13
 
19
- In order to create a report You need to define:
14
+ In order to create a report, you need to define:
20
15
 
21
16
  * scope of ActiveRecord objects to look through
22
17
  * filters that will be used to filter data
23
- * columns that should be displayed and sortable(if possible)
18
+ * columns that should be displayed and sortable (if possible)
24
19
 
25
20
 
26
21
  ### Working grid example
@@ -125,6 +120,13 @@ Datagrid supports different type of filters including:
125
120
  ### Columns
126
121
 
127
122
  Each column is represented by name and code block to calculate the value.
123
+
124
+ ``` ruby
125
+ column(:activated, :header => "Active", :order => "activated") do
126
+ self.activated?
127
+ end
128
+ ```
129
+
128
130
  Some formatting options are also available.
129
131
  Each column is sortable.
130
132
 
@@ -132,6 +134,7 @@ Each column is sortable.
132
134
 
133
135
  ### Front end
134
136
 
137
+
135
138
  In order to create form for your report you can use all set of rails built-in tools.
136
139
 
137
140
  More over Datagrid provides you two additional form helpers:
@@ -164,5 +167,7 @@ There is a simple helper set of helpers that allows you display report:
164
167
  = will_paginate assets
165
168
  ```
166
169
 
170
+ If you need a custom interface for your report you should probably build it yourself with datagrid helpers.
171
+
167
172
  [More about frontend](https://github.com/bogdan/datagrid/wiki/Frontend)
168
173
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.3
1
+ 0.3.4
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{datagrid}
8
- s.version = "0.3.3"
8
+ s.version = "0.3.4"
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-07-07}
12
+ s.date = %q{2011-08-10}
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 = [
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "lib/datagrid.rb",
29
29
  "lib/datagrid/columns.rb",
30
30
  "lib/datagrid/columns/column.rb",
31
+ "lib/datagrid/conversion.rb",
31
32
  "lib/datagrid/core.rb",
32
33
  "lib/datagrid/filters.rb",
33
34
  "lib/datagrid/filters/base_filter.rb",
@@ -42,6 +43,7 @@ Gem::Specification.new do |s|
42
43
  "lib/datagrid/filters/string_filter.rb",
43
44
  "lib/datagrid/form_builder.rb",
44
45
  "lib/datagrid/helper.rb",
46
+ "lib/datagrid/ordering.rb",
45
47
  "lib/datagrid/rspec.rb",
46
48
  "lib/datagrid/utils.rb",
47
49
  "spec/datagrid/columns_spec.rb",
@@ -57,10 +59,11 @@ Gem::Specification.new do |s|
57
59
  s.homepage = %q{http://github.com/bogdan/datagrid}
58
60
  s.licenses = ["MIT"]
59
61
  s.require_paths = ["lib"]
60
- s.rubygems_version = %q{1.6.2}
62
+ s.rubygems_version = %q{1.3.7}
61
63
  s.summary = %q{Ruby gem to create datagrids}
62
64
 
63
65
  if s.respond_to? :specification_version then
66
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
64
67
  s.specification_version = 3
65
68
 
66
69
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
@@ -1,6 +1,8 @@
1
+ require "datagrid/core"
2
+ require "datagrid/conversion"
1
3
  require "datagrid/filters"
2
4
  require "datagrid/columns"
3
- require "datagrid/core"
5
+ require "datagrid/ordering"
4
6
 
5
7
  require "datagrid/helper"
6
8
  require "datagrid/form_builder"
@@ -12,8 +14,10 @@ module Datagrid
12
14
  base.class_eval do
13
15
 
14
16
  include ::Datagrid::Core
17
+ include ::Datagrid::Conversion
15
18
  include ::Datagrid::Filters
16
19
  include ::Datagrid::Columns
20
+ include ::Datagrid::Ordering
17
21
 
18
22
  end
19
23
  base.send :include, InstanceMethods
@@ -2,8 +2,6 @@ require "datagrid/utils"
2
2
 
3
3
  module Datagrid
4
4
 
5
- class OrderUnsupported < StandardError
6
- end
7
5
 
8
6
  module Columns
9
7
  require "datagrid/columns/column"
@@ -14,28 +12,6 @@ module Datagrid
14
12
 
15
13
  include Datagrid::Core
16
14
 
17
- datagrid_attribute :order do |value|
18
- unless value.blank?
19
- value = value.to_sym
20
- column = column_by_name(value)
21
- unless column
22
- order_unsupported(value, "no column #{value} in #{self.class}")
23
- end
24
- unless column.order
25
- order_unsupported(
26
- name, "#{self.class}##{name} don't support order"
27
- )
28
- end
29
- value
30
- else
31
- nil
32
- end
33
-
34
- end
35
- datagrid_attribute :descending do |value|
36
- Datagrid::Utils.booleanize(value)
37
- end
38
-
39
15
 
40
16
  end
41
17
  base.send :include, InstanceMethods
@@ -55,10 +31,6 @@ module Datagrid
55
31
  self.columns << Datagrid::Columns::Column.new(self, name, options, &block)
56
32
  end
57
33
 
58
- def order_unsupported(name, reason)
59
- raise Datagrid::OrderUnsupported, "Can not sort #{self.inspect} by ##{name}: #{reason}"
60
- end
61
-
62
34
  def column_by_name(name)
63
35
  self.columns.find do |col|
64
36
  col.name.to_sym == name.to_sym
@@ -105,15 +77,6 @@ module Datagrid
105
77
  end
106
78
  end
107
79
 
108
- def assets
109
- result = super
110
- if self.order
111
- column = column_by_name(self.order)
112
- result = apply_order(result, self.descending ? column.desc_order : column.order)
113
- end
114
- result
115
- end
116
-
117
80
  def to_csv(options = {})
118
81
  klass = if RUBY_VERSION >= "1.9"
119
82
  require 'csv'
@@ -139,14 +102,6 @@ module Datagrid
139
102
  self.class.column_by_name(name)
140
103
  end
141
104
 
142
- private
143
-
144
- def apply_order(assets, order)
145
- # Rails 3.0.x don't able to override already applied order
146
- # Using #reorder instead
147
- assets.respond_to?(:reorder) ? assets.reorder(order) : assets.order(order)
148
- end
149
-
150
105
  end # InstanceMethods
151
106
 
152
107
  end
@@ -46,7 +46,8 @@ class Datagrid::Columns::Column
46
46
  end
47
47
 
48
48
  def desc_order
49
- self.options[:desc_order] || (order ? order.to_s + " DESC" : nil)
49
+ return nil unless order
50
+ self.options[:desc_order]
50
51
  end
51
52
 
52
53
  end
@@ -0,0 +1,52 @@
1
+ module Datagrid
2
+
3
+ # Required to be ActiveModel compatible
4
+ module Conversion
5
+
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ base.class_eval do
9
+
10
+ end
11
+ base.send :include, InstanceMethods
12
+ end # self.included
13
+
14
+ module ClassMethods
15
+
16
+ def param_name
17
+ self.to_s.underscore.split('/').last
18
+ end
19
+
20
+ def model_name
21
+ self.param_name
22
+ end
23
+
24
+ end # ClassMethods
25
+
26
+ module InstanceMethods
27
+
28
+ def param_name
29
+ self.class.param_name
30
+ end
31
+
32
+ def to_key
33
+ [self.class.param_name]
34
+ end
35
+
36
+ def persisted?
37
+ false
38
+ end
39
+
40
+ def to_model
41
+ self
42
+ end
43
+
44
+ def to_param
45
+ self.param_name
46
+ end
47
+ end # InstanceMethods
48
+
49
+ end
50
+
51
+
52
+ end
@@ -1,3 +1,5 @@
1
+ require "active_record"
2
+
1
3
  module Datagrid
2
4
  module Core
3
5
 
@@ -40,14 +42,6 @@ module Datagrid
40
42
  end
41
43
  end
42
44
 
43
- def param_name
44
- self.to_s.underscore.split('/').last
45
- end
46
-
47
- # Required for ActionView form builder
48
- def model_name
49
- self.param_name
50
- end
51
45
 
52
46
  protected
53
47
  def check_scope_defined!(message = "Scope not defined")
@@ -101,19 +95,6 @@ module Datagrid
101
95
  end
102
96
 
103
97
 
104
- def param_name
105
- self.class.param_name
106
- end
107
-
108
- # Required for ActionView form builder
109
- def to_key
110
- [self.class.param_name]
111
- end
112
-
113
- # Required for ActionView form builder
114
- def persisted?
115
- false
116
- end
117
98
 
118
99
  protected
119
100
 
@@ -2,7 +2,9 @@ class Datagrid::Filters::BooleanEnumFilter < Datagrid::Filters::EnumFilter
2
2
 
3
3
  YES = "YES"
4
4
  NO = "NO"
5
- VALUES = {YES => true, NO => false}
5
+ VALUES = ActiveSupport::OrderedHash.new
6
+ VALUES[YES] = YES
7
+ VALUES[NO] = NO
6
8
 
7
9
  def initialize(report, attribute, options = {}, &block)
8
10
  options[:select] = VALUES.keys
@@ -1,7 +1,8 @@
1
+ require "datagrid/utils"
1
2
  class Datagrid::Filters::BooleanFilter < Datagrid::Filters::BaseFilter
2
3
 
3
4
  def format(value)
4
- [true, 1, "1", "true", "yes", "on"].include?(value)
5
+ Datagrid::Utils.booleanize(value)
5
6
  end
6
7
 
7
8
  end
@@ -0,0 +1,87 @@
1
+ require "datagrid/columns"
2
+
3
+ module Datagrid
4
+ class OrderUnsupported < StandardError
5
+ end
6
+ module Ordering
7
+
8
+ def self.included(base)
9
+ base.extend ClassMethods
10
+ base.class_eval do
11
+ include Datagrid::Columns
12
+
13
+ datagrid_attribute :order do |value|
14
+ unless value.blank?
15
+ value = value.to_sym
16
+ column = column_by_name(value)
17
+ unless column
18
+ order_unsupported(value, "no column #{value} in #{self.class}")
19
+ end
20
+ unless column.order
21
+ order_unsupported(
22
+ name, "#{self.class}##{name} don't support order"
23
+ )
24
+ end
25
+ value
26
+ else
27
+ nil
28
+ end
29
+
30
+ end
31
+
32
+ datagrid_attribute :descending do |value|
33
+ Datagrid::Utils.booleanize(value)
34
+ end
35
+ alias descending? descending
36
+
37
+ end
38
+ base.send :include, InstanceMethods
39
+ end # self.included
40
+
41
+ module ClassMethods
42
+
43
+ def order_unsupported(name, reason)
44
+ raise Datagrid::OrderUnsupported, "Can not sort #{self.inspect} by ##{name}: #{reason}"
45
+ end
46
+
47
+ end # ClassMethods
48
+
49
+ module InstanceMethods
50
+
51
+ def assets
52
+ result = super
53
+ if self.order
54
+ column = column_by_name(self.order)
55
+ result = apply_order(result, column)
56
+ end
57
+ result
58
+ end
59
+
60
+ private
61
+
62
+ def apply_order(assets, column)
63
+
64
+ # Rails 3.0.x don't able to override already applied order
65
+ # Using #reorder instead
66
+ order_method = assets.respond_to?(:reorder) ? :reorder : :order # Rails 3?
67
+
68
+ order = column.order
69
+ if self.descending?
70
+ if column.order_desc
71
+ assets.send(order_method, column.order_desc)
72
+ else
73
+ if assets.respond_to?(:reverse_order) # Rails 3?
74
+ assets.reorder(order).reverse_order
75
+ else
76
+ assets.order(ActiveRecord::Base.send(:reverse_sql_order, order))
77
+ end
78
+ end
79
+ else
80
+ assets.send(order_method, order)
81
+ end
82
+ end
83
+
84
+ end # InstanceMethods
85
+
86
+ end
87
+ end
@@ -2,7 +2,7 @@ module Datagrid
2
2
  module Utils
3
3
  class << self
4
4
 
5
- TRUTH =["1", true, 1, "true", "yes"]
5
+ TRUTH = [true, 1, "1", "true", "yes", "on"]
6
6
 
7
7
  def booleanize(value)
8
8
  TRUTH.include?(value)
@@ -22,6 +22,7 @@ describe Datagrid::Filters do
22
22
 
23
23
  it "should initialize report Scope table not exists" do
24
24
  class ModelWithoutTable < ActiveRecord::Base; end
25
+ ModelWithoutTable.should_not be_table_exists
25
26
  class TheReport
26
27
  include Datagrid
27
28
 
@@ -29,6 +30,7 @@ describe Datagrid::Filters do
29
30
 
30
31
  filter(:name)
31
32
  end
33
+ TheReport.new(:name => 'hello')
32
34
 
33
35
  end
34
36
 
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datagrid
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.3.3
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 4
10
+ version: 0.3.4
6
11
  platform: ruby
7
12
  authors:
8
13
  - Bogdan Gusiev
@@ -10,141 +15,187 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2011-07-07 00:00:00 +03:00
18
+ date: 2011-08-10 00:00:00 +03:00
14
19
  default_executable:
15
20
  dependencies:
16
21
  - !ruby/object:Gem::Dependency
17
- name: rails
18
- requirement: &id001 !ruby/object:Gem::Requirement
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
19
23
  none: false
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
23
30
  version: "0"
31
+ requirement: *id001
24
32
  type: :runtime
33
+ name: rails
25
34
  prerelease: false
26
- version_requirements: *id001
27
35
  - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: &id002 !ruby/object:Gem::Requirement
36
+ version_requirements: &id002 !ruby/object:Gem::Requirement
30
37
  none: false
31
38
  requirements:
32
39
  - - "="
33
40
  - !ruby/object:Gem::Version
41
+ hash: 49
42
+ segments:
43
+ - 0
44
+ - 8
45
+ - 7
34
46
  version: 0.8.7
47
+ requirement: *id002
35
48
  type: :development
49
+ name: rake
36
50
  prerelease: false
37
- version_requirements: *id002
38
51
  - !ruby/object:Gem::Dependency
39
- name: mocha
40
- requirement: &id003 !ruby/object:Gem::Requirement
52
+ version_requirements: &id003 !ruby/object:Gem::Requirement
41
53
  none: false
42
54
  requirements:
43
55
  - - ">="
44
56
  - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
45
60
  version: "0"
61
+ requirement: *id003
46
62
  type: :development
63
+ name: mocha
47
64
  prerelease: false
48
- version_requirements: *id003
49
65
  - !ruby/object:Gem::Dependency
50
- name: rspec
51
- requirement: &id004 !ruby/object:Gem::Requirement
66
+ version_requirements: &id004 !ruby/object:Gem::Requirement
52
67
  none: false
53
68
  requirements:
54
69
  - - "="
55
70
  - !ruby/object:Gem::Version
71
+ hash: 23
72
+ segments:
73
+ - 2
74
+ - 6
75
+ - 0
56
76
  version: 2.6.0
77
+ requirement: *id004
57
78
  type: :development
79
+ name: rspec
58
80
  prerelease: false
59
- version_requirements: *id004
60
81
  - !ruby/object:Gem::Dependency
61
- name: bundler
62
- requirement: &id005 !ruby/object:Gem::Requirement
82
+ version_requirements: &id005 !ruby/object:Gem::Requirement
63
83
  none: false
64
84
  requirements:
65
85
  - - ~>
66
86
  - !ruby/object:Gem::Version
87
+ hash: 23
88
+ segments:
89
+ - 1
90
+ - 0
91
+ - 0
67
92
  version: 1.0.0
93
+ requirement: *id005
68
94
  type: :development
95
+ name: bundler
69
96
  prerelease: false
70
- version_requirements: *id005
71
97
  - !ruby/object:Gem::Dependency
72
- name: jeweler
73
- requirement: &id006 !ruby/object:Gem::Requirement
98
+ version_requirements: &id006 !ruby/object:Gem::Requirement
74
99
  none: false
75
100
  requirements:
76
101
  - - ~>
77
102
  - !ruby/object:Gem::Version
103
+ hash: 15
104
+ segments:
105
+ - 1
106
+ - 6
107
+ - 0
78
108
  version: 1.6.0
109
+ requirement: *id006
79
110
  type: :development
111
+ name: jeweler
80
112
  prerelease: false
81
- version_requirements: *id006
82
113
  - !ruby/object:Gem::Dependency
83
- name: rcov
84
- requirement: &id007 !ruby/object:Gem::Requirement
114
+ version_requirements: &id007 !ruby/object:Gem::Requirement
85
115
  none: false
86
116
  requirements:
87
117
  - - ">="
88
118
  - !ruby/object:Gem::Version
119
+ hash: 3
120
+ segments:
121
+ - 0
89
122
  version: "0"
123
+ requirement: *id007
90
124
  type: :development
125
+ name: rcov
91
126
  prerelease: false
92
- version_requirements: *id007
93
127
  - !ruby/object:Gem::Dependency
94
- name: ruby-debug
95
- requirement: &id008 !ruby/object:Gem::Requirement
128
+ version_requirements: &id008 !ruby/object:Gem::Requirement
96
129
  none: false
97
130
  requirements:
98
131
  - - ">="
99
132
  - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
100
136
  version: "0"
137
+ requirement: *id008
101
138
  type: :development
139
+ name: ruby-debug
102
140
  prerelease: false
103
- version_requirements: *id008
104
141
  - !ruby/object:Gem::Dependency
105
- name: sqlite3-ruby
106
- requirement: &id009 !ruby/object:Gem::Requirement
142
+ version_requirements: &id009 !ruby/object:Gem::Requirement
107
143
  none: false
108
144
  requirements:
109
145
  - - ">="
110
146
  - !ruby/object:Gem::Version
147
+ hash: 3
148
+ segments:
149
+ - 0
111
150
  version: "0"
151
+ requirement: *id009
112
152
  type: :development
153
+ name: sqlite3-ruby
113
154
  prerelease: false
114
- version_requirements: *id009
115
155
  - !ruby/object:Gem::Dependency
116
- name: fastercsv
117
- requirement: &id010 !ruby/object:Gem::Requirement
156
+ version_requirements: &id010 !ruby/object:Gem::Requirement
118
157
  none: false
119
158
  requirements:
120
159
  - - ">="
121
160
  - !ruby/object:Gem::Version
161
+ hash: 3
162
+ segments:
163
+ - 0
122
164
  version: "0"
165
+ requirement: *id010
123
166
  type: :development
167
+ name: fastercsv
124
168
  prerelease: false
125
- version_requirements: *id010
126
169
  - !ruby/object:Gem::Dependency
127
- name: nokogiri
128
- requirement: &id011 !ruby/object:Gem::Requirement
170
+ version_requirements: &id011 !ruby/object:Gem::Requirement
129
171
  none: false
130
172
  requirements:
131
173
  - - ">="
132
174
  - !ruby/object:Gem::Version
175
+ hash: 3
176
+ segments:
177
+ - 0
133
178
  version: "0"
179
+ requirement: *id011
134
180
  type: :development
181
+ name: nokogiri
135
182
  prerelease: false
136
- version_requirements: *id011
137
183
  - !ruby/object:Gem::Dependency
138
- name: will_paginate
139
- requirement: &id012 !ruby/object:Gem::Requirement
184
+ version_requirements: &id012 !ruby/object:Gem::Requirement
140
185
  none: false
141
186
  requirements:
142
187
  - - "="
143
188
  - !ruby/object:Gem::Version
189
+ hash: 29
190
+ segments:
191
+ - 2
192
+ - 3
193
+ - 15
144
194
  version: 2.3.15
195
+ requirement: *id012
145
196
  type: :development
197
+ name: will_paginate
146
198
  prerelease: false
147
- version_requirements: *id012
148
199
  description: This allows you to easily build datagrid aka data tables with sortable columns and filters
149
200
  email: agresso@gmail.com
150
201
  executables: []
@@ -166,6 +217,7 @@ files:
166
217
  - lib/datagrid.rb
167
218
  - lib/datagrid/columns.rb
168
219
  - lib/datagrid/columns/column.rb
220
+ - lib/datagrid/conversion.rb
169
221
  - lib/datagrid/core.rb
170
222
  - lib/datagrid/filters.rb
171
223
  - lib/datagrid/filters/base_filter.rb
@@ -180,6 +232,7 @@ files:
180
232
  - lib/datagrid/filters/string_filter.rb
181
233
  - lib/datagrid/form_builder.rb
182
234
  - lib/datagrid/helper.rb
235
+ - lib/datagrid/ordering.rb
183
236
  - lib/datagrid/rspec.rb
184
237
  - lib/datagrid/utils.rb
185
238
  - spec/datagrid/columns_spec.rb
@@ -205,7 +258,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
205
258
  requirements:
206
259
  - - ">="
207
260
  - !ruby/object:Gem::Version
208
- hash: 2656529442255327450
261
+ hash: 3
209
262
  segments:
210
263
  - 0
211
264
  version: "0"
@@ -214,11 +267,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
214
267
  requirements:
215
268
  - - ">="
216
269
  - !ruby/object:Gem::Version
270
+ hash: 3
271
+ segments:
272
+ - 0
217
273
  version: "0"
218
274
  requirements: []
219
275
 
220
276
  rubyforge_project:
221
- rubygems_version: 1.6.2
277
+ rubygems_version: 1.3.7
222
278
  signing_key:
223
279
  specification_version: 3
224
280
  summary: Ruby gem to create datagrids