commonbase 0.1.3 → 0.2.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 +4 -4
- data/lib/commonbase/concerns/date_validations.rb +48 -0
- data/lib/commonbase/helpers/log_helpers.rb +92 -0
- data/lib/commonbase/helpers/utils.rb +10 -0
- data/lib/commonbase/version.rb +3 -3
- data/lib/commonbase.rb +9 -7
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7cf0b1b44b4a7dcbf307fa34a85e743942e2eebc2cc6b52e5ccdd8d5ac60d208
|
4
|
+
data.tar.gz: 3ef45a67a92b232404f54904d1773a2e8d39c6b2289dc298a981d5d08fef4c3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a0d3e2b1f1cfd24b673312d51469458dce1c9c2f79e22928246241e5404374dc1fcf2cc026b1f40c15c48bc6f8ef3ff587f52bdea9d2bd43aace9d874440bbb
|
7
|
+
data.tar.gz: 98d22e582b2f07afc0e6abde125a72bdbb1f28c6f9a0ef1c7865f3600c72e2282e4ccf2ebb2a5aef9fe0ca6c39c6e7a964bd87393ae40ee0939cedbee3df67c9
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Commonbase
|
2
|
+
module DateValidations
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
class_methods do
|
6
|
+
def validate_future_date(attribute, options = {})
|
7
|
+
validates_each attribute do |record, attr, value|
|
8
|
+
if value.present? && value <= Date.today
|
9
|
+
record.errors.add(attr, options[:message] || "must be in the future")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate_past_date(attribute, options = {})
|
15
|
+
validates_each attribute do |record, attr, value|
|
16
|
+
if value.present? && value >= Date.today
|
17
|
+
record.errors.add(attr, options[:message] || "must be in the past")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def validate_date_within_range(attribute, options = {})
|
23
|
+
validates_each attribute do |record, attr, value|
|
24
|
+
next unless value.present?
|
25
|
+
|
26
|
+
begin_date = options[:begin].present? ? options[:begin] : nil
|
27
|
+
end_date = options[:end].present? ? options[:end] : nil
|
28
|
+
|
29
|
+
unless (value.to_date rescue nil).present?
|
30
|
+
record.errors.add(attr, "must be a valid date")
|
31
|
+
end
|
32
|
+
|
33
|
+
unless (begin_date.to_date rescue nil).present? || (end_date.to_date rescue nil).present?
|
34
|
+
record.errors.add(attr, "must have options with a valid date for begin or end")
|
35
|
+
end
|
36
|
+
|
37
|
+
if begin_date.present? && value < begin_date
|
38
|
+
record.errors.add(attr, "must be after or equal to #{begin_date.strftime('%Y-%m-%d')}")
|
39
|
+
end
|
40
|
+
|
41
|
+
if end_date.present? && value > end_date
|
42
|
+
record.errors.add(attr, "must be before or equal to #{end_date.strftime('%Y-%m-%d')}")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,92 @@
|
|
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
|
+
private
|
86
|
+
def log_message(level, message, data = nil)
|
87
|
+
return unless level.present?
|
88
|
+
|
89
|
+
Rails.logger.public_send(level, "#{level.upcase}: #{message} #{data.inspect if data.present?}")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/lib/commonbase/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Commonbase
|
2
|
-
VERSION = "0.
|
3
|
-
end
|
1
|
+
module Commonbase
|
2
|
+
VERSION = "0.2.0"
|
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
|
-
|
6
|
-
|
7
|
-
|
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.
|
4
|
+
version: 0.2.0
|
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-
|
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
|