eac_rails_utils 0.17.2 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: efe1c5f0500cc98440fb31e38b2ecdcf3959d5d415016ac4ad622d1a309a4695
4
- data.tar.gz: 23e56f4eae5a1126c3136971cb84d74755aeed649f18eafc6dc10dc6118f1327
3
+ metadata.gz: 3e4525483ddef206757144a407560d2b7569db1933b16935da157aef0e8645c4
4
+ data.tar.gz: 163bb84353cde0a519afaba18890920cf8f04920291f8e457d81431743e61bf4
5
5
  SHA512:
6
- metadata.gz: 63ec98f3e567a68e2aa5f82d09e9fa8283861ea7d2801175f2969b13abaa126511c73a22ed7d35c5862f4a9a4c67f4553049aef96a775a4fcfae8af6e214ea13
7
- data.tar.gz: f626434211fb0d7743fc22e6deb20fe0edc216f7ec6d425cf5514e88c1652e8b37031c4f5ebf202a41e277ca22163e7dc8012bc34c865be7d05c62d87542a286
6
+ metadata.gz: 7313d2b4e45e6893a574df04e25ef627d0edea64820eda383a22ead2bd21243d377fb659ced3c58c84726f1f06d1c0904678510a6a60263e4aac88c45742d414
7
+ data.tar.gz: 8d5b6f1ab06c37696f0dd780e4017c519c41e1a2c717e9bbe3fa066824a30766fd84641546bdabe20978cb3adbba8808b06384bc3ec8ef3a4e93bf61c378f678
@@ -1,48 +1,51 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'eac_ruby_utils/core_ext'
4
+
3
5
  module EacRailsUtils
4
6
  module DataTableHelper
5
7
  class Column
6
8
  EMPTY_VALUE = '-'
7
9
 
8
- attr_reader :label
10
+ common_constructor :args, :block
11
+
12
+ # @return [String]
13
+ def label
14
+ args[0]
15
+ end
9
16
 
10
- def initialize(label, path, block)
11
- @label = label
12
- @path = path.to_s.split('.')
13
- @block = block
17
+ # @return [String]
18
+ def path
19
+ args[1].to_s.split('.')
14
20
  end
15
21
 
16
22
  def record_value(record)
17
- v = Node.new(record, @path).value
23
+ v = ::EacRailsUtils::DataTableHelper::ColumnNode.new(record, path).value
18
24
  if v.present?
19
- @block ? @block.call(v) : v
25
+ block ? block.call(v) : v
20
26
  else
21
27
  EMPTY_VALUE
22
28
  end
23
29
  end
24
30
 
25
- private
31
+ # @param attribute [Symbol]
32
+ # @param value [Proc, Object]
33
+ # @return [self]
34
+ def value_cell(attribute, value = nil, &block)
35
+ value_cell_attributes[attribute.to_sym] = block.if_present(value)
26
36
 
27
- def node_value(node, subpath)
28
- return node if subpath.empty?
37
+ self
29
38
  end
30
39
 
31
- class Node
32
- def initialize(node, path)
33
- @node = node
34
- @path = path
35
- end
36
-
37
- def value
38
- return @node if @node.nil? || @path.empty?
40
+ # @return [Hash]
41
+ def value_cell_attributes
42
+ @value_cell_attributes ||= {}
43
+ end
39
44
 
40
- subpath = @path.dup
41
- n = subpath.shift
42
- return Node.new(@node.send(n), subpath).value if @node.respond_to?(n)
45
+ private
43
46
 
44
- raise "Instance of #{@node.class} does not respond to #{n}"
45
- end
47
+ def node_value(node, subpath)
48
+ return node if subpath.empty?
46
49
  end
47
50
  end
48
51
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacRailsUtils
6
+ module DataTableHelper
7
+ class ColumnNode
8
+ common_constructor :node, :path
9
+
10
+ # @return [Boolean]
11
+ def ended?
12
+ node.nil? || path.empty?
13
+ end
14
+
15
+ # @return [Object]
16
+ def value
17
+ ended? ? node : child_value
18
+ end
19
+
20
+ private
21
+
22
+ # @return [Object]
23
+ def child_value
24
+ subpath = path.dup
25
+ n = subpath.shift
26
+ return self.class.new(node.send(n), subpath).value if node.respond_to?(n)
27
+
28
+ raise "Instance of #{node.class} does not respond to #{n}"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacRailsUtils
6
+ module DataTableHelper
7
+ class DataTable
8
+ class ValueCell
9
+ enable_method_class
10
+ common_constructor :data_table, :column, :record
11
+ delegate :view, to: :data_table
12
+
13
+ # @return [ActiveSupport::SafeBuffer]
14
+ def result
15
+ view.content_tag('td', column.record_value(record), tag_attributes)
16
+ end
17
+
18
+ # @return [Object]
19
+ def tag_attribute_value(value)
20
+ value.is_a?(::Proc) ? value.call(record) : value
21
+ end
22
+
23
+ # @return [Hash]
24
+ def tag_attributes
25
+ column.value_cell_attributes.map { |k, v| [k, tag_attribute_value(v)] }.to_h
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,17 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'eac_ruby_utils/core_ext'
4
+
3
5
  module EacRailsUtils
4
6
  module DataTableHelper
5
7
  class DataTable
6
- def initialize(view, dataset)
7
- @view = view
8
- @dataset = dataset
9
- @setup = ::EacRailsUtils::DataTableHelper::Setup.new
10
- yield(@setup)
11
- end
8
+ common_constructor :view, :dataset, :setup_block, block_arg: true
12
9
 
13
10
  def output
14
- @view.content_tag(:table, id: id) do
11
+ view.content_tag(:table, id: id) do
15
12
  head << body
16
13
  end << script
17
14
  end
@@ -19,29 +16,29 @@ module EacRailsUtils
19
16
  private
20
17
 
21
18
  def head
22
- @view.content_tag(:thead) do
23
- @view.content_tag(:tr) do
24
- @view.safe_join(@setup.columns.map { |c| @view.content_tag('th', c.label) })
19
+ view.content_tag(:thead) do
20
+ view.content_tag(:tr) do
21
+ view.safe_join(setup.columns.map { |c| view.content_tag('th', c.label) })
25
22
  end
26
23
  end
27
24
  end
28
25
 
29
26
  def body
30
- @view.content_tag(:tbody) do
31
- @view.safe_join(@dataset.map { |r| row(r) })
27
+ view.content_tag(:tbody) do
28
+ view.safe_join(dataset.map { |r| row(r) })
32
29
  end
33
30
  end
34
31
 
35
32
  def row(record)
36
- @view.content_tag(:tr) do
37
- @view.safe_join(
38
- @setup.columns.map { |c| @view.content_tag('td', c.record_value(record)) << "\n" }
33
+ view.content_tag(:tr) do
34
+ view.safe_join(
35
+ setup.columns.map { |c| value_cell(c, record) << "\n" }
39
36
  )
40
37
  end << "\n"
41
38
  end
42
39
 
43
40
  def script
44
- @view.javascript_tag <<~JS_CODE
41
+ view.javascript_tag <<~JS_CODE
45
42
  $(document).ready(function () {
46
43
  $('##{id}').DataTable({
47
44
  paging: #{@setup.paging ? 'true' : 'false'}
@@ -53,6 +50,17 @@ module EacRailsUtils
53
50
  def id
54
51
  @id ||= SecureRandom.hex(32)
55
52
  end
53
+
54
+ # @return [EacRailsUtils::DataTableHelper::Setup]
55
+ def setup
56
+ @setup ||= begin
57
+ r = ::EacRailsUtils::DataTableHelper::Setup.new
58
+ setup_block.call(r)
59
+ r
60
+ end
61
+ end
62
+
63
+ require_sub __FILE__, require_mode: :kernel
56
64
  end
57
65
  end
58
66
  end
@@ -11,8 +11,11 @@ module EacRailsUtils
11
11
  @paging = true
12
12
  end
13
13
 
14
- def column(label, path = nil, &block)
15
- @columns << ::EacRailsUtils::DataTableHelper::Column.new(label, path, block)
14
+ # @return [EacRailsUtils::DataTableHelper::Column]
15
+ def column(*args, &block)
16
+ column = ::EacRailsUtils::DataTableHelper::Column.new(args, block)
17
+ @columns << column
18
+ column
16
19
  end
17
20
  end
18
21
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRailsUtils
4
+ class ImmutableValidator < ActiveModel::EachValidator
5
+ DEFAULT_MESSAGE = 'cannot be changed'
6
+
7
+ def validate_each(record, attribute, _value)
8
+ return if record.new_record?
9
+ return unless record.send("#{attribute}_changed?")
10
+
11
+ record.errors[attribute] << (options[:message] || DEFAULT_MESSAGE)
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRailsUtils
4
- VERSION = '0.17.2'
4
+ VERSION = '0.19.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_rails_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.2
4
+ version: 0.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - E.A.C.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-23 00:00:00.000000000 Z
11
+ date: 2023-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel-associations
@@ -193,7 +193,9 @@ files:
193
193
  - app/helpers/eac_rails_utils/common_form_helper/form_builder/year_month_field.rb
194
194
  - app/helpers/eac_rails_utils/data_table_helper.rb
195
195
  - app/helpers/eac_rails_utils/data_table_helper/column.rb
196
+ - app/helpers/eac_rails_utils/data_table_helper/column_node.rb
196
197
  - app/helpers/eac_rails_utils/data_table_helper/data_table.rb
198
+ - app/helpers/eac_rails_utils/data_table_helper/data_table/value_cell.rb
197
199
  - app/helpers/eac_rails_utils/data_table_helper/setup.rb
198
200
  - app/helpers/eac_rails_utils/formatter_helper.rb
199
201
  - app/helpers/eac_rails_utils/links_helper.rb
@@ -203,6 +205,7 @@ files:
203
205
  - app/helpers/eac_rails_utils/menus_helper/gui_builder.rb
204
206
  - app/helpers/eac_rails_utils/open_graph_protocol_helper.rb
205
207
  - app/validators/eac_rails_utils/cpf_validator.rb
208
+ - app/validators/eac_rails_utils/immutable_validator.rb
206
209
  - app/validators/eac_rails_utils/no_presence_validator.rb
207
210
  - app/validators/eac_rails_utils/yaml_validator.rb
208
211
  - config/initializers/assets.rb