datagrid 1.8.0 → 1.8.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,7 +27,6 @@ module Datagrid
27
27
  alias descending? descending
28
28
 
29
29
  end
30
- base.include InstanceMethods
31
30
  end
32
31
 
33
32
  # @!visibility private
@@ -37,96 +36,94 @@ module Datagrid
37
36
  end
38
37
  end
39
38
 
40
- module InstanceMethods
41
39
 
42
- # @!visibility private
43
- def assets
44
- check_order_valid!
45
- apply_order(super)
46
- end
40
+ # @!visibility private
41
+ def assets
42
+ check_order_valid!
43
+ apply_order(super)
44
+ end
47
45
 
48
- # @return [Datagrid::Columns::Column, nil] a column definition that is currently used to order assets
49
- # @example
50
- # class MyGrid
51
- # scope { Model }
52
- # column(:id)
53
- # column(:name)
54
- # end
55
- # MyGrid.new(order: "name").order_column # => #<Column name: "name", ...>
56
- def order_column
57
- order ? column_by_name(order) : nil
58
- end
46
+ # @return [Datagrid::Columns::Column, nil] a column definition that is currently used to order assets
47
+ # @example
48
+ # class MyGrid
49
+ # scope { Model }
50
+ # column(:id)
51
+ # column(:name)
52
+ # end
53
+ # MyGrid.new(order: "name").order_column # => #<Column name: "name", ...>
54
+ def order_column
55
+ order ? column_by_name(order) : nil
56
+ end
59
57
 
60
- # @param column [String, Datagrid::Columns::Column]
61
- # @return [Boolean] true if given grid is ordered by given column.
62
- def ordered_by?(column)
63
- order_column == column_by_name(column)
64
- end
58
+ # @param column [String, Datagrid::Columns::Column]
59
+ # @return [Boolean] true if given grid is ordered by given column.
60
+ def ordered_by?(column)
61
+ order_column == column_by_name(column)
62
+ end
65
63
 
66
- private
64
+ private
67
65
 
68
- def apply_order(assets)
69
- return assets unless order
70
- if order_column.order_by_value?
71
- assets = assets.sort_by do |asset|
72
- order_column.order_by_value(asset, self)
73
- end
74
- descending? ? assets.reverse : assets
75
- else
76
- if descending?
77
- if order_column.order_desc
78
- apply_asc_order(assets, order_column.order_desc)
79
- else
80
- apply_desc_order(assets, order_column.order)
81
- end
66
+ def apply_order(assets)
67
+ return assets unless order
68
+ if order_column.order_by_value?
69
+ assets = assets.sort_by do |asset|
70
+ order_column.order_by_value(asset, self)
71
+ end
72
+ descending? ? assets.reverse : assets
73
+ else
74
+ if descending?
75
+ if order_column.order_desc
76
+ apply_asc_order(assets, order_column.order_desc)
82
77
  else
83
- apply_asc_order(assets, order_column.order)
78
+ apply_desc_order(assets, order_column.order)
84
79
  end
80
+ else
81
+ apply_asc_order(assets, order_column.order)
85
82
  end
86
83
  end
84
+ end
87
85
 
88
- def check_order_valid!
89
- return unless order
90
- column = column_by_name(order)
91
- unless column
92
- self.class.order_unsupported(order, "no column #{order} in #{self.class}")
93
- end
94
- unless column.supports_order?
95
- self.class.order_unsupported(column.name, "column don't support order" )
96
- end
86
+ def check_order_valid!
87
+ return unless order
88
+ column = column_by_name(order)
89
+ unless column
90
+ self.class.order_unsupported(order, "no column #{order} in #{self.class}")
97
91
  end
98
-
99
- def apply_asc_order(assets, order)
100
- if order.respond_to?(:call)
101
- apply_block_order(assets, order)
102
- else
103
- driver.asc(assets, order)
104
- end
92
+ unless column.supports_order?
93
+ self.class.order_unsupported(column.name, "column don't support order" )
105
94
  end
95
+ end
106
96
 
107
- def apply_desc_order(assets, order)
108
- if order.respond_to?(:call)
109
- reverse_order(apply_asc_order(assets, order))
110
- else
111
- driver.desc(assets, order)
112
- end
97
+ def apply_asc_order(assets, order)
98
+ if order.respond_to?(:call)
99
+ apply_block_order(assets, order)
100
+ else
101
+ driver.asc(assets, order)
113
102
  end
103
+ end
114
104
 
115
- def reverse_order(assets)
116
- driver.reverse_order(assets)
117
- rescue NotImplementedError
118
- self.class.order_unsupported(order_column.name, "Your ORM do not support reverse order: please specify :order_desc option manually")
105
+ def apply_desc_order(assets, order)
106
+ if order.respond_to?(:call)
107
+ reverse_order(apply_asc_order(assets, order))
108
+ else
109
+ driver.desc(assets, order)
119
110
  end
111
+ end
120
112
 
121
- def apply_block_order(assets, order)
122
- case order.arity
123
- when -1, 0
124
- assets.instance_eval(&order)
125
- when 1
126
- order.call(assets)
127
- else
128
- self.class.order_unsupported(order_column.name, "Order option proc can not handle more than one argument")
129
- end
113
+ def reverse_order(assets)
114
+ driver.reverse_order(assets)
115
+ rescue NotImplementedError
116
+ self.class.order_unsupported(order_column.name, "Your ORM do not support reverse order: please specify :order_desc option manually")
117
+ end
118
+
119
+ def apply_block_order(assets, order)
120
+ case order.arity
121
+ when -1, 0
122
+ assets.instance_eval(&order)
123
+ when 1
124
+ order.call(assets)
125
+ else
126
+ self.class.order_unsupported(order_column.name, "Order option proc can not handle more than one argument")
130
127
  end
131
128
  end
132
129
  end
@@ -46,7 +46,7 @@ shared_examples_for "Datagrid" do
46
46
  1
47
47
  when :enum
48
48
  select = subject.select_options(filter)
49
- select.first.try(:last)
49
+ select.first&.last
50
50
  else
51
51
  raise "unknown filter type: #{filter.class}"
52
52
  end.to_s
@@ -54,7 +54,7 @@ shared_examples_for "Datagrid" do
54
54
 
55
55
  before(:each) do
56
56
  subject.attributes = {filter.name => filter_value}
57
- subject.send(filter.name).should_not be_nil
57
+ subject.public_send(filter.name).should_not be_nil
58
58
  end
59
59
 
60
60
  it "should be supported" do
@@ -4,7 +4,7 @@ class Datagrid::Scaffold < Rails::Generators::NamedBase
4
4
 
5
5
  include Rails::Generators::ResourceHelpers
6
6
 
7
- check_class_collision :suffix => "Grid"
7
+ check_class_collision suffix: "Grid"
8
8
  source_root File.expand_path(__FILE__ + "/../../../templates")
9
9
 
10
10
  def create_scaffold
@@ -13,7 +13,7 @@ class Datagrid::Scaffold < Rails::Generators::NamedBase
13
13
  end
14
14
  template "grid.rb.erb", "app/grids/#{grid_class_name.underscore}.rb"
15
15
  if file_exists?(grid_controller_file)
16
- inject_into_file grid_controller_file, index_action, :after => %r{class .*#{grid_controller_class_name}.*\n}
16
+ inject_into_file grid_controller_file, index_action, after: %r{class .*#{grid_controller_class_name}.*\n}
17
17
  else
18
18
  template "controller.rb.erb", grid_controller_file
19
19
  end
@@ -30,7 +30,7 @@ class Datagrid::Scaffold < Rails::Generators::NamedBase
30
30
  }.each do |extension, string|
31
31
  file = "app/assets/stylesheets/application.#{extension}"
32
32
  if file_exists?(file)
33
- inject_into_file file, string + "\n", {:before => %r{.*require_self}} # before all
33
+ inject_into_file file, string + "\n", {before: %r{.*require_self}} # before all
34
34
  end
35
35
  end
36
36
  end
@@ -38,15 +38,12 @@ module Datagrid
38
38
  end
39
39
 
40
40
  def add_html_classes(options, *classes)
41
+ return options if classes.empty?
41
42
  options = options.clone
42
- options[:class] ||= ""
43
- if options[:class].is_a?(Array)
44
- options[:class] += classes
45
- else
46
- # suppose that it is a String
47
- options[:class] += " " unless options[:class].blank?
48
- options[:class] += classes.join(" ")
49
- end
43
+ options[:class] ||= []
44
+ array = options[:class].is_a?(Array)
45
+ value = [*options[:class], *classes]
46
+ options[:class] = array ? value : value.join(" ")
50
47
  options
51
48
  end
52
49
 
@@ -117,9 +114,9 @@ module Datagrid
117
114
  if !value
118
115
  value
119
116
  elsif value.is_a?(Array)
120
- [value.first.try(:beginning_of_day), value.last.try(:end_of_day)]
117
+ [value.first&.beginning_of_day, value.last&.end_of_day]
121
118
  elsif value.is_a?(Range)
122
- (value.first.beginning_of_day..value.last.end_of_day)
119
+ (value.begin&.beginning_of_day..value.end&.end_of_day)
123
120
  else
124
121
  value.beginning_of_day..value.end_of_day
125
122
  end
@@ -1,3 +1,3 @@
1
1
  module Datagrid
2
- VERSION = "1.8.0"
2
+ VERSION = "1.8.2"
3
3
  end
@@ -11,11 +11,35 @@ class BaseGrid
11
11
  # Enable forbidden attributes protection
12
12
  # self.forbidden_attributes_protection = true
13
13
 
14
- def self.date_column(name, *args)
14
+ # Makes a date column
15
+ # @param name [Symbol] Column name
16
+ # @param args [Array] Other column helper arguments
17
+ # @example
18
+ # date_column(:created_at)
19
+ # date_column(:owner_registered_at) do |model|
20
+ # model.owner.registered_at
21
+ # end
22
+ def self.date_column(name, *args, &block)
15
23
  column(name, *args) do |model|
16
- format(block_given? ? yield : model.send(name)) do |date|
17
- date.strftime("%m/%d/%Y")
24
+ format(block ? block.call(model) : model.public_send(name)) do |date|
25
+ date&.strftime("%m/%d/%Y") || "&mdash;".html_safe
18
26
  end
19
27
  end
20
28
  end
29
+
30
+ # Makes a boolean YES/NO column
31
+ # @param name [Symbol] Column name
32
+ # @param args [Array] Other column helper arguments
33
+ # @example
34
+ # boolean_column(:approved)
35
+ # boolean_column(:has_tasks, preload: :tasks) do |model|
36
+ # model.tasks.unfinished.any?
37
+ # end
38
+ def self.boolean_column(name, *args, &block)
39
+ column(name, *args) do |model|
40
+ value = block ? block.call(model) : model.public_send(name)
41
+ value ? "Yes" : "No"
42
+ end
43
+ end
44
+
21
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datagrid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bogdan Gusiev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-12 00:00:00.000000000 Z
11
+ date: 2024-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -16,16 +16,16 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '6.0'
19
+ version: '6.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '6.0'
27
- description: This allows you to easily build datagrid aka data tables with sortable
28
- columns and filters
26
+ version: '6.1'
27
+ description: The library allows you to easily build datagrid aka data tables with
28
+ sortable columns and filters
29
29
  email: agresso@gmail.com
30
30
  executables: []
31
31
  extensions: []
@@ -113,8 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  - !ruby/object:Gem::Version
114
114
  version: '0'
115
115
  requirements: []
116
- rubygems_version: 3.4.15
116
+ rubygems_version: 3.4.10
117
117
  signing_key:
118
118
  specification_version: 4
119
- summary: Ruby gem to create datagrids
119
+ summary: Library that provides DSL to present table like data
120
120
  test_files: []