datagrid 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.markdown CHANGED
@@ -141,6 +141,26 @@ Each column is sortable.
141
141
 
142
142
  ### Front end
143
143
 
144
+ ### Using Generator
145
+
146
+ Datagrid has a builtin generator:
147
+
148
+ ```
149
+ rails g datagrid:scaffold skills
150
+ ```
151
+
152
+ That gives you some code to play with out of the box:
153
+
154
+ ```
155
+ create app/grids/skills_grid.rb
156
+ create app/controllers/skills_controller.rb
157
+ create app/views/skills/index.html.erb
158
+ route resources :skills
159
+ ```
160
+
161
+ ### Manually
162
+
163
+ If you want to be a true ninja you can build everything from scratch on your own.
144
164
 
145
165
  In order to create form for your report you can use all set of rails built-in tools.
146
166
  More over Datagrid provides you two additional form helpers:
@@ -149,8 +169,7 @@ More over Datagrid provides you two additional form helpers:
149
169
  * datagrid\_filter
150
170
 
151
171
 
152
- The easiest way to create a report form:
153
- (haml for readablity)
172
+ The easiest way to create a report form (haml for readablity):
154
173
 
155
174
  ``` haml
156
175
  # Method `GET` is recommended for all report forms by default.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.0
1
+ 0.8.1
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.0"
8
+ s.version = "0.8.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 = "2013-04-07"
12
+ s.date = "2013-04-28"
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 = [
@@ -36,6 +36,7 @@ Gem::Specification.new do |s|
36
36
  "lib/datagrid/drivers.rb",
37
37
  "lib/datagrid/drivers/abstract_driver.rb",
38
38
  "lib/datagrid/drivers/active_record.rb",
39
+ "lib/datagrid/drivers/array.rb",
39
40
  "lib/datagrid/drivers/mongo_mapper.rb",
40
41
  "lib/datagrid/drivers/mongoid.rb",
41
42
  "lib/datagrid/engine.rb",
@@ -63,6 +64,7 @@ Gem::Specification.new do |s|
63
64
  "spec/datagrid/columns_spec.rb",
64
65
  "spec/datagrid/core_spec.rb",
65
66
  "spec/datagrid/drivers/active_record_spec.rb",
67
+ "spec/datagrid/drivers/array_spec.rb",
66
68
  "spec/datagrid/drivers/mongo_mapper_spec.rb",
67
69
  "spec/datagrid/drivers/mongoid_spec.rb",
68
70
  "spec/datagrid/filters/boolean_enum_filter_spec.rb",
@@ -75,6 +77,7 @@ Gem::Specification.new do |s|
75
77
  "spec/datagrid/form_builder_spec.rb",
76
78
  "spec/datagrid/helper_spec.rb",
77
79
  "spec/datagrid/ordering_spec.rb",
80
+ "spec/datagrid/scaffold_spec.rb",
78
81
  "spec/datagrid/utils_spec.rb",
79
82
  "spec/datagrid_spec.rb",
80
83
  "spec/spec_helper.rb",
@@ -84,8 +87,6 @@ Gem::Specification.new do |s|
84
87
  "spec/support/mongoid.rb",
85
88
  "spec/support/simple_report.rb",
86
89
  "spec/support/test_partials/_actions.html.erb",
87
- "template/controller.rb.erb",
88
- "template/grid.rb.erb",
89
90
  "templates/controller.rb.erb",
90
91
  "templates/grid.rb.erb",
91
92
  "templates/index.html.erb"
data/lib/datagrid/core.rb CHANGED
@@ -102,6 +102,7 @@ module Datagrid
102
102
  def scope(&block)
103
103
  if block_given?
104
104
  self.scope_value = block
105
+ self
105
106
  else
106
107
  check_scope_defined!
107
108
  scope_value.call
@@ -1,5 +1,6 @@
1
1
  require "datagrid/drivers/abstract_driver"
2
2
  require "datagrid/drivers/active_record"
3
+ require "datagrid/drivers/array"
3
4
  require "datagrid/drivers/mongoid"
4
5
  require "datagrid/drivers/mongo_mapper"
5
6
 
@@ -11,21 +11,24 @@ module Datagrid
11
11
  end
12
12
 
13
13
  def self.guess_driver(scope)
14
- klass = self.subclasses.find do |driver_class|
14
+ self.subclasses.find do |driver_class|
15
15
  driver_class.match?(scope)
16
16
  end || raise(Datagrid::ConfigurationError, "ORM Driver not found for scope: #{scope.inspect}.")
17
17
  end
18
18
 
19
-
20
19
  def self.match?(scope)
21
20
  raise NotImplementedError
22
21
  end
23
22
 
23
+ def match?(scope)
24
+ self.class.match?(scope)
25
+ end
26
+
24
27
  def to_scope(scope)
25
28
  raise NotImplementedError
26
29
  end
27
30
 
28
- def where(scope, condition)
31
+ def where(scope, attribute, value)
29
32
  raise NotImplementedError
30
33
  end
31
34
 
@@ -12,21 +12,22 @@ module Datagrid
12
12
  end
13
13
 
14
14
  def to_scope(scope)
15
+ return scope if scope.is_a?(::ActiveRecord::Relation)
15
16
  # Model class or Active record association
16
17
  # ActiveRecord association class hides itself under an Array
17
18
  # We can only reveal it by checking if it respond to some specific
18
19
  # to ActiveRecord method like #scoped
19
20
  if scope.is_a?(Class)
20
21
  scope.scoped({})
21
- elsif (scope.is_a?(Array) && scope.respond_to?(:scoped))
22
+ elsif scope.respond_to?(:scoped)
22
23
  scope.scoped
23
24
  else
24
25
  scope
25
26
  end
26
27
  end
27
28
 
28
- def where(scope, condition)
29
- scope.where(condition)
29
+ def where(scope, attribute, value)
30
+ scope.where(attribute => value)
30
31
  end
31
32
 
32
33
  def asc(scope, order)
@@ -0,0 +1,52 @@
1
+ module Datagrid
2
+ module Drivers
3
+ class Array < AbstractDriver
4
+
5
+ def self.match?(scope)
6
+ !Datagrid::Drivers::ActiveRecord.match?(scope) && scope.is_a?(::Array)
7
+ end
8
+
9
+ def to_scope(scope)
10
+ scope
11
+ end
12
+
13
+ def where(scope, attribute, value)
14
+ scope.select do |object|
15
+ object.send(attribute) == value
16
+ end
17
+ end
18
+
19
+ def asc(scope, order)
20
+ return scope unless order
21
+ return scope if order.empty?
22
+ scope.sort_by do |object|
23
+ object.send(order)
24
+ end
25
+ end
26
+
27
+ def desc(scope, order)
28
+ asc(scope, order).reverse
29
+ end
30
+
31
+ def default_order(scope, column_name)
32
+ column_name
33
+ end
34
+
35
+ def greater_equal(scope, field, value)
36
+ scope.select do |object|
37
+ object.send(field) >= value
38
+ end
39
+ end
40
+
41
+ def less_equal(scope, field, value)
42
+ scope.select do |object|
43
+ object.send(field) <= value
44
+ end
45
+ end
46
+
47
+ def has_column?(scope, column_name)
48
+ scope.first.respond_to?(column_name)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -15,8 +15,8 @@ module Datagrid
15
15
  scope.where
16
16
  end
17
17
 
18
- def where(scope, condition)
19
- scope.where(condition)
18
+ def where(scope, attribute, value)
19
+ scope.where(attribute => value)
20
20
  end
21
21
 
22
22
  def asc(scope, order)
@@ -15,8 +15,8 @@ module Datagrid
15
15
  scope.where(nil)
16
16
  end
17
17
 
18
- def where(scope, condition)
19
- scope.where(condition)
18
+ def where(scope, attribute, value)
19
+ scope.where(attribute => value)
20
20
  end
21
21
 
22
22
  def asc(scope, order)
@@ -84,8 +84,7 @@ module Datagrid
84
84
  def assets
85
85
  result = super
86
86
  self.class.filters.each do |filter|
87
- result_filter = filter.apply(self, result, filter_value(filter))
88
- result = result_filter if result_filter
87
+ result = filter.apply(self, result, filter_value(filter))
89
88
  end
90
89
  result
91
90
  end
@@ -1,3 +1,5 @@
1
+ class Datagrid::FilteringError < StandardError
2
+ end
1
3
 
2
4
  class Datagrid::Filters::BaseFilter
3
5
 
@@ -7,7 +9,7 @@ class Datagrid::Filters::BaseFilter
7
9
  self.grid = grid_class
8
10
  self.name = name
9
11
  self.options = options
10
- self.block = block || default_filter_block
12
+ self.block = block || default_filter_block
11
13
  end
12
14
 
13
15
  def format(value)
@@ -21,18 +23,17 @@ class Datagrid::Filters::BaseFilter
21
23
  return scope if value.blank? && !allow_blank?
22
24
  end
23
25
 
24
- if block.arity >= 3 || block.arity < 0
25
- scope.instance_exec(value, scope, grid_object, &block)
26
- elsif block.arity == 2
27
- scope.instance_exec(value, scope, &block)
28
- else
29
- scope.instance_exec(value, &block)
26
+ result = execute(value, scope, grid_object, &block)
27
+ return scope unless result
28
+ unless grid_object.driver.match?(result)
29
+ raise Datagrid::FilteringError, "Can not apply #{name.inspect} filter: result #{result.inspect} no longer match #{grid_object.driver.class}."
30
30
  end
31
+ result
31
32
  end
32
33
 
33
34
  def format_values(value)
34
- if !self.multiple && value.is_a?(Array)
35
- raise Datagrid::ArgumentError, "#{grid}##{name} filter can not accept Array argument. Use :multiple option."
35
+ if !self.multiple && value.is_a?(Array)
36
+ raise Datagrid::ArgumentError, "#{grid}##{name} filter can not accept Array argument. Use :multiple option."
36
37
  end
37
38
  values = Array.wrap(value)
38
39
  values.map! do |v|
@@ -42,7 +43,7 @@ class Datagrid::Filters::BaseFilter
42
43
  end
43
44
 
44
45
  def header
45
- options[:header] ||
46
+ options[:header] ||
46
47
  I18n.translate(self.name, :scope => "datagrid.#{grid.param_name}.filters", :default => self.name.to_s.humanize)
47
48
  end
48
49
 
@@ -73,9 +74,9 @@ class Datagrid::Filters::BaseFilter
73
74
 
74
75
  def default_filter_block
75
76
  filter = self
76
- lambda do |value, scope, grid|
77
+ lambda do |value, scope, grid|
77
78
  filter.default_filter(value, scope, grid)
78
- end
79
+ end
79
80
  end
80
81
 
81
82
  def default_filter(value, scope, grid)
@@ -87,8 +88,20 @@ class Datagrid::Filters::BaseFilter
87
88
  end
88
89
  end
89
90
 
91
+ protected
92
+
90
93
  def default_filter_where(driver, scope, value)
91
- driver.where(scope, name => value)
94
+ driver.where(scope, name, value)
95
+ end
96
+
97
+ def execute(value, scope, grid_object, &block)
98
+ if block.arity >= 3 || block.arity < 0
99
+ scope.instance_exec(value, scope, grid_object, &block)
100
+ elsif block.arity == 2
101
+ scope.instance_exec(value, scope, &block)
102
+ else
103
+ scope.instance_exec(value, &block)
104
+ end
92
105
  end
93
106
 
94
107
  end
@@ -33,7 +33,7 @@ class Datagrid::Scaffold < Rails::Generators::NamedBase
33
33
  def paginate_code
34
34
  if defined?(Kaminari)
35
35
  "page(params[:page])"
36
- elsif defned?(WillPaginate)
36
+ elsif defined?(WillPaginate)
37
37
  "paginate(:page => params[:page])"
38
38
  else
39
39
  "paginate_somehow"
@@ -43,7 +43,7 @@ class Datagrid::Scaffold < Rails::Generators::NamedBase
43
43
  def pagination_helper_code
44
44
  if defined?(Kaminari)
45
45
  "paginate(@assets)"
46
- elsif defned?(WillPaginate)
46
+ elsif defined?(WillPaginate)
47
47
  "will_paginate(@assets)"
48
48
  else
49
49
  "some_pagination_helper(@assets)"
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe Datagrid::Drivers::Array do
4
+
5
+ describe ".match?" do
6
+ subject { described_class }
7
+
8
+ it {should be_match(Array.new)}
9
+ it {should_not be_match({})}
10
+ end
11
+
12
+ describe "api" do
13
+
14
+ class ArrayGrid
15
+ class User < Struct.new(:name, :age); end
16
+ include Datagrid
17
+ scope do
18
+ []
19
+ end
20
+
21
+ filter(:name)
22
+ filter(:age, :integer, :range => true)
23
+
24
+ column(:name)
25
+ column(:age)
26
+ end
27
+
28
+ let(:first) { ArrayGrid::User.new("Vasya", 15) }
29
+ let(:second) { ArrayGrid::User.new("Petya", 12) }
30
+ let(:third) { ArrayGrid::User.new("Vova", 13) }
31
+
32
+ subject do
33
+ ArrayGrid.new(
34
+ defined?(_attributes) ? _attributes : {}
35
+ ).scope do
36
+ [ first, second, third ]
37
+ end
38
+ end
39
+
40
+
41
+ its(:"assets.size") {should == 3}
42
+ its(:rows) {should == [["Vasya", 15], ["Petya", 12], ["Vova", 13]]}
43
+ its(:header) {should ==[ "Name", "Age"]}
44
+
45
+ its(:data) {should == [[ "Name", "Age"], ["Vasya", 15], ["Petya", 12], ["Vova", 13]]}
46
+
47
+
48
+ describe "when some filters specified" do
49
+ let(:_attributes) { {:age => [12,14]} }
50
+ its(:assets) {should_not include(first)}
51
+ its(:assets) {should include(second)}
52
+ its(:assets) {should include(third)}
53
+ end
54
+
55
+ describe "when reverse ordering is specified" do
56
+ let(:_attributes) { {:order => :name, :descending => true} }
57
+ its(:assets) {should == [third, first, second]}
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Datagrid::Scaffold do
4
+ subject { Datagrid::Scaffold.new([""]) }
5
+
6
+ describe '.paginate_code' do
7
+ it 'should fall through options successfully' do
8
+ subject.paginate_code.should eql('paginate_somehow')
9
+ end
10
+ end
11
+
12
+ describe '.pagination_helper_code' do
13
+ it 'should fall through options successfully' do
14
+ subject.pagination_helper_code.should eql('some_pagination_helper(@assets)')
15
+ end
16
+ end
17
+ end
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.8.0
4
+ version: 0.8.1
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-04-07 00:00:00.000000000 Z
12
+ date: 2013-04-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -263,6 +263,7 @@ files:
263
263
  - lib/datagrid/drivers.rb
264
264
  - lib/datagrid/drivers/abstract_driver.rb
265
265
  - lib/datagrid/drivers/active_record.rb
266
+ - lib/datagrid/drivers/array.rb
266
267
  - lib/datagrid/drivers/mongo_mapper.rb
267
268
  - lib/datagrid/drivers/mongoid.rb
268
269
  - lib/datagrid/engine.rb
@@ -290,6 +291,7 @@ files:
290
291
  - spec/datagrid/columns_spec.rb
291
292
  - spec/datagrid/core_spec.rb
292
293
  - spec/datagrid/drivers/active_record_spec.rb
294
+ - spec/datagrid/drivers/array_spec.rb
293
295
  - spec/datagrid/drivers/mongo_mapper_spec.rb
294
296
  - spec/datagrid/drivers/mongoid_spec.rb
295
297
  - spec/datagrid/filters/boolean_enum_filter_spec.rb
@@ -302,6 +304,7 @@ files:
302
304
  - spec/datagrid/form_builder_spec.rb
303
305
  - spec/datagrid/helper_spec.rb
304
306
  - spec/datagrid/ordering_spec.rb
307
+ - spec/datagrid/scaffold_spec.rb
305
308
  - spec/datagrid/utils_spec.rb
306
309
  - spec/datagrid_spec.rb
307
310
  - spec/spec_helper.rb
@@ -311,8 +314,6 @@ files:
311
314
  - spec/support/mongoid.rb
312
315
  - spec/support/simple_report.rb
313
316
  - spec/support/test_partials/_actions.html.erb
314
- - template/controller.rb.erb
315
- - template/grid.rb.erb
316
317
  - templates/controller.rb.erb
317
318
  - templates/grid.rb.erb
318
319
  - templates/index.html.erb
@@ -331,7 +332,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
331
332
  version: '0'
332
333
  segments:
333
334
  - 0
334
- hash: 1083654781262067166
335
+ hash: 3628850104894180281
335
336
  required_rubygems_version: !ruby/object:Gem::Requirement
336
337
  none: false
337
338
  requirements:
@@ -1,9 +0,0 @@
1
- class <%= class_name %>Controller < ApplicationController
2
-
3
- def index
4
- @grid = <%= class_name %>Grid.new(params[:<%= (class_name + "Grid").underscore %>])
5
- @assets = @grid.assets.page(params[:page])
6
- end
7
-
8
- end
9
-
data/template/grid.rb.erb DELETED
@@ -1,10 +0,0 @@
1
- class <%= class_name %>Grid
2
-
3
- include Datagrid
4
-
5
- scope do
6
- <%= class_name.singularize %>
7
- end
8
-
9
- column(:id)
10
- end