datagrid 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -6,7 +6,12 @@ Ruby library that helps you to build and repsend table like data with:
6
6
  * Columns
7
7
  * Sort order
8
8
  * Localization
9
- * Exportort to CSV
9
+ * Export to CSV
10
+
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.*
10
15
 
11
16
 
12
17
  ### Grid DSL
@@ -66,7 +71,7 @@ report = SimpleReport.new(:order => "group", :reverse => true, :group_id => [1,2
66
71
  report.assets # => Array of User instances:
67
72
  # SELECT * FROM users WHERE users.group_id in (1,2) AND users.logins_count >= 1 AND users.category = 'first' ORDER BY groups.name DESC
68
73
 
69
- report.header # => ["Group", "Name", "Disabled"]
74
+ report.header # => ["Group", "Name", "Activated"]
70
75
  report.rows # => [
71
76
  # ["Steve", "Spammers", true],
72
77
  # [ "John", "Spoilers", true],
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{datagrid}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
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-06-21}
12
+ s.date = %q{2011-06-22}
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 = [
@@ -43,6 +43,7 @@ Gem::Specification.new do |s|
43
43
  "lib/datagrid/helper.rb",
44
44
  "lib/datagrid/rspec.rb",
45
45
  "spec/datagrid/columns_spec.rb",
46
+ "spec/datagrid/filters_spec.rb",
46
47
  "spec/datagrid/form_builder_spec.rb",
47
48
  "spec/datagrid/helper_spec.rb",
48
49
  "spec/datagrid_spec.rb",
@@ -99,6 +99,10 @@ module Datagrid
99
99
  def param_name
100
100
  self.class.param_name
101
101
  end
102
+
103
+ def to_key
104
+ [self.class.param_name]
105
+ end
102
106
 
103
107
  protected
104
108
 
@@ -103,7 +103,7 @@ module Datagrid
103
103
  def filter(attribute, type = :string, options = {}, &block)
104
104
 
105
105
  klass = type.is_a?(Class) ? type : FILTER_TYPES[type]
106
- raise ConfigurationError, "filter class not found" unless klass
106
+ raise ConfigurationError, "filter class #{type.inspect} not found" unless klass
107
107
 
108
108
  block ||= default_filter(attribute)
109
109
 
@@ -35,7 +35,8 @@ class Datagrid::Filters::BaseFilter
35
35
  end
36
36
 
37
37
  def default
38
- self.options[:default]
38
+ default = self.options[:default]
39
+ default.respond_to?(:call) ? default.call : default
39
40
  end
40
41
 
41
42
  def multiple
@@ -12,7 +12,7 @@ module Datagrid
12
12
  when :url
13
13
  link_to(column.label ? asset.send(column.label) : I18n.t("datagrid.table.url_label", :default => "URL"), value)
14
14
  else
15
- value
15
+ _safe(value)
16
16
  end
17
17
  end
18
18
  end
@@ -21,14 +21,11 @@ module Datagrid
21
21
  options = args.extract_options!
22
22
  html = options[:html] || {}
23
23
  html[:class] ||= "datagrid"
24
- paginate = options[:paginate] || {}
25
- paginate[:page] ||= params[:page]
26
- assets = report.assets.paginate(paginate)
27
- content_tag(:table, html) do
28
- table = content_tag(:tr, datagrid_header(report, options))
29
- table << datagrid_rows(report.columns, assets, options)
30
- table
31
- end
24
+ assets = report.assets
25
+ paginate = options[:paginate]
26
+ assets = assets.paginate(paginate) if paginate
27
+ content = content_tag(:tr, datagrid_header(report, options)) + datagrid_rows(report.columns, assets, options)
28
+ content_tag(:table, content, html)
32
29
  end
33
30
 
34
31
  protected
@@ -36,7 +33,7 @@ module Datagrid
36
33
  def datagrid_header(grid, options)
37
34
  header = empty_string
38
35
  grid.columns.each do |column|
39
- data = column.header.html_safe
36
+ data = _safe(column.header)
40
37
  if column.order
41
38
  data << datagrid_order_for(grid, column)
42
39
  end
@@ -46,32 +43,36 @@ module Datagrid
46
43
  end
47
44
 
48
45
  def datagrid_rows(columns, assets, options)
49
- rows = empty_string
50
- assets.each do |asset|
51
- rows << content_tag(:tr, :class => cycle("odd", "even")) do
52
- html = empty_string
53
- columns.each do |column|
54
- html << content_tag(:td, datagrid_format_value(column, asset))
55
- end
56
- html
57
- end
58
-
59
- end
60
- rows
46
+ result = assets.map do |asset|
47
+ content = columns.map do |column|
48
+ content_tag(:td, datagrid_format_value(column, asset))
49
+ end.join(empty_string)
50
+ content_tag(:tr, _safe(content), :class => cycle("odd", "even"))
51
+ end.join(empty_string)
52
+ _safe(result)
61
53
  end
62
54
 
63
55
  def datagrid_order_for(grid, column)
64
56
  content_tag(:div, :class => "order") do
65
57
  link_to(
66
- I18n.t("datagrid.table.order.asc", :default => "ASC"), url_for(grid.param_name => grid.attributes.merge(:order => column.name))
67
- ) + " " +
68
- link_to(I18n.t("datagrid.table.order.desc", :default => "DESC"), url_for(grid.param_name => grid.attributes.merge(:order => column.name, :reverse => true )))
58
+ I18n.t("datagrid.table.order.asc", :default => "ASC"),
59
+ url_for(grid.param_name => grid.attributes.merge(:order => column.name)),
60
+ :class => "order asc"
61
+ ) + " " + link_to(
62
+ I18n.t("datagrid.table.order.desc", :default => "DESC"),
63
+ url_for(grid.param_name => grid.attributes.merge(:order => column.name, :reverse => true )),
64
+ :class => "order desc"
65
+ )
69
66
  end
70
67
  end
71
68
 
69
+ protected
72
70
  def empty_string
73
- res = ""
74
- res.respond_to?(:html_safe) ? res.html_safe : res
71
+ _safe("")
72
+ end
73
+
74
+ def _safe(string)
75
+ string.respond_to?(:html_safe) ? string.html_safe : string
75
76
  end
76
77
  end
77
78
 
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Datagrid::Filters do
4
+
5
+ it "should support default option as proc" do
6
+ test_report do
7
+ scope {Entry}
8
+ filter(:created_at, :date, :default => proc { Date.today } )
9
+ end.created_at.should == Date.today
10
+ end
11
+
12
+ end
@@ -1,8 +1,18 @@
1
+
2
+
3
+ def test_report(&block)
4
+ klass = Class.new
5
+ klass.class_eval do
6
+ include Datagrid
7
+ end
8
+ klass.class_eval(&block)
9
+ klass.new
10
+ end
11
+
1
12
  class SimpleReport
2
13
 
3
14
  include Datagrid
4
15
 
5
-
6
16
  scope do
7
17
  ::Entry.includes(:group).order("entries.created_at")
8
18
  end
@@ -11,6 +21,7 @@ class SimpleReport
11
21
  filter(:category, :enum, :select => ["first", "second"])
12
22
  filter(:disabled, :eboolean)
13
23
  filter(:confirmed, :boolean)
24
+
14
25
  filter(:name) do |value|
15
26
  self.scoped(:conditions => {:name => value})
16
27
  end
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
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bogdan Gusiev
@@ -15,12 +15,11 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-21 00:00:00 +03:00
18
+ date: 2011-06-22 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: rails
23
- requirement: &id001 !ruby/object:Gem::Requirement
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
23
  none: false
25
24
  requirements:
26
25
  - - ">="
@@ -29,12 +28,12 @@ dependencies:
29
28
  segments:
30
29
  - 0
31
30
  version: "0"
32
- prerelease: false
31
+ requirement: *id001
33
32
  type: :runtime
34
- version_requirements: *id001
33
+ name: rails
34
+ prerelease: false
35
35
  - !ruby/object:Gem::Dependency
36
- name: rake
37
- requirement: &id002 !ruby/object:Gem::Requirement
36
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
37
  none: false
39
38
  requirements:
40
39
  - - "="
@@ -45,12 +44,12 @@ dependencies:
45
44
  - 8
46
45
  - 7
47
46
  version: 0.8.7
48
- prerelease: false
47
+ requirement: *id002
49
48
  type: :development
50
- version_requirements: *id002
49
+ name: rake
50
+ prerelease: false
51
51
  - !ruby/object:Gem::Dependency
52
- name: mocha
53
- requirement: &id003 !ruby/object:Gem::Requirement
52
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
53
  none: false
55
54
  requirements:
56
55
  - - ">="
@@ -59,12 +58,12 @@ dependencies:
59
58
  segments:
60
59
  - 0
61
60
  version: "0"
62
- prerelease: false
61
+ requirement: *id003
63
62
  type: :development
64
- version_requirements: *id003
63
+ name: mocha
64
+ prerelease: false
65
65
  - !ruby/object:Gem::Dependency
66
- name: rspec
67
- requirement: &id004 !ruby/object:Gem::Requirement
66
+ version_requirements: &id004 !ruby/object:Gem::Requirement
68
67
  none: false
69
68
  requirements:
70
69
  - - "="
@@ -75,12 +74,12 @@ dependencies:
75
74
  - 6
76
75
  - 0
77
76
  version: 2.6.0
78
- prerelease: false
77
+ requirement: *id004
79
78
  type: :development
80
- version_requirements: *id004
79
+ name: rspec
80
+ prerelease: false
81
81
  - !ruby/object:Gem::Dependency
82
- name: bundler
83
- requirement: &id005 !ruby/object:Gem::Requirement
82
+ version_requirements: &id005 !ruby/object:Gem::Requirement
84
83
  none: false
85
84
  requirements:
86
85
  - - ~>
@@ -91,12 +90,12 @@ dependencies:
91
90
  - 0
92
91
  - 0
93
92
  version: 1.0.0
94
- prerelease: false
93
+ requirement: *id005
95
94
  type: :development
96
- version_requirements: *id005
95
+ name: bundler
96
+ prerelease: false
97
97
  - !ruby/object:Gem::Dependency
98
- name: jeweler
99
- requirement: &id006 !ruby/object:Gem::Requirement
98
+ version_requirements: &id006 !ruby/object:Gem::Requirement
100
99
  none: false
101
100
  requirements:
102
101
  - - ~>
@@ -107,12 +106,12 @@ dependencies:
107
106
  - 6
108
107
  - 0
109
108
  version: 1.6.0
110
- prerelease: false
109
+ requirement: *id006
111
110
  type: :development
112
- version_requirements: *id006
111
+ name: jeweler
112
+ prerelease: false
113
113
  - !ruby/object:Gem::Dependency
114
- name: rcov
115
- requirement: &id007 !ruby/object:Gem::Requirement
114
+ version_requirements: &id007 !ruby/object:Gem::Requirement
116
115
  none: false
117
116
  requirements:
118
117
  - - ">="
@@ -121,12 +120,12 @@ dependencies:
121
120
  segments:
122
121
  - 0
123
122
  version: "0"
124
- prerelease: false
123
+ requirement: *id007
125
124
  type: :development
126
- version_requirements: *id007
125
+ name: rcov
126
+ prerelease: false
127
127
  - !ruby/object:Gem::Dependency
128
- name: ruby-debug
129
- requirement: &id008 !ruby/object:Gem::Requirement
128
+ version_requirements: &id008 !ruby/object:Gem::Requirement
130
129
  none: false
131
130
  requirements:
132
131
  - - ">="
@@ -135,12 +134,12 @@ dependencies:
135
134
  segments:
136
135
  - 0
137
136
  version: "0"
138
- prerelease: false
137
+ requirement: *id008
139
138
  type: :development
140
- version_requirements: *id008
139
+ name: ruby-debug
140
+ prerelease: false
141
141
  - !ruby/object:Gem::Dependency
142
- name: sqlite3-ruby
143
- requirement: &id009 !ruby/object:Gem::Requirement
142
+ version_requirements: &id009 !ruby/object:Gem::Requirement
144
143
  none: false
145
144
  requirements:
146
145
  - - ">="
@@ -149,12 +148,12 @@ dependencies:
149
148
  segments:
150
149
  - 0
151
150
  version: "0"
152
- prerelease: false
151
+ requirement: *id009
153
152
  type: :development
154
- version_requirements: *id009
153
+ name: sqlite3-ruby
154
+ prerelease: false
155
155
  - !ruby/object:Gem::Dependency
156
- name: fastercsv
157
- requirement: &id010 !ruby/object:Gem::Requirement
156
+ version_requirements: &id010 !ruby/object:Gem::Requirement
158
157
  none: false
159
158
  requirements:
160
159
  - - ">="
@@ -163,12 +162,12 @@ dependencies:
163
162
  segments:
164
163
  - 0
165
164
  version: "0"
166
- prerelease: false
165
+ requirement: *id010
167
166
  type: :development
168
- version_requirements: *id010
167
+ name: fastercsv
168
+ prerelease: false
169
169
  - !ruby/object:Gem::Dependency
170
- name: nokogiri
171
- requirement: &id011 !ruby/object:Gem::Requirement
170
+ version_requirements: &id011 !ruby/object:Gem::Requirement
172
171
  none: false
173
172
  requirements:
174
173
  - - ">="
@@ -177,12 +176,12 @@ dependencies:
177
176
  segments:
178
177
  - 0
179
178
  version: "0"
180
- prerelease: false
179
+ requirement: *id011
181
180
  type: :development
182
- version_requirements: *id011
181
+ name: nokogiri
182
+ prerelease: false
183
183
  - !ruby/object:Gem::Dependency
184
- name: will_paginate
185
- requirement: &id012 !ruby/object:Gem::Requirement
184
+ version_requirements: &id012 !ruby/object:Gem::Requirement
186
185
  none: false
187
186
  requirements:
188
187
  - - "="
@@ -193,9 +192,10 @@ dependencies:
193
192
  - 3
194
193
  - 15
195
194
  version: 2.3.15
196
- prerelease: false
195
+ requirement: *id012
197
196
  type: :development
198
- version_requirements: *id012
197
+ name: will_paginate
198
+ prerelease: false
199
199
  description: This allows you to easily build datagrid aka data tables with sortable columns and filters
200
200
  email: agresso@gmail.com
201
201
  executables: []
@@ -232,6 +232,7 @@ files:
232
232
  - lib/datagrid/helper.rb
233
233
  - lib/datagrid/rspec.rb
234
234
  - spec/datagrid/columns_spec.rb
235
+ - spec/datagrid/filters_spec.rb
235
236
  - spec/datagrid/form_builder_spec.rb
236
237
  - spec/datagrid/helper_spec.rb
237
238
  - spec/datagrid_spec.rb