datagrid 1.8.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +2 -0
- data/CHANGELOG.md +11 -1
- data/{Readme.markdown → README.md} +44 -29
- data/app/assets/stylesheets/datagrid.css +145 -0
- data/app/views/datagrid/_enum_checkboxes.html.erb +5 -3
- data/app/views/datagrid/_form.html.erb +4 -4
- data/app/views/datagrid/_head.html.erb +26 -3
- data/app/views/datagrid/_range_filter.html.erb +5 -3
- data/app/views/datagrid/_row.html.erb +12 -1
- data/app/views/datagrid/_table.html.erb +4 -4
- data/datagrid.gemspec +8 -7
- data/lib/datagrid/active_model.rb +9 -17
- data/lib/datagrid/base.rb +39 -0
- data/lib/datagrid/column_names_attribute.rb +9 -11
- data/lib/datagrid/columns/column.rb +155 -133
- data/lib/datagrid/columns.rb +325 -115
- data/lib/datagrid/configuration.rb +33 -4
- data/lib/datagrid/core.rb +89 -54
- data/lib/datagrid/deprecated_object.rb +20 -0
- data/lib/datagrid/drivers/abstract_driver.rb +12 -23
- data/lib/datagrid/drivers/active_record.rb +24 -26
- data/lib/datagrid/drivers/array.rb +22 -14
- data/lib/datagrid/drivers/mongo_mapper.rb +15 -14
- data/lib/datagrid/drivers/mongoid.rb +15 -17
- data/lib/datagrid/drivers/sequel.rb +14 -19
- data/lib/datagrid/drivers.rb +2 -1
- data/lib/datagrid/engine.rb +11 -3
- data/lib/datagrid/filters/base_filter.rb +166 -143
- data/lib/datagrid/filters/boolean_filter.rb +19 -5
- data/lib/datagrid/filters/date_filter.rb +33 -35
- data/lib/datagrid/filters/date_time_filter.rb +24 -16
- data/lib/datagrid/filters/default_filter.rb +9 -3
- data/lib/datagrid/filters/dynamic_filter.rb +151 -105
- data/lib/datagrid/filters/enum_filter.rb +43 -19
- data/lib/datagrid/filters/extended_boolean_filter.rb +39 -31
- data/lib/datagrid/filters/float_filter.rb +15 -5
- data/lib/datagrid/filters/integer_filter.rb +21 -10
- data/lib/datagrid/filters/ranged_filter.rb +66 -45
- data/lib/datagrid/filters/select_options.rb +58 -49
- data/lib/datagrid/filters/string_filter.rb +9 -4
- data/lib/datagrid/filters.rb +204 -79
- data/lib/datagrid/form_builder.rb +116 -128
- data/lib/datagrid/generators/scaffold.rb +184 -0
- data/lib/datagrid/generators/views.rb +20 -0
- data/lib/datagrid/helper.rb +436 -69
- data/lib/datagrid/ordering.rb +26 -29
- data/lib/datagrid/rspec.rb +6 -10
- data/lib/datagrid/utils.rb +37 -30
- data/lib/datagrid/version.rb +3 -1
- data/lib/datagrid.rb +8 -28
- data/templates/base.rb.erb +6 -4
- data/templates/grid.rb.erb +1 -1
- metadata +17 -17
- data/app/assets/stylesheets/datagrid.sass +0 -134
- data/lib/datagrid/filters/composite_filters.rb +0 -49
- data/lib/datagrid/renderer.rb +0 -157
- data/lib/datagrid/scaffold.rb +0 -129
- data/lib/tasks/datagrid_tasks.rake +0 -15
- data/templates/controller.rb.erb +0 -6
- data/templates/index.html.erb +0 -5
@@ -1,56 +1,77 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
8
|
-
end
|
3
|
+
module Datagrid
|
4
|
+
module Filters
|
5
|
+
module RangedFilter
|
6
|
+
SERIALIZED_RANGE = %r{\A(.*)\.{2,3}(.*)\z}
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
32
|
+
def range?
|
33
|
+
options[:range]
|
34
|
+
end
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
47
|
-
|
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
|
-
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
33
|
-
|
34
|
-
end
|
29
|
+
def include_blank
|
30
|
+
return if prompt
|
35
31
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
module Datagrid
|
4
|
+
module Filters
|
5
|
+
class StringFilter < Datagrid::Filters::BaseFilter
|
6
|
+
include Datagrid::Filters::RangedFilter
|
4
7
|
|
5
|
-
|
6
|
-
|
8
|
+
def parse(value)
|
9
|
+
value&.to_s
|
10
|
+
end
|
11
|
+
end
|
7
12
|
end
|
8
13
|
end
|