commonbase 0.1.3 → 0.2.1

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: 785407da3257dc77ec3e159982f08642f92ba7c259f9317e0324a9ab8aaf12c4
4
- data.tar.gz: b8afbb76b6256bdd6c7f8becfe97383aa8b888c166721650300539f0d6c665f5
3
+ metadata.gz: '09314c0aa23ebba5f883831c62f545fe457545724a66a2716d0dd4f907cfac92'
4
+ data.tar.gz: 521b98bf484b4fc2da12a040329d6f9ec24b1c461a485bf99674de0e466f573c
5
5
  SHA512:
6
- metadata.gz: 9333a964e6368104778641c22deb6d619511c50aaf8acf53de57392e00f0758a14510cc92541c9681ae637297f24d368d121c0175ee280550ebd74b402237312
7
- data.tar.gz: 2a26e45a94c9ce46ae78a6d5e61d6f03cb31ef5288162ea24c0b4da7820a2caf4abe2e934dda8247734473edd5e3a528c514e40279f693264a43d49b28a206d5
6
+ metadata.gz: 183a8500985b7c4b0618ac5b56b0301de02283319e82fa3b487f86ad08acff40f82ca5eca08c0485c485970bc2838872441c16a964f7a3aeefa8d6eb1a7029fa
7
+ data.tar.gz: 1f97167fc68097c15f3752b6edd3cb78e3372f4cfab48b0f41dd0972dc671dc93feb9a04eacb782e9c9601b0b8d68e9231e4592393bf279c9a5c6f19631eeb44
@@ -1,5 +1,5 @@
1
- module Commonbase
2
- class ApplicationRecord < ActiveRecord::Base
3
- self.abstract_class = true
4
- end
5
- end
1
+ module Commonbase
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,58 @@
1
+ module Commonbase
2
+ module DateValidations
3
+ extend ActiveSupport::Concern
4
+
5
+ class_methods do
6
+ # Validate if a date is in future. By default today is invalid. Set include_today to true to include today.
7
+ def validate_future_date(attribute, options = {})
8
+ validates_each attribute do |record, attr, value|
9
+ next unless value.present?
10
+
11
+ include_today = options[:include_today] || false
12
+
13
+ if value < Date.today || (value == Date.today && include_today.equal?(false))
14
+ record.errors.add(attr, options[:message] || "must be in the future")
15
+ end
16
+ end
17
+ end
18
+
19
+ # Validate if a date is in the past. By default today is invalid. Set include_today to true to include today.
20
+ def validate_past_date(attribute, options = {})
21
+ validates_each attribute do |record, attr, value|
22
+ next unless value.present?
23
+
24
+ include_today = options[:include_today] || false
25
+
26
+ if value > Date.today || (value == Date.today && include_today.equal?(false))
27
+ record.errors.add(attr, options[:message] || "must be in the past")
28
+ end
29
+ end
30
+ end
31
+
32
+ def validate_date_within_range(attribute, options = {})
33
+ validates_each attribute do |record, attr, value|
34
+ next unless value.present?
35
+
36
+ begin_date = options[:begin].present? ? options[:begin] : nil
37
+ end_date = options[:end].present? ? options[:end] : nil
38
+
39
+ unless (value.to_date rescue nil).present?
40
+ record.errors.add(attr, "must be a valid date")
41
+ end
42
+
43
+ unless (begin_date.to_date rescue nil).present? || (end_date.to_date rescue nil).present?
44
+ record.errors.add(attr, "must have options with a valid date for begin or end")
45
+ end
46
+
47
+ if begin_date.present? && value < begin_date
48
+ record.errors.add(attr, "must be after or equal to #{begin_date.strftime('%Y-%m-%d')}")
49
+ end
50
+
51
+ if end_date.present? && value > end_date
52
+ record.errors.add(attr, "must be before or equal to #{end_date.strftime('%Y-%m-%d')}")
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,91 @@
1
+ module Commonbase
2
+ module LogHelpers
3
+ # ++
4
+ # Log an exception details
5
+ # ++
6
+ # @param exception [Exception] Exception object
7
+ # @param level [Symbol] Log level, default to :error
8
+ # @param data [Hash] Additional data to log
9
+ # --
10
+
11
+ def log_exception(exception, level = :error, data = nil)
12
+ unless level.present?
13
+ level = :error
14
+ end
15
+
16
+ log_message(level, "Exception to_s: #{ exception.to_s }", data)
17
+ log_message(level, "Exception message: #{ exception.message }", data)
18
+ log_message(level, "Exception type: #{ exception.class }", data)
19
+ log_message(level, "Exception backtrace: #{ exception.backtrace&.join("\n") }", data)
20
+ end
21
+
22
+ # ++
23
+ # Log an error message
24
+ # ++
25
+ # @param message [String] Error message
26
+ # @param data [Hash] Additional data to log
27
+ # --
28
+
29
+ def log_error(message, data = nil)
30
+ log_message(:error, message, data)
31
+ end
32
+
33
+ # ++
34
+ # Log an info message
35
+ # ++
36
+ # @param message [String] Information message
37
+ # @param data [Hash] Additional data to log
38
+ # --
39
+
40
+ def log_info(message, data = nil)
41
+ log_message(:info, message, data)
42
+ end
43
+
44
+ # ++
45
+ # Log SMTP settings
46
+ # ++
47
+ # @param with_password [Boolean] Keep password in the log, default to false
48
+ # @param level [Symbol] Log level, default to :info
49
+ # --
50
+
51
+ def log_smtp_settings(with_password = false, level = :info)
52
+ smtp_settings = ActionMailer::Base.smtp_settings.dup()
53
+
54
+ unless with_password
55
+ smtp_settings.delete(:password)
56
+ end
57
+
58
+ unless level.present?
59
+ level = :info
60
+ end
61
+
62
+ log_message(level, "SMTP settings: #{ smtp_settings.to_json }")
63
+ end
64
+
65
+ # ++
66
+ # Log a request details
67
+ # ++
68
+ # @param request [ActionDispatch::Request] Request object
69
+ # @param level [Symbol] Log level, default to :info
70
+ # --
71
+
72
+ def log_request(request, level = :info)
73
+ return if request.nil?
74
+
75
+ unless level.present?
76
+ level = :info
77
+ end
78
+
79
+ log_message(level, "Request url:", request.url)
80
+ log_message(level, "Request method:", request.method)
81
+ log_message(level, "Request params:", request.params)
82
+ log_message(level, "Request body:", request.body&.read)
83
+ end
84
+
85
+ def log_message(level, message, data = nil)
86
+ return unless level.present?
87
+
88
+ Rails.logger.public_send(level, "#{level.upcase}: #{message} #{data.inspect if data.present?}")
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,10 @@
1
+ module Commonbase
2
+ module Utils
3
+ def self.parse_date(str, format = nil)
4
+ return nil unless str.present?
5
+ return format.present? ? Date.strptime(str, format) : Date.strptime(str)
6
+ rescue ArgumentError
7
+ nil
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
- module Commonbase
2
- VERSION = "0.1.3"
3
- end
1
+ module Commonbase
2
+ VERSION = "0.2.1"
3
+ end
data/lib/commonbase.rb CHANGED
@@ -1,7 +1,9 @@
1
- require "commonbase/version"
2
- require "commonbase/engine"
3
- require "commonbase/concerns/ransack_searchable"
4
-
5
- module Commonbase
6
- # Your code goes here...
7
- end
1
+ require "commonbase/version"
2
+ require "commonbase/engine"
3
+ require "commonbase/concerns/ransack_searchable"
4
+ require "commonbase/concerns/date_validations"
5
+ require "commonbase/helpers/log_helpers"
6
+
7
+ module Commonbase
8
+ # Your code goes here...
9
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commonbase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-02-19 00:00:00.000000000 Z
11
+ date: 2025-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -44,8 +44,11 @@ files:
44
44
  - app/views/layouts/commonbase/application.html.erb
45
45
  - config/routes.rb
46
46
  - lib/commonbase.rb
47
+ - lib/commonbase/concerns/date_validations.rb
47
48
  - lib/commonbase/concerns/ransack_searchable.rb
48
49
  - lib/commonbase/engine.rb
50
+ - lib/commonbase/helpers/log_helpers.rb
51
+ - lib/commonbase/helpers/utils.rb
49
52
  - lib/commonbase/version.rb
50
53
  - lib/tasks/commonbase_tasks.rake
51
54
  homepage: https://abizvn.com