datagrid 0.7.1 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -1
- data/VERSION +1 -1
- data/app/views/datagrid/_order_for.html.erb +10 -0
- data/datagrid.gemspec +13 -3
- data/lib/datagrid.rb +1 -8
- data/lib/datagrid/columns.rb +16 -2
- data/lib/datagrid/filters.rb +2 -1
- data/lib/datagrid/renderer.rb +1 -11
- data/lib/datagrid/scaffold.rb +58 -0
- data/lib/tasks/datagrid_tasks.rake +1 -0
- data/spec/datagrid/columns_spec.rb +4 -0
- data/spec/datagrid/filters_spec.rb +15 -0
- data/spec/datagrid/helper_spec.rb +2 -1
- data/spec/support/matchers.rb +9 -2
- data/template/controller.rb.erb +9 -0
- data/template/grid.rb.erb +10 -0
- data/templates/controller.rb.erb +9 -0
- data/templates/grid.rb.erb +17 -0
- data/templates/index.html.erb +13 -0
- metadata +26 -3
data/Gemfile
CHANGED
@@ -3,14 +3,15 @@ source "http://rubygems.org"
|
|
3
3
|
gem "rails", ">= 3.0"
|
4
4
|
|
5
5
|
|
6
|
-
|
7
6
|
group :development do
|
8
7
|
|
9
8
|
gem "rake"
|
10
9
|
gem "bundler"
|
11
10
|
gem "jeweler"
|
12
11
|
|
12
|
+
|
13
13
|
gem "debugger", :platform => "ruby_19"
|
14
|
+
gem "ruby-debug", :platform => "ruby_18"
|
14
15
|
|
15
16
|
gem "rspec"
|
16
17
|
gem "nokogiri" # used to test html output
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.8.0
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<div class="order">
|
2
|
+
<%= link_to(
|
3
|
+
I18n.t("datagrid.table.order.asc", :default => "↑".html_safe).html_safe,
|
4
|
+
url_for(grid.param_name => grid.attributes.merge(:order => column.name, :descending => false)),
|
5
|
+
:class => "order asc") %>
|
6
|
+
<%= link_to(
|
7
|
+
I18n.t("datagrid.table.order.desc", :default => "↓".html_safe).html_safe,
|
8
|
+
url_for(grid.param_name => grid.attributes.merge(:order => column.name, :descending => true )),
|
9
|
+
:class => "order desc") %>
|
10
|
+
</div>
|
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.
|
8
|
+
s.version = "0.8.0"
|
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-
|
12
|
+
s.date = "2013-04-07"
|
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 = [
|
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
|
|
24
24
|
"Readme.markdown",
|
25
25
|
"VERSION",
|
26
26
|
"app/views/datagrid/_head.html.erb",
|
27
|
+
"app/views/datagrid/_order_for.html.erb",
|
27
28
|
"app/views/datagrid/_row.html.erb",
|
28
29
|
"app/views/datagrid/_table.html.erb",
|
29
30
|
"datagrid.gemspec",
|
@@ -55,6 +56,7 @@ Gem::Specification.new do |s|
|
|
55
56
|
"lib/datagrid/ordering.rb",
|
56
57
|
"lib/datagrid/renderer.rb",
|
57
58
|
"lib/datagrid/rspec.rb",
|
59
|
+
"lib/datagrid/scaffold.rb",
|
58
60
|
"lib/datagrid/utils.rb",
|
59
61
|
"lib/tasks/datagrid_tasks.rake",
|
60
62
|
"spec/datagrid/active_model_spec.rb",
|
@@ -81,7 +83,12 @@ Gem::Specification.new do |s|
|
|
81
83
|
"spec/support/mongo_mapper.rb",
|
82
84
|
"spec/support/mongoid.rb",
|
83
85
|
"spec/support/simple_report.rb",
|
84
|
-
"spec/support/test_partials/_actions.html.erb"
|
86
|
+
"spec/support/test_partials/_actions.html.erb",
|
87
|
+
"template/controller.rb.erb",
|
88
|
+
"template/grid.rb.erb",
|
89
|
+
"templates/controller.rb.erb",
|
90
|
+
"templates/grid.rb.erb",
|
91
|
+
"templates/index.html.erb"
|
85
92
|
]
|
86
93
|
s.homepage = "http://github.com/bogdan/datagrid"
|
87
94
|
s.licenses = ["MIT"]
|
@@ -98,6 +105,7 @@ Gem::Specification.new do |s|
|
|
98
105
|
s.add_development_dependency(%q<bundler>, [">= 0"])
|
99
106
|
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
100
107
|
s.add_development_dependency(%q<debugger>, [">= 0"])
|
108
|
+
s.add_development_dependency(%q<ruby-debug>, [">= 0"])
|
101
109
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
102
110
|
s.add_development_dependency(%q<nokogiri>, [">= 0"])
|
103
111
|
s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
|
@@ -112,6 +120,7 @@ Gem::Specification.new do |s|
|
|
112
120
|
s.add_dependency(%q<bundler>, [">= 0"])
|
113
121
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
114
122
|
s.add_dependency(%q<debugger>, [">= 0"])
|
123
|
+
s.add_dependency(%q<ruby-debug>, [">= 0"])
|
115
124
|
s.add_dependency(%q<rspec>, [">= 0"])
|
116
125
|
s.add_dependency(%q<nokogiri>, [">= 0"])
|
117
126
|
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
@@ -127,6 +136,7 @@ Gem::Specification.new do |s|
|
|
127
136
|
s.add_dependency(%q<bundler>, [">= 0"])
|
128
137
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
129
138
|
s.add_dependency(%q<debugger>, [">= 0"])
|
139
|
+
s.add_dependency(%q<ruby-debug>, [">= 0"])
|
130
140
|
s.add_dependency(%q<rspec>, [">= 0"])
|
131
141
|
s.add_dependency(%q<nokogiri>, [">= 0"])
|
132
142
|
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
data/lib/datagrid.rb
CHANGED
@@ -31,23 +31,16 @@ module Datagrid
|
|
31
31
|
include ::Datagrid::Ordering
|
32
32
|
|
33
33
|
end
|
34
|
-
base.send :include, InstanceMethods
|
35
34
|
end # self.included
|
36
35
|
|
37
36
|
module ClassMethods
|
38
|
-
|
39
|
-
|
40
37
|
end # ClassMethods
|
41
38
|
|
42
|
-
module InstanceMethods
|
43
|
-
|
44
|
-
end # InstanceMethods
|
45
|
-
|
46
39
|
class ConfigurationError < StandardError; end
|
47
40
|
class ArgumentError < ::ArgumentError; end
|
48
41
|
|
49
|
-
|
50
42
|
end
|
51
43
|
|
44
|
+
require "datagrid/scaffold"
|
52
45
|
|
53
46
|
|
data/lib/datagrid/columns.rb
CHANGED
@@ -21,11 +21,13 @@ module Datagrid
|
|
21
21
|
module ClassMethods
|
22
22
|
|
23
23
|
# Returns a list of columns defined.
|
24
|
+
# All column definistion are returned by default
|
24
25
|
# You can limit the output with only columns you need like:
|
25
26
|
#
|
26
27
|
# grid.columns(:id, :name)
|
27
28
|
#
|
28
29
|
# Supported options:
|
30
|
+
#
|
29
31
|
# * :data - if true returns only non-html columns. Default: false.
|
30
32
|
def columns(*args)
|
31
33
|
options = args.extract_options!
|
@@ -94,15 +96,27 @@ module Datagrid
|
|
94
96
|
self.rows.unshift(self.header)
|
95
97
|
end
|
96
98
|
|
97
|
-
# Return Array of Hashes where keys are column names and values are column values
|
99
|
+
# Return Array of Hashes where keys are column names and values are column values
|
100
|
+
# for each row in datagrid <tt>#assets</tt>
|
98
101
|
def data_hash
|
99
102
|
self.assets.map do |asset|
|
100
103
|
hash_for(asset)
|
101
104
|
end
|
102
105
|
end
|
103
106
|
|
107
|
+
# Returns a CSV representation of the data in the table
|
108
|
+
# You are able to specify which columns you want to see in CSV.
|
109
|
+
# All data columns are included by default
|
110
|
+
# Also you can specify options hash as last argument that is proxied to
|
111
|
+
# Ruby CSV library.
|
112
|
+
#
|
113
|
+
# Example:
|
114
|
+
#
|
115
|
+
# grid.to_csv
|
116
|
+
# grid.to_csv(:id, :name)
|
117
|
+
# grid.to_csv(:col_sep => ';')
|
104
118
|
def to_csv(*column_names)
|
105
|
-
options =
|
119
|
+
options = column_names.extract_options!
|
106
120
|
klass = if RUBY_VERSION >= "1.9"
|
107
121
|
require 'csv'
|
108
122
|
CSV
|
data/lib/datagrid/filters.rb
CHANGED
@@ -84,7 +84,8 @@ module Datagrid
|
|
84
84
|
def assets
|
85
85
|
result = super
|
86
86
|
self.class.filters.each do |filter|
|
87
|
-
|
87
|
+
result_filter = filter.apply(self, result, filter_value(filter))
|
88
|
+
result = result_filter if result_filter
|
88
89
|
end
|
89
90
|
result
|
90
91
|
end
|
data/lib/datagrid/renderer.rb
CHANGED
@@ -72,17 +72,7 @@ module Datagrid
|
|
72
72
|
end
|
73
73
|
|
74
74
|
def order_for(grid, column)
|
75
|
-
@template.
|
76
|
-
@template.link_to(
|
77
|
-
I18n.t("datagrid.table.order.asc", :default => "↑".html_safe),
|
78
|
-
@template.url_for(grid.param_name => grid.attributes.merge(:order => column.name, :descending => false)),
|
79
|
-
:class => "order asc"
|
80
|
-
) + " " + @template.link_to(
|
81
|
-
I18n.t("datagrid.table.order.desc", :default => "↓".html_safe),
|
82
|
-
@template.url_for(grid.param_name => grid.attributes.merge(:order => column.name, :descending => true )),
|
83
|
-
:class => "order desc"
|
84
|
-
)
|
85
|
-
end
|
75
|
+
@template.render :partial => "datagrid/order_for", :locals => { :grid => grid, :column => column }
|
86
76
|
end
|
87
77
|
|
88
78
|
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
|
3
|
+
class Datagrid::Scaffold < Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path(__FILE__ + "/../../../templates")
|
5
|
+
|
6
|
+
def create_scaffold
|
7
|
+
template "grid.rb.erb", "app/grids/#{grid_class_name.underscore}.rb"
|
8
|
+
template "controller.rb.erb", "app/controllers/#{grid_controller_name.underscore}.rb"
|
9
|
+
template "index.html.erb", "app/views/#{grid_controller_short_name}/index.html.erb"
|
10
|
+
route("resources :#{grid_controller_short_name}")
|
11
|
+
end
|
12
|
+
|
13
|
+
def grid_class_name
|
14
|
+
file_name.camelize.pluralize + "Grid"
|
15
|
+
end
|
16
|
+
|
17
|
+
def grid_controller_name
|
18
|
+
grid_controller_short_name.camelize + "Controller"
|
19
|
+
end
|
20
|
+
|
21
|
+
def grid_controller_short_name
|
22
|
+
file_name.underscore.pluralize
|
23
|
+
end
|
24
|
+
|
25
|
+
def grid_model_name
|
26
|
+
file_name.camelize.singularize
|
27
|
+
end
|
28
|
+
|
29
|
+
def grid_ivar_name
|
30
|
+
grid_class_name.underscore
|
31
|
+
end
|
32
|
+
|
33
|
+
def paginate_code
|
34
|
+
if defined?(Kaminari)
|
35
|
+
"page(params[:page])"
|
36
|
+
elsif defned?(WillPaginate)
|
37
|
+
"paginate(:page => params[:page])"
|
38
|
+
else
|
39
|
+
"paginate_somehow"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def pagination_helper_code
|
44
|
+
if defined?(Kaminari)
|
45
|
+
"paginate(@assets)"
|
46
|
+
elsif defned?(WillPaginate)
|
47
|
+
"will_paginate(@assets)"
|
48
|
+
else
|
49
|
+
"some_pagination_helper(@assets)"
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
def grid_route_name
|
55
|
+
grid_controller_short_name + "_path"
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -53,6 +53,10 @@ describe Datagrid::Columns do
|
|
53
53
|
it "should support csv export of particular columns" do
|
54
54
|
subject.to_csv(:name).should == "Name\nStar\n"
|
55
55
|
end
|
56
|
+
|
57
|
+
it "should support csv export options" do
|
58
|
+
subject.to_csv(:col_sep => ";").should == "Group;Name;Access level;Pet\nPop;Star;admin;ROTTWEILER\n"
|
59
|
+
end
|
56
60
|
end
|
57
61
|
|
58
62
|
it "should support columns with model and report arguments" do
|
@@ -114,4 +114,19 @@ describe Datagrid::Filters do
|
|
114
114
|
end
|
115
115
|
|
116
116
|
end
|
117
|
+
describe "default filter as scope" do
|
118
|
+
it "should create default filter if scope respond to filter name method" do
|
119
|
+
Entry.create!
|
120
|
+
grid = test_report(:custom => 'skip') do
|
121
|
+
scope {Entry}
|
122
|
+
filter(:custom) do |value|
|
123
|
+
if value != 'skip'
|
124
|
+
where(:custom => value)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
grid.assets.should_not be_empty
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
117
132
|
end
|
@@ -272,7 +272,8 @@ describe Datagrid::Helper do
|
|
272
272
|
grid = OrderedGrid.new(:descending => true, :order => :category)
|
273
273
|
subject.datagrid_order_for(grid, grid.column_by_name(:category)).should equal_to_dom(<<-HTML)
|
274
274
|
<div class="order">
|
275
|
-
<a href="ordered_grid%5Bdescending%5D=false&ordered_grid%5Border%5D=category" class="order asc">↑</a>
|
275
|
+
<a href="ordered_grid%5Bdescending%5D=false&ordered_grid%5Border%5D=category" class="order asc">↑</a>
|
276
|
+
<a href="ordered_grid%5Bdescending%5D=true&ordered_grid%5Border%5D=category" class="order desc">↓</a>
|
276
277
|
</div>
|
277
278
|
HTML
|
278
279
|
end
|
data/spec/support/matchers.rb
CHANGED
@@ -34,17 +34,24 @@ end
|
|
34
34
|
class EqualToDom
|
35
35
|
|
36
36
|
def initialize(expectation)
|
37
|
-
@expectation = Nokogiri::HTML::DocumentFragment.parse(expectation.strip).to_s
|
37
|
+
@expectation = Nokogiri::HTML::DocumentFragment.parse(force_encoding(expectation).strip).to_s
|
38
38
|
end
|
39
39
|
|
40
40
|
def matches?(text)
|
41
|
-
|
41
|
+
|
42
|
+
@matcher = Nokogiri::HTML::DocumentFragment.parse(force_encoding(text).strip).to_s
|
42
43
|
@matcher == @expectation
|
43
44
|
end
|
44
45
|
|
45
46
|
def failure_message
|
46
47
|
"Expected dom #{@matcher} to match #{@expectation}, but it wasn't"
|
47
48
|
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def force_encoding(text)
|
53
|
+
"1.9.3".respond_to?(:force_encoding) ? text.clone.force_encoding("UTF-8") : text
|
54
|
+
end
|
48
55
|
end
|
49
56
|
|
50
57
|
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class <%= grid_class_name %>
|
2
|
+
|
3
|
+
include Datagrid
|
4
|
+
|
5
|
+
scope do
|
6
|
+
<%= grid_model_name %>
|
7
|
+
end
|
8
|
+
|
9
|
+
filter(:id, :integer)
|
10
|
+
filter(:created_at, :date, :range => true)
|
11
|
+
|
12
|
+
column(:id)
|
13
|
+
column(:name)
|
14
|
+
column(:created_at) do |model|
|
15
|
+
model.created_at.to_date
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
<%%= form_for @grid, :method => :get, :url => <%= grid_route_name %> do |f| %>
|
2
|
+
<%% @grid.filters.each do |filter| %>
|
3
|
+
<div class="field">
|
4
|
+
<%%= f.datagrid_label filter %>
|
5
|
+
<%%= f.datagrid_filter filter %>
|
6
|
+
</div>
|
7
|
+
<%% end %>
|
8
|
+
<%%= f.submit %>
|
9
|
+
<%% end %>
|
10
|
+
|
11
|
+
<%%= <%=pagination_helper_code%> %>
|
12
|
+
<%%= datagrid_table @grid, @assets %>
|
13
|
+
<%%= <%=pagination_helper_code%> %>
|
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.
|
4
|
+
version: 0.8.0
|
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-
|
12
|
+
date: 2013-04-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -91,6 +91,22 @@ dependencies:
|
|
91
91
|
- - ! '>='
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: ruby-debug
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
94
110
|
- !ruby/object:Gem::Dependency
|
95
111
|
name: rspec
|
96
112
|
requirement: !ruby/object:Gem::Requirement
|
@@ -235,6 +251,7 @@ files:
|
|
235
251
|
- Readme.markdown
|
236
252
|
- VERSION
|
237
253
|
- app/views/datagrid/_head.html.erb
|
254
|
+
- app/views/datagrid/_order_for.html.erb
|
238
255
|
- app/views/datagrid/_row.html.erb
|
239
256
|
- app/views/datagrid/_table.html.erb
|
240
257
|
- datagrid.gemspec
|
@@ -266,6 +283,7 @@ files:
|
|
266
283
|
- lib/datagrid/ordering.rb
|
267
284
|
- lib/datagrid/renderer.rb
|
268
285
|
- lib/datagrid/rspec.rb
|
286
|
+
- lib/datagrid/scaffold.rb
|
269
287
|
- lib/datagrid/utils.rb
|
270
288
|
- lib/tasks/datagrid_tasks.rake
|
271
289
|
- spec/datagrid/active_model_spec.rb
|
@@ -293,6 +311,11 @@ files:
|
|
293
311
|
- spec/support/mongoid.rb
|
294
312
|
- spec/support/simple_report.rb
|
295
313
|
- spec/support/test_partials/_actions.html.erb
|
314
|
+
- template/controller.rb.erb
|
315
|
+
- template/grid.rb.erb
|
316
|
+
- templates/controller.rb.erb
|
317
|
+
- templates/grid.rb.erb
|
318
|
+
- templates/index.html.erb
|
296
319
|
homepage: http://github.com/bogdan/datagrid
|
297
320
|
licenses:
|
298
321
|
- MIT
|
@@ -308,7 +331,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
308
331
|
version: '0'
|
309
332
|
segments:
|
310
333
|
- 0
|
311
|
-
hash:
|
334
|
+
hash: 1083654781262067166
|
312
335
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
313
336
|
none: false
|
314
337
|
requirements:
|