zutils 3.0.5 → 4.0.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.
@@ -0,0 +1,173 @@
1
+ module Zutils
2
+ module Helpers
3
+ def eval_with_rescue(code)
4
+ eval(code)
5
+ rescue Exception => e
6
+ "error"
7
+ end
8
+
9
+ # ── Flash ────────────────────────────────────────────────────────────
10
+ def bootstrap_flash(options = {})
11
+ flash_messages = []
12
+
13
+ begin
14
+ object_name = controller_name.singularize
15
+ obj = instance_variable_get("@#{object_name}")
16
+
17
+ if obj && obj.respond_to?(:errors) && obj.errors.size > 0
18
+ msg = if obj.errors[:base].present?
19
+ obj.errors[:base]
20
+ else
21
+ I18n.t("helpers.links.#{action_name}_error",
22
+ model: Kernel.const_get(object_name.camelize).model_name.human, default: "erro de validação")
23
+ end
24
+ flash[:error] = msg if msg.present?
25
+ end
26
+ rescue
27
+ # ignore
28
+ end
29
+
30
+ flash.each do |type, message|
31
+ message = Array(message).select(&:presence)
32
+ next if message.empty?
33
+ data = case type.to_s
34
+ when "success", "notice"
35
+ { title: options[:success_title] || "Sucesso!", icon: options[:success_icon] || "fas fa-check", type: :success, kind: "success" }
36
+ when "error"
37
+ { title: options[:error_title] || "Erro!", icon: options[:error_icon] || "fas fa-ban", type: :danger, kind: "error" }
38
+ when "alert"
39
+ { title: options[:alert_title] || "Alerta!", icon: options[:alert_icon] || "fas fa-exclamation-triangle", type: :warning, kind: "warning" }
40
+ else
41
+ { title: options[:info_title] || "Aviso", icon: options[:info_icon] || "fas fa-info", type: :info, kind: "info" }
42
+ end
43
+
44
+ next unless data
45
+
46
+ tag_options = {
47
+ class: "alert alert-#{data[:type]} alert-dismissible d-flex align-items-center",
48
+ role: "alert",
49
+ data: { kind: data[:kind], text: message.first.to_s }
50
+ }
51
+
52
+ close_button = content_tag(:button, "", type: "button", class: "btn-close",
53
+ "data-bs-dismiss": "alert", "aria-label": "Fechar")
54
+
55
+ icon = content_tag(:i, "", class: "#{data[:icon]} me-2")
56
+ title = content_tag(:strong, data[:title])
57
+ text = safe_join([title, " ", sanitize(Array(message).first.to_s.gsub("|", "<br>"), tags: %w[br], attributes: [])])
58
+
59
+ content = safe_join([close_button, icon, text])
60
+ flash_messages << content_tag(:div, content, tag_options)
61
+ end
62
+
63
+ flash[:error] = nil
64
+ safe_join(flash_messages, "\n")
65
+ end
66
+
67
+ def render_turbo_stream_flash_messages(local: false)
68
+ partial = local ? "shared/flash" : "shared/flash"
69
+ turbo_stream.prepend(local ? "localflash" : "flash", partial: partial)
70
+ end
71
+
72
+ def render_turbo_stream_flash_messages_only_toastr
73
+ turbo_stream.prepend "flash", partial: "shared/flash_only_toastr"
74
+ end
75
+
76
+ # ── DOM helpers ──────────────────────────────────────────────────────
77
+ def nested_dom_id(*args)
78
+ args.map { |arg| arg.respond_to?(:to_key) ? dom_id(arg) : arg.to_s }.join("_")
79
+ end
80
+
81
+ # ── Menu ─────────────────────────────────────────────────────────────
82
+ def menu_activated?(menu_item)
83
+ check = ->(test) { eval_with_rescue(test) rescue false }
84
+
85
+ check.call(menu_item.dig(:active_test)) ||
86
+ menu_item.dig(:children).to_a.any? { |c| check.call(c[:active_test]) } ||
87
+ menu_item.dig(:children).to_a.flat_map { |c| c.dig(:children).to_a }.any? { |c| check.call(c[:active_test]) }
88
+ end
89
+
90
+ # ── Display helpers (BS5) ────────────────────────────────────────────
91
+ def display(object, field, options = {})
92
+ icon = options[:icon] || "fa fa-stream"
93
+ title = options[:title] || object.class.human_attribute_name(field)
94
+ value = options[:value] || interactive_send(object, field)
95
+ hide_title = options[:hide_title] || false
96
+
97
+ parts = []
98
+ unless hide_title
99
+ parts << content_tag(:strong, tag.i("", class: icon) + " #{title}")
100
+ end
101
+ parts << content_tag(:p, value.presence || "-", class: "text-body-secondary")
102
+ safe_join(parts, "\n")
103
+ end
104
+
105
+ def display_boolean(object, field, options = {})
106
+ bool_value = options[:value] || interactive_send(object, field)
107
+ label = bool_value ? (options[:true_label] || "Sim") : (options[:false_label] || "Não")
108
+ css = bool_value ? "bg-success" : "bg-danger"
109
+
110
+ badge = content_tag(:span, label, class: "badge #{css}")
111
+ display(object, field, options.merge(value: badge))
112
+ end
113
+
114
+ def display_boolean_icon(object, field, options = {})
115
+ bool_value = options[:value] || interactive_send(object, field)
116
+ title = options[:title] || object.class.human_attribute_name(field)
117
+ icon_true = options[:icon_true] || options[:icon] || "fa fa-thumbs-up"
118
+ icon_false = options[:icon_false] || options[:icon] || "fa fa-thumbs-down"
119
+ css = bool_value ? "bg-success" : "bg-danger"
120
+
121
+ icon = bool_value ? tag.i("", class: icon_true) : tag.i("", class: icon_false)
122
+ content = content_tag(:span, icon, class: "badge #{css}",
123
+ data: { "bs-toggle": "tooltip", "bs-title": "#{title}: #{bool_value ? "Sim" : "Não"}" })
124
+ end
125
+
126
+ def interactive_send(obj, field)
127
+ field.to_s.split(".").each { |m| obj = obj.send(m) if obj }
128
+
129
+ case obj
130
+ when Date, Time, DateTime, ActiveSupport::TimeWithZone
131
+ I18n.l(obj)
132
+ else
133
+ obj
134
+ end
135
+ end
136
+
137
+ # ── Section / Header ─────────────────────────────────────────────────
138
+ def section_header(title, icon: "fa-info-circle", color: "primary", border: true)
139
+ content_tag(:h5, class: "text-uppercase small text-#{color} fw-bold mb-4 #{"border-bottom" if border} pb-2") do
140
+ concat tag.i(class: "fa #{icon} me-2")
141
+ concat title
142
+ end
143
+ end
144
+
145
+ def smart_date_tag(date, icon: nil, label: nil, text_class: "text-body")
146
+ return "-" if date.blank?
147
+
148
+ day_month = l(date, format: "%d %b")
149
+ if date.year > Date.today.year
150
+ year_tag = " <small class='opacity-75' style='font-size: 0.85em;'>#{date.year}</small>".html_safe
151
+ else
152
+ year_tag = ""
153
+ end
154
+
155
+ parts = []
156
+ parts << tag.i("", class: "bi bi-#{icon} me-1 text-muted") if icon.present?
157
+ parts << content_tag(:span, "#{label}:", class: "small text-muted me-1") if label.present?
158
+ parts << "#{day_month}#{year_tag}".html_safe
159
+
160
+ content_tag(:span, safe_join(parts), class: text_class)
161
+ end
162
+
163
+ def format_date(value)
164
+ return if value.blank?
165
+
166
+ Date.iso8601(value.to_s).strftime("%d/%m/%Y")
167
+ rescue
168
+ Date.parse(value.to_s).strftime("%d/%m/%Y")
169
+ rescue
170
+ value
171
+ end
172
+ end
173
+ end
@@ -1,3 +1,3 @@
1
1
  module Zutils
2
- VERSION = "3.0.5"
2
+ VERSION = "4.0.0"
3
3
  end
data/lib/zutils.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require "zutils/version"
2
2
  require "zutils/engine"
3
+ require "zutils/helpers"
3
4
 
4
5
  module Zutils
5
- # Your code goes here...
6
- end
6
+ end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zutils
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.5
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Viana
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-12-18 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rails
@@ -16,79 +15,82 @@ dependencies:
16
15
  requirements:
17
16
  - - ">="
18
17
  - !ruby/object:Gem::Version
19
- version: '5.0'
18
+ version: '7.0'
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
- version: '5.0'
25
+ version: '7.0'
27
26
  - !ruby/object:Gem::Dependency
28
27
  name: bundler
29
28
  requirement: !ruby/object:Gem::Requirement
30
29
  requirements:
31
- - - "~>"
30
+ - - ">="
32
31
  - !ruby/object:Gem::Version
33
- version: '1.16'
32
+ version: '2.0'
34
33
  type: :development
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
- - - "~>"
37
+ - - ">="
39
38
  - !ruby/object:Gem::Version
40
- version: '1.16'
39
+ version: '2.0'
41
40
  - !ruby/object:Gem::Dependency
42
41
  name: rake
43
42
  requirement: !ruby/object:Gem::Requirement
44
43
  requirements:
45
- - - "~>"
44
+ - - ">="
46
45
  - !ruby/object:Gem::Version
47
- version: '10.0'
46
+ version: '13.0'
48
47
  type: :development
49
48
  prerelease: false
50
49
  version_requirements: !ruby/object:Gem::Requirement
51
50
  requirements:
52
- - - "~>"
51
+ - - ">="
53
52
  - !ruby/object:Gem::Version
54
- version: '10.0'
55
- description: Utilidades gerais (views, models, helpers) para aplicações rails
53
+ version: '13.0'
54
+ description: Engine Rails com partials Bootstrap 5, helpers (bootstrap_flash, eval_with_rescue)
55
+ e controllers Stimulus reutilizáveis (filtering, toastr, bootstrap-table, dropdown,
56
+ tooltip) para os apps captei, sofia e reduz.
56
57
  email:
57
58
  - zezim.ricardo@gmail.com
58
59
  executables: []
59
60
  extensions: []
60
61
  extra_rdoc_files: []
61
62
  files:
62
- - ".gitignore"
63
- - CODE_OF_CONDUCT.md
64
- - Gemfile
65
63
  - LICENSE.txt
66
64
  - README.md
67
- - Rakefile
68
- - app/assets/javascripts/zutils.js
69
65
  - app/assets/stylesheets/zutils.scss
70
66
  - app/views/shared/_action_links.html.erb
71
67
  - app/views/shared/_btn_action_links.html.erb
72
68
  - app/views/shared/_card_list.html.erb
69
+ - app/views/shared/_dropdown_menu.html.erb
70
+ - app/views/shared/_empty_state.html.erb
71
+ - app/views/shared/_empty_state_card.html.erb
73
72
  - app/views/shared/_fields.html.erb
73
+ - app/views/shared/_file_chooser.html.erb
74
74
  - app/views/shared/_flash.html.erb
75
+ - app/views/shared/_flash_only_toastr.html.erb
75
76
  - app/views/shared/_form.html.erb
76
77
  - app/views/shared/_index.html.erb
77
78
  - app/views/shared/_list.html.erb
78
79
  - app/views/shared/_modal.html.erb
80
+ - app/views/shared/_pagination.html.erb
79
81
  - app/views/shared/_search_form.html.erb
80
82
  - app/views/shared/_show.html.erb
83
+ - app/views/shared/_turbo_modal.html.erb
81
84
  - bin/console
82
85
  - bin/setup
83
86
  - lib/zutils.rb
84
87
  - lib/zutils/engine.rb
88
+ - lib/zutils/helpers.rb
85
89
  - lib/zutils/version.rb
86
- - zutils.gemspec
87
90
  homepage: http://gitlab.tjpi.jus.br/
88
91
  licenses:
89
92
  - MIT
90
93
  metadata: {}
91
- post_install_message:
92
94
  rdoc_options: []
93
95
  require_paths:
94
96
  - lib
@@ -96,15 +98,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
98
  requirements:
97
99
  - - ">="
98
100
  - !ruby/object:Gem::Version
99
- version: '0'
101
+ version: '3.0'
100
102
  required_rubygems_version: !ruby/object:Gem::Requirement
101
103
  requirements:
102
104
  - - ">="
103
105
  - !ruby/object:Gem::Version
104
106
  version: '0'
105
107
  requirements: []
106
- rubygems_version: 3.5.23
107
- signing_key:
108
+ rubygems_version: 3.6.7
108
109
  specification_version: 4
109
- summary: Utilidades gerais para aplicações rails
110
+ summary: Utilidades gerais (views, helpers e controllers Stimulus) para aplicações
111
+ Rails
110
112
  test_files: []
data/.gitignore DELETED
@@ -1,8 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
data/CODE_OF_CONDUCT.md DELETED
@@ -1,74 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
11
-
12
- ## Our Standards
13
-
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
16
-
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
22
-
23
- Examples of unacceptable behavior by participants include:
24
-
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
28
- * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
31
- * Other conduct which could reasonably be considered inappropriate in a
32
- professional setting
33
-
34
- ## Our Responsibilities
35
-
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
39
-
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
45
-
46
- ## Scope
47
-
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
-
55
- ## Enforcement
56
-
57
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at ricardo.viana@tjpi.jus.br. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
-
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
67
-
68
- ## Attribution
69
-
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [http://contributor-covenant.org/version/1/4][version]
72
-
73
- [homepage]: http://contributor-covenant.org
74
- [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in zutils.gemspec
6
- gemspec
data/Rakefile DELETED
@@ -1,2 +0,0 @@
1
- require "bundler/gem_tasks"
2
- task :default => :spec
@@ -1,86 +0,0 @@
1
- //= require selectize
2
-
3
- $(document).ready(function () {
4
- var selectizeCallback = null;
5
-
6
- $(".selectize_with_add").each(function(i, item) {
7
- $(item).selectize({
8
- create: function(input, callback) {
9
- selectizeCallback = callback;
10
-
11
- $("." + $(item).data('name') + "-modal").modal();
12
- $("#" + $(item).data('name') + "_name").val(input);
13
- }
14
- });
15
-
16
- $("." + $(item).data('name') + "-modal").on("hide.bs.modal", function(e) {
17
- if (selectizeCallback != null) {
18
- selectizeCallback();
19
- selecitzeCallback = null;
20
- }
21
-
22
- $("." + $(item).data('name') + "-modal form").trigger("reset");
23
- $("." + $(item).data('name') + "-modal form select").val(null).trigger('change');
24
- $.rails.enableFormElements($("." + $(item).data('name') + "-modal form"));
25
- });
26
-
27
- $("." + $(item).data('name') + "-modal form").on("submit", function(e) {
28
- e.preventDefault();
29
- $("." + $(item).data('name') + "-modal form").find('.has-error').removeClass('has-error');
30
- $("." + $(item).data('name') + "-modal form").find('.help-block').text("");
31
- $.ajax({
32
- method: "POST",
33
- dataType: "json",
34
- url: $(this).attr("action"),
35
- data: $(this).serialize(),
36
- success: function(response) {
37
- selectizeCallback({value: response.id, text: response.name});
38
- selectizeCallback = null;
39
-
40
- $("." + $(item).data('name') + "-modal").modal('toggle');
41
- },
42
- error: function(response) {
43
- $.each(response.responseJSON, function(key, value) {
44
- $("." + $(item).data('name') + "-modal form").find("." + $(item).data('model') + '_' + key).addClass('has-error');
45
- if (!($("." + $(item).data('name') + "-modal form" + " ." + $(item).data('model') + '_' + key).find('.help-block').length)) {
46
- $("." + $(item).data('name') + "-modal form").find("." + $(item).data('model') + '_' + key).append("<p class='help-block'></p>");
47
- }
48
- $("." + $(item).data('name') + "-modal form").find("." + $(item).data('model') + '_' + key).find('.help-block').text(value);
49
- });
50
- $.rails.enableFormElements($("." + $(item).data('name') + "-modal form" + ""));
51
- }
52
- });
53
- });
54
- });
55
-
56
- $(".selectize_with_add_without_modal").each(function(i, item) {
57
- $(item).selectize({
58
- create: function(input, callback) {
59
- $(item).parent().removeClass('has-error');
60
- $(item).parent().find('.help-block').text("");
61
- selectizeCallback = callback;
62
- data = {};
63
- data[$(item).data('model')] = {};
64
- data[$(item).data('model')][$(item).data('field')] = input;
65
- $.ajax({
66
- method: "POST",
67
- dataType: "json",
68
- url: "/" + $(item).data('pluralized'),
69
- data: data,
70
- success: function(response) {
71
- selectizeCallback({value: response.id, text: response.name});
72
- },
73
- error: function(response) {
74
- $(item).parent().addClass('has-error');
75
- if (!( $(item).parent().find('.help-block').length)) {
76
- $(item).parent().append("<p class='help-block'></p>");
77
- }
78
- $(item).parent().find('.help-block').text("Valor já existente: " + input);
79
- selectizeCallback();
80
- }
81
- });
82
- selectizeCallback = callback;
83
- }
84
- });
85
- });
86
- });
data/zutils.gemspec DELETED
@@ -1,39 +0,0 @@
1
-
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "zutils/version"
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "zutils"
8
- spec.version = Zutils::VERSION
9
- spec.authors = ["Ricardo Viana"]
10
- spec.email = ["zezim.ricardo@gmail.com"]
11
-
12
- spec.summary = "Utilidades gerais para aplicações rails"
13
- spec.description = "Utilidades gerais (views, models, helpers) para aplicações rails"
14
- spec.homepage = "http://gitlab.tjpi.jus.br/"
15
- spec.license = "MIT"
16
-
17
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
- # to allow pushing to a single host or delete this section to allow pushing to any host.
19
- # if spec.respond_to?(:metadata)
20
- # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
- # else
22
- # raise "RubyGems 2.0 or newer is required to protect against " \
23
- # "public gem pushes."
24
- # end
25
-
26
- # Specify which files should be added to the gem when it is released.
27
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
- end
31
- spec.bindir = "exe"
32
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
- spec.require_paths = ["lib"]
34
-
35
- spec.add_dependency 'rails', '>= 5.0'
36
-
37
- spec.add_development_dependency "bundler", "~> 1.16"
38
- spec.add_development_dependency "rake", "~> 10.0"
39
- end