datagrid 1.8.1 → 2.0.0.pre.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -7
  3. data/{Readme.markdown → README.md} +46 -29
  4. data/app/assets/stylesheets/datagrid.css +145 -0
  5. data/app/views/datagrid/_enum_checkboxes.html.erb +5 -3
  6. data/app/views/datagrid/_form.html.erb +4 -5
  7. data/app/views/datagrid/_head.html.erb +26 -3
  8. data/app/views/datagrid/_range_filter.html.erb +5 -3
  9. data/app/views/datagrid/_row.html.erb +12 -1
  10. data/app/views/datagrid/_table.html.erb +4 -4
  11. data/datagrid.gemspec +8 -8
  12. data/lib/datagrid/active_model.rb +9 -17
  13. data/lib/datagrid/base.rb +39 -0
  14. data/lib/datagrid/column_names_attribute.rb +12 -12
  15. data/lib/datagrid/columns/column.rb +155 -133
  16. data/lib/datagrid/columns.rb +495 -282
  17. data/lib/datagrid/configuration.rb +23 -10
  18. data/lib/datagrid/core.rb +184 -150
  19. data/lib/datagrid/deprecated_object.rb +20 -0
  20. data/lib/datagrid/drivers/abstract_driver.rb +13 -25
  21. data/lib/datagrid/drivers/active_record.rb +24 -26
  22. data/lib/datagrid/drivers/array.rb +26 -17
  23. data/lib/datagrid/drivers/mongo_mapper.rb +15 -14
  24. data/lib/datagrid/drivers/mongoid.rb +16 -18
  25. data/lib/datagrid/drivers/sequel.rb +14 -19
  26. data/lib/datagrid/drivers.rb +2 -1
  27. data/lib/datagrid/engine.rb +11 -3
  28. data/lib/datagrid/filters/base_filter.rb +166 -142
  29. data/lib/datagrid/filters/boolean_filter.rb +19 -5
  30. data/lib/datagrid/filters/date_filter.rb +33 -35
  31. data/lib/datagrid/filters/date_time_filter.rb +24 -16
  32. data/lib/datagrid/filters/default_filter.rb +9 -3
  33. data/lib/datagrid/filters/dynamic_filter.rb +151 -105
  34. data/lib/datagrid/filters/enum_filter.rb +43 -19
  35. data/lib/datagrid/filters/extended_boolean_filter.rb +39 -27
  36. data/lib/datagrid/filters/float_filter.rb +16 -5
  37. data/lib/datagrid/filters/integer_filter.rb +21 -10
  38. data/lib/datagrid/filters/ranged_filter.rb +66 -45
  39. data/lib/datagrid/filters/select_options.rb +58 -49
  40. data/lib/datagrid/filters/string_filter.rb +9 -4
  41. data/lib/datagrid/filters.rb +234 -106
  42. data/lib/datagrid/form_builder.rb +116 -128
  43. data/lib/datagrid/generators/scaffold.rb +185 -0
  44. data/lib/datagrid/generators/views.rb +20 -0
  45. data/lib/datagrid/helper.rb +397 -22
  46. data/lib/datagrid/ordering.rb +81 -87
  47. data/lib/datagrid/rspec.rb +8 -12
  48. data/lib/datagrid/utils.rb +42 -38
  49. data/lib/datagrid/version.rb +3 -1
  50. data/lib/datagrid.rb +18 -28
  51. data/templates/base.rb.erb +33 -7
  52. data/templates/grid.rb.erb +1 -1
  53. metadata +18 -19
  54. data/app/assets/stylesheets/datagrid.sass +0 -134
  55. data/lib/datagrid/filters/composite_filters.rb +0 -49
  56. data/lib/datagrid/renderer.rb +0 -157
  57. data/lib/datagrid/scaffold.rb +0 -129
  58. data/lib/tasks/datagrid_tasks.rake +0 -15
  59. data/templates/controller.rb.erb +0 -6
  60. data/templates/index.html.erb +0 -5
@@ -1,10 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Datagrid
2
4
  module Drivers
3
5
  # @!visibility private
4
6
  class Array < AbstractDriver
5
-
6
7
  def self.match?(scope)
7
- !Datagrid::Drivers::ActiveRecord.match?(scope) && (scope.is_a?(::Array) || scope.is_a?(Enumerator))
8
+ !Datagrid::Drivers::ActiveRecord.match?(scope) && (
9
+ scope.is_a?(::Array) || scope.is_a?(Enumerator) ||
10
+ (defined?(::ActiveRecord::Result) && scope.is_a?(::ActiveRecord::Result))
11
+ )
8
12
  end
9
13
 
10
14
  def to_scope(scope)
@@ -13,15 +17,16 @@ module Datagrid
13
17
 
14
18
  def where(scope, attribute, value)
15
19
  scope.select do |object|
16
- object.send(attribute) == value
20
+ get(object, attribute) == value
17
21
  end
18
22
  end
19
23
 
20
24
  def asc(scope, order)
21
25
  return scope unless order
22
26
  return scope if order.empty?
27
+
23
28
  scope.sort_by do |object|
24
- object.send(order)
29
+ get(object, order)
25
30
  end
26
31
  end
27
32
 
@@ -29,7 +34,7 @@ module Datagrid
29
34
  asc(scope, order).reverse
30
35
  end
31
36
 
32
- def default_order(scope, column_name)
37
+ def default_order(_scope, column_name)
33
38
  column_name
34
39
  end
35
40
 
@@ -39,38 +44,36 @@ module Datagrid
39
44
 
40
45
  def greater_equal(scope, field, value)
41
46
  scope.select do |object|
42
- compare_value = object.send(field)
43
- compare_value.respond_to?(:>=) && compare_value >= value
47
+ get(object, field) >= value
44
48
  end
45
49
  end
46
50
 
47
51
  def less_equal(scope, field, value)
48
52
  scope.select do |object|
49
- compare_value = object.send(field)
50
- compare_value.respond_to?(:<=) && compare_value <= value
53
+ get(object, field) <= value
51
54
  end
52
55
  end
53
56
 
54
- def has_column?(scope, column_name)
57
+ def scope_has_column?(scope, column_name)
55
58
  scope.any? && scope.first.respond_to?(column_name)
56
59
  end
57
60
 
58
- def is_timestamp?(scope, column_name)
59
- has_column?(scope, column_name) &&
60
- timestamp_class?(scope.first.send(column_name).class)
61
+ def timestamp_column?(scope, column_name)
62
+ scope_has_column?(scope, column_name) &&
63
+ timestamp_class?(get(scope.first, column_name).class)
61
64
  end
62
65
 
63
66
  def contains(scope, field, value)
64
67
  scope.select do |object|
65
- object.send(field).to_s.include?(value)
68
+ get(object, field).to_s.include?(value)
66
69
  end
67
70
  end
68
71
 
69
- def column_names(scope)
72
+ def column_names(_scope)
70
73
  []
71
74
  end
72
75
 
73
- def batch_each(scope, batch_size, &block)
76
+ def batch_each(scope, _batch_size, &block)
74
77
  scope.each(&block)
75
78
  end
76
79
 
@@ -78,9 +81,15 @@ module Datagrid
78
81
  asset
79
82
  end
80
83
 
81
- def can_preload?(scope, association)
84
+ def can_preload?(_scope, _association)
82
85
  false
83
86
  end
87
+
88
+ protected
89
+
90
+ def get(object, property)
91
+ object.is_a?(Hash) ? object[property] : object.public_send(property)
92
+ end
84
93
  end
85
94
  end
86
95
  end
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Datagrid
2
4
  module Drivers
3
5
  # @!visibility private
4
6
  class MongoMapper < AbstractDriver
5
-
6
7
  def self.match?(scope)
7
8
  return false unless defined?(::MongoMapper)
9
+
8
10
  if scope.is_a?(Class)
9
11
  scope.ancestors.include?(::MongoMapper::Document)
10
12
  else
@@ -29,31 +31,31 @@ module Datagrid
29
31
  end
30
32
 
31
33
  def default_order(scope, column_name)
32
- has_column?(scope, column_name) ? column_name : nil
34
+ scope_has_column?(scope, column_name) ? column_name : nil
33
35
  end
34
36
 
35
37
  def greater_equal(scope, field, value)
36
- scope.where(field => {"$gte" => value})
38
+ scope.where(field => { "$gte" => value })
37
39
  end
38
40
 
39
41
  def less_equal(scope, field, value)
40
- scope.where(field => {"$lte" => value})
42
+ scope.where(field => { "$lte" => value })
41
43
  end
42
44
 
43
- def has_column?(scope, column_name)
45
+ def scope_has_column?(scope, column_name)
44
46
  scope.key?(column_name)
45
47
  end
46
48
 
47
- def is_timestamp?(scope, column_name)
48
- #TODO implement the support
49
+ def timestamp_column?(_scope, _column_name)
50
+ # TODO: implement the support
49
51
  false
50
52
  end
51
53
 
52
- def contains(scope, field, value)
54
+ def contains(_scope, field, value)
53
55
  scope(field => Regexp.compile(Regexp.escape(value)))
54
56
  end
55
57
 
56
- def column_names(scope)
58
+ def column_names(_scope)
57
59
  [] # TODO: implement support
58
60
  end
59
61
 
@@ -62,10 +64,9 @@ module Datagrid
62
64
  loop do
63
65
  batch = scope.skip(current_page * batch_size).limit(batch_size).to_a
64
66
  return if batch.empty?
65
- scope.skip(current_page * batch_size).limit(batch_size).each do |item|
66
- yield(item)
67
- end
68
- current_page+=1
67
+
68
+ scope.skip(current_page * batch_size).limit(batch_size).each(&block)
69
+ current_page += 1
69
70
  end
70
71
  end
71
72
 
@@ -77,7 +78,7 @@ module Datagrid
77
78
  raise NotImplementedError
78
79
  end
79
80
 
80
- def can_preload?(scope, association)
81
+ def can_preload?(_scope, _association)
81
82
  false
82
83
  end
83
84
  end
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Datagrid
2
4
  module Drivers
3
5
  # @!visibility private
4
6
  class Mongoid < AbstractDriver
5
-
6
7
  def self.match?(scope)
7
8
  return false unless defined?(::Mongoid)
9
+
8
10
  if scope.is_a?(Class)
9
11
  scope.ancestors.include?(::Mongoid::Document)
10
12
  else
@@ -21,9 +23,7 @@ module Datagrid
21
23
  end
22
24
 
23
25
  def where(scope, attribute, value)
24
- if value.is_a?(Range)
25
- value = {"$gte" => value.first, "$lte" => value.last}
26
- end
26
+ value = { "$gte" => value.first, "$lte" => value.last } if value.is_a?(Range)
27
27
  scope.where(attribute => value)
28
28
  end
29
29
 
@@ -36,18 +36,18 @@ module Datagrid
36
36
  end
37
37
 
38
38
  def default_order(scope, column_name)
39
- has_column?(scope, column_name) ? column_name : nil
39
+ scope_has_column?(scope, column_name) ? column_name : nil
40
40
  end
41
41
 
42
42
  def greater_equal(scope, field, value)
43
- scope.where(field => {"$gte" => value})
43
+ scope.where(field => { "$gte" => value })
44
44
  end
45
45
 
46
46
  def less_equal(scope, field, value)
47
- scope.where(field => {"$lte" => value})
47
+ scope.where(field => { "$lte" => value })
48
48
  end
49
49
 
50
- def has_column?(scope, column_name)
50
+ def scope_has_column?(scope, column_name)
51
51
  column_names(scope).include?(column_name.to_s)
52
52
  end
53
53
 
@@ -60,10 +60,11 @@ module Datagrid
60
60
  end
61
61
 
62
62
  def normalized_column_type(scope, field)
63
- type = to_scope(scope).klass.fields[field.to_s].try(:type)
63
+ type = to_scope(scope).klass.fields[field.to_s]&.type
64
64
  return nil unless type
65
+
65
66
  {
66
- [BigDecimal , String, Symbol, Range, Array, Hash, ] => :string,
67
+ [BigDecimal, String, Symbol, Range, Array, Hash] => :string,
67
68
  [::Mongoid::Boolean] => :boolean,
68
69
 
69
70
  [Date] => :date,
@@ -76,7 +77,7 @@ module Datagrid
76
77
  }.each do |keys, value|
77
78
  return value if keys.include?(type)
78
79
  end
79
- return :string
80
+ :string
80
81
  end
81
82
 
82
83
  def batch_each(scope, batch_size, &block)
@@ -84,10 +85,9 @@ module Datagrid
84
85
  loop do
85
86
  batch = scope.skip(current_page * batch_size).limit(batch_size).to_a
86
87
  return if batch.empty?
87
- scope.skip(current_page * batch_size).limit(batch_size).each do |item|
88
- yield(item)
89
- end
90
- current_page+=1
88
+
89
+ scope.skip(current_page * batch_size).limit(batch_size).each(&block)
90
+ current_page += 1
91
91
  end
92
92
  end
93
93
 
@@ -100,10 +100,8 @@ module Datagrid
100
100
  end
101
101
 
102
102
  def can_preload?(scope, association)
103
- !! scope.klass.reflect_on_association(association)
103
+ !!scope.klass.reflect_on_association(association)
104
104
  end
105
-
106
105
  end
107
106
  end
108
107
  end
109
-
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Datagrid
2
4
  module Drivers
3
5
  # @!visibility private
4
6
  class Sequel < AbstractDriver
5
-
6
7
  def self.match?(scope)
7
8
  return false unless defined?(::Sequel)
9
+
8
10
  if scope.is_a?(Class)
9
11
  scope.ancestors.include?(::Sequel::Model)
10
12
  else
@@ -14,11 +16,8 @@ module Datagrid
14
16
 
15
17
  def to_scope(scope)
16
18
  return scope if scope.is_a?(::Sequel::Dataset)
17
- scope.where({})
18
- end
19
19
 
20
- def append_column_queries(assets, columns)
21
- super
20
+ scope.where({})
22
21
  end
23
22
 
24
23
  def where(scope, attribute, value)
@@ -33,12 +32,8 @@ module Datagrid
33
32
  scope.order(::Sequel.desc(::Sequel.lit(order)))
34
33
  end
35
34
 
36
- def reverse_order(scope)
37
- super
38
- end
39
-
40
35
  def default_order(scope, column_name)
41
- has_column?(scope, column_name) ? ::Sequel.lit(prefix_table_name(scope, column_name)) : nil
36
+ scope_has_column?(scope, column_name) ? ::Sequel.lit(prefix_table_name(scope, column_name)) : nil
42
37
  end
43
38
 
44
39
  def greater_equal(scope, field, value)
@@ -49,7 +44,7 @@ module Datagrid
49
44
  scope.where(::Sequel.lit("#{prefix_table_name(scope, field)} <= ?", value))
50
45
  end
51
46
 
52
- def has_column?(scope, column_name)
47
+ def scope_has_column?(scope, column_name)
53
48
  scope.columns.include?(column_name.to_sym)
54
49
  end
55
50
 
@@ -65,13 +60,14 @@ module Datagrid
65
60
  def normalized_column_type(scope, field)
66
61
  type = column_type(scope, field)
67
62
  return nil unless type
63
+
68
64
  {
69
- [:string, :blob, :time] => :string,
70
- [:integer, :primary_key] => :integer,
71
- [:float, :decimal] => :float,
65
+ %i[string blob time] => :string,
66
+ %i[integer primary_key] => :integer,
67
+ %i[float decimal] => :float,
72
68
  [:date] => :date,
73
69
  [:datetime] => :timestamp,
74
- [:boolean] => :boolean
70
+ [:boolean] => :boolean,
75
71
  }.each do |keys, value|
76
72
  return value if keys.include?(type)
77
73
  end
@@ -96,18 +92,17 @@ module Datagrid
96
92
  end
97
93
 
98
94
  def can_preload?(scope, association)
99
- !! scope.model.association_reflection(association)
95
+ !!scope.model.association_reflection(association)
100
96
  end
101
97
 
102
-
103
98
  protected
104
99
 
105
100
  def prefix_table_name(scope, field)
106
- has_column?(scope, field) ? [to_scope(scope).row_proc.table_name, field].join(".") : field
101
+ scope_has_column?(scope, field) ? [to_scope(scope).row_proc.table_name, field].join(".") : field
107
102
  end
108
103
 
109
104
  def column_type(scope, field)
110
- has_column?(scope, field) ? to_scope(scope).row_proc.db_schema[field.to_sym][:type] : nil
105
+ scope_has_column?(scope, field) ? to_scope(scope).row_proc.db_schema[field.to_sym][:type] : nil
111
106
  end
112
107
  end
113
108
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "datagrid/drivers/abstract_driver"
2
4
  require "datagrid/drivers/active_record"
3
5
  require "datagrid/drivers/array"
@@ -5,7 +7,6 @@ require "datagrid/drivers/mongoid"
5
7
  require "datagrid/drivers/mongo_mapper"
6
8
  require "datagrid/drivers/sequel"
7
9
 
8
-
9
10
  module Datagrid
10
11
  # @!visibility private
11
12
  module Drivers
@@ -1,12 +1,20 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rails/engine"
4
+ require "datagrid/helper"
5
+ require "datagrid/form_builder"
2
6
 
3
7
  module Datagrid
4
- # @!private
8
+ # @!visibility private
5
9
  class Engine < ::Rails::Engine
10
+ def self.extend_modules
11
+ ActionView::Base.include(Datagrid::Helper)
12
+ ActionView::Helpers::FormBuilder.include(Datagrid::FormBuilder)
13
+ end
14
+
6
15
  initializer "datagrid.helpers" do
7
- #TODO: check why it doesn't work
8
16
  ActiveSupport.on_load :action_view do
9
- include Datagrid::Helper
17
+ Engine.extend_modules
10
18
  end
11
19
  end
12
20
  end