datagrid 1.8.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +2 -0
  3. data/CHANGELOG.md +11 -1
  4. data/{Readme.markdown → README.md} +44 -29
  5. data/app/assets/stylesheets/datagrid.css +145 -0
  6. data/app/views/datagrid/_enum_checkboxes.html.erb +5 -3
  7. data/app/views/datagrid/_form.html.erb +4 -4
  8. data/app/views/datagrid/_head.html.erb +26 -3
  9. data/app/views/datagrid/_range_filter.html.erb +5 -3
  10. data/app/views/datagrid/_row.html.erb +12 -1
  11. data/app/views/datagrid/_table.html.erb +4 -4
  12. data/datagrid.gemspec +8 -7
  13. data/lib/datagrid/active_model.rb +9 -17
  14. data/lib/datagrid/base.rb +39 -0
  15. data/lib/datagrid/column_names_attribute.rb +9 -11
  16. data/lib/datagrid/columns/column.rb +155 -133
  17. data/lib/datagrid/columns.rb +325 -115
  18. data/lib/datagrid/configuration.rb +33 -4
  19. data/lib/datagrid/core.rb +89 -54
  20. data/lib/datagrid/deprecated_object.rb +20 -0
  21. data/lib/datagrid/drivers/abstract_driver.rb +12 -23
  22. data/lib/datagrid/drivers/active_record.rb +24 -26
  23. data/lib/datagrid/drivers/array.rb +22 -14
  24. data/lib/datagrid/drivers/mongo_mapper.rb +15 -14
  25. data/lib/datagrid/drivers/mongoid.rb +15 -17
  26. data/lib/datagrid/drivers/sequel.rb +14 -19
  27. data/lib/datagrid/drivers.rb +2 -1
  28. data/lib/datagrid/engine.rb +11 -3
  29. data/lib/datagrid/filters/base_filter.rb +166 -143
  30. data/lib/datagrid/filters/boolean_filter.rb +19 -5
  31. data/lib/datagrid/filters/date_filter.rb +33 -35
  32. data/lib/datagrid/filters/date_time_filter.rb +24 -16
  33. data/lib/datagrid/filters/default_filter.rb +9 -3
  34. data/lib/datagrid/filters/dynamic_filter.rb +151 -105
  35. data/lib/datagrid/filters/enum_filter.rb +43 -19
  36. data/lib/datagrid/filters/extended_boolean_filter.rb +39 -31
  37. data/lib/datagrid/filters/float_filter.rb +15 -5
  38. data/lib/datagrid/filters/integer_filter.rb +21 -10
  39. data/lib/datagrid/filters/ranged_filter.rb +66 -45
  40. data/lib/datagrid/filters/select_options.rb +58 -49
  41. data/lib/datagrid/filters/string_filter.rb +9 -4
  42. data/lib/datagrid/filters.rb +204 -79
  43. data/lib/datagrid/form_builder.rb +116 -128
  44. data/lib/datagrid/generators/scaffold.rb +184 -0
  45. data/lib/datagrid/generators/views.rb +20 -0
  46. data/lib/datagrid/helper.rb +436 -69
  47. data/lib/datagrid/ordering.rb +26 -29
  48. data/lib/datagrid/rspec.rb +6 -10
  49. data/lib/datagrid/utils.rb +37 -30
  50. data/lib/datagrid/version.rb +3 -1
  51. data/lib/datagrid.rb +8 -28
  52. data/templates/base.rb.erb +6 -4
  53. data/templates/grid.rb.erb +1 -1
  54. metadata +17 -17
  55. data/app/assets/stylesheets/datagrid.sass +0 -134
  56. data/lib/datagrid/filters/composite_filters.rb +0 -49
  57. data/lib/datagrid/renderer.rb +0 -157
  58. data/lib/datagrid/scaffold.rb +0 -129
  59. data/lib/tasks/datagrid_tasks.rake +0 -15
  60. data/templates/controller.rb.erb +0 -6
  61. data/templates/index.html.erb +0 -5
@@ -1,56 +1,77 @@
1
- module Datagrid::Filters::RangedFilter
1
+ # frozen_string_literal: true
2
2
 
3
- def initialize(grid, name, options, &block)
4
- super(grid, name, options, &block)
5
- if range?
6
- options[:multiple] = true
7
- end
8
- end
3
+ module Datagrid
4
+ module Filters
5
+ module RangedFilter
6
+ SERIALIZED_RANGE = %r{\A(.*)\.{2,3}(.*)\z}
9
7
 
10
- def parse_values(value)
11
- result = super(value)
12
- return result if !range? || result.nil?
13
- # Simulate single point range
14
- return [result, result] unless result.is_a?(Array)
15
-
16
- case result.size
17
- when 0
18
- nil
19
- when 1
20
- result.first
21
- when 2
22
- if result.first && result.last && result.first > result.last
23
- # If wrong range is given - reverse it to be always valid
24
- result.reverse
25
- elsif !result.first && !result.last
26
- nil
27
- else
28
- result
8
+ def parse_values(value)
9
+ return super unless range?
10
+
11
+ case value
12
+ when String
13
+ if ["..", "..."].include?(value)
14
+ nil
15
+ elsif (match = value.match(SERIALIZED_RANGE))
16
+ to_range(match.captures[0], match.captures[1], value.include?("..."))
17
+ else
18
+ super
19
+ end
20
+ when Hash
21
+ parse_hash(value)
22
+ when Array
23
+ parse_array(value)
24
+ when Range
25
+ to_range(value.begin, value.end)
26
+ else
27
+ result = super
28
+ to_range(result, result)
29
+ end
29
30
  end
30
- else
31
- raise ArgumentError, "Can not create a date range from array of more than two: #{result.inspect}"
32
- end
33
31
 
34
- end
32
+ def range?
33
+ options[:range]
34
+ end
35
35
 
36
- def range?
37
- options[:range]
38
- end
36
+ def default_filter_where(scope, value)
37
+ if range? && value.is_a?(Range)
38
+ scope = driver.greater_equal(scope, name, value.begin) if value.begin
39
+ scope = driver.less_equal(scope, name, value.end) if value.end
40
+ scope
41
+ else
42
+ super
43
+ end
44
+ end
39
45
 
40
- def default_filter_where(scope, value)
41
- if range? && value.is_a?(Array)
42
- left, right = value
43
- if left
44
- scope = driver.greater_equal(scope, name, left)
46
+ protected
47
+
48
+ def parse_hash(result)
49
+ to_range(result[:from] || result["from"], result[:to] || result["to"])
45
50
  end
46
- if right
47
- scope = driver.less_equal(scope, name, right)
51
+
52
+ def to_range(from, to, exclusive = false)
53
+ from = parse(from)
54
+ to = parse(to)
55
+ return nil unless to || from
56
+
57
+ # If wrong range is given - reverse it to be always valid
58
+ from, to = to, from if from && to && from > to
59
+ exclusive ? from...to : from..to
48
60
  end
49
- scope
50
- else
51
- super(scope, value)
52
- end
53
- end
54
61
 
62
+ def parse_array(result)
63
+ first = result.first
64
+ last = result.last
55
65
 
66
+ case result.size
67
+ when 0
68
+ nil
69
+ when 1, 2
70
+ to_range(first, last)
71
+ else
72
+ raise ArgumentError, "Can not create a range from array of more than two elements"
73
+ end
74
+ end
75
+ end
76
+ end
56
77
  end
@@ -1,57 +1,66 @@
1
- module Datagrid::Filters::SelectOptions
2
- def select(object)
3
- select = self.options[:select]
4
- if select.is_a?(Symbol)
5
- object.send(select)
6
- elsif select.respond_to?(:call)
7
- Datagrid::Utils.apply_args(object, &select)
8
- else
9
- select
10
- end
11
- end
1
+ # frozen_string_literal: true
12
2
 
13
- def select_values(object)
14
- options = select(object)
15
- groups_used = grouped_choices?(options)
16
- options.map do |option|
17
- if groups_used
18
- option[1].map {|o| option_text_and_value(o)}
19
- else
20
- option_text_and_value(option)
3
+ module Datagrid
4
+ module Filters
5
+ module SelectOptions
6
+ def select(object)
7
+ select = options[:select]
8
+ if select.is_a?(Symbol)
9
+ object.send(select)
10
+ elsif select.respond_to?(:call)
11
+ Datagrid::Utils.apply_args(object, &select)
12
+ else
13
+ select
14
+ end
21
15
  end
22
- end.map(&:last)
23
- end
24
16
 
25
- def include_blank
26
- unless prompt
27
- options.has_key?(:include_blank) ?
28
- Datagrid::Utils.callable(options[:include_blank]) : !multiple?
29
- end
30
- end
17
+ def select_values(object)
18
+ options = select(object)
19
+ groups_used = grouped_choices?(options)
20
+ options.map do |option|
21
+ if groups_used
22
+ option[1].map { |o| option_text_and_value(o) }
23
+ else
24
+ option_text_and_value(option)
25
+ end
26
+ end.map(&:last)
27
+ end
31
28
 
32
- def prompt
33
- options.has_key?(:prompt) ? Datagrid::Utils.callable(options[:prompt]) : false
34
- end
29
+ def include_blank
30
+ return if prompt
35
31
 
36
- protected
37
-
38
- # Rails built-in method:
39
- # https://github.com/rails/rails/blob/94e80269e36caf7b2d22a7ab68e6898d3a824122/actionview/lib/action_view/helpers/form_options_helper.rb#L789
40
- # Code reuse is difficult, so it is easier to duplicate it
41
- def option_text_and_value(option)
42
- # Options are [text, value] pairs or strings used for both.
43
- if !option.is_a?(String) && option.respond_to?(:first) && option.respond_to?(:last)
44
- option = option.reject { |e| Hash === e } if Array === option
45
- [option.first, option.last]
46
- else
47
- [option, option]
48
- end
49
- end
32
+ if options.key?(:include_blank)
33
+ Datagrid::Utils.callable(options[:include_blank])
34
+ else
35
+ !multiple?
36
+ end
37
+ end
38
+
39
+ def prompt
40
+ options.key?(:prompt) ? Datagrid::Utils.callable(options[:prompt]) : false
41
+ end
42
+
43
+ protected
50
44
 
51
- # Rails built-in method:
52
- # https://github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4/actionview/lib/action_view/helpers/tags/select.rb#L36
53
- # Code reuse is difficult, so it is easier to duplicate it
54
- def grouped_choices?(choices)
55
- !choices.blank? && choices.first.respond_to?(:last) && Array === choices.first.last
45
+ # Rails built-in method:
46
+ # https://github.com/rails/rails/blob/94e80269e36caf7b2d22a7ab68e6898d3a824122/actionview/lib/action_view/helpers/form_options_helper.rb#L789
47
+ # Code reuse is difficult, so it is easier to duplicate it
48
+ def option_text_and_value(option)
49
+ # Options are [text, value] pairs or strings used for both.
50
+ if !option.is_a?(String) && option.respond_to?(:first) && option.respond_to?(:last)
51
+ option = option.reject { |e| e.is_a?(Hash) } if option.is_a?(Array)
52
+ [option.first, option.last]
53
+ else
54
+ [option, option]
55
+ end
56
+ end
57
+
58
+ # Rails built-in method:
59
+ # https://github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4/actionview/lib/action_view/helpers/tags/select.rb#L36
60
+ # Code reuse is difficult, so it is easier to duplicate it
61
+ def grouped_choices?(choices)
62
+ !choices.blank? && choices.first.respond_to?(:last) && choices.first.last.is_a?(Array)
63
+ end
64
+ end
56
65
  end
57
66
  end
@@ -1,8 +1,13 @@
1
- class Datagrid::Filters::StringFilter < Datagrid::Filters::BaseFilter
1
+ # frozen_string_literal: true
2
2
 
3
- include Datagrid::Filters::RangedFilter
3
+ module Datagrid
4
+ module Filters
5
+ class StringFilter < Datagrid::Filters::BaseFilter
6
+ include Datagrid::Filters::RangedFilter
4
7
 
5
- def parse(value)
6
- value.nil? ? nil : value.to_s
8
+ def parse(value)
9
+ value&.to_s
10
+ end
11
+ end
7
12
  end
8
13
  end