active_record_date_validator 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ad28cfed6d5afe1678fc6b40bb834067c5234556f797ce0d3e8e1b682404c803
4
+ data.tar.gz: 362e15240eb7b390a4d77477864bb7060f992ad56b2677fb836c6a2279545543
5
+ SHA512:
6
+ metadata.gz: 9f784404d242bd1233e6ee3d4c8f3699e06481cb0e8041e83a70f7dfaf8917c8fff028afa81fc79d2bb15c7a3c6f5cbdf21883127dedf4b77751a84677aba165
7
+ data.tar.gz: cdf05846bf61eeee135f1f08020df2ecb7842fd3fa76b291028a4412766912564fffa9ecfc82e25909385df20ceca5f0b6be3d888a7e950d9f1e438e5905aba5
@@ -0,0 +1,11 @@
1
+ require "rails/railtie"
2
+
3
+ module ActiveRecordDateValidator
4
+ class Railtie < Rails::Railtie
5
+ initializer "active_record_date_validator.include_concern" do
6
+ ActiveSupport.on_load(:active_record) do
7
+ ActiveRecord::Base.include(ActiveRecordDateValidator)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordDateValidator
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,35 @@
1
+ module ActiveRecordDateValidator
2
+ extend ActiveSupport::Concern
3
+
4
+ class DateValidator < ActiveModel::EachValidator
5
+ def validate_each(record, attribute, value)
6
+ # Get the raw value before typecasting
7
+ raw_value = record.read_attribute_before_type_cast(attribute)
8
+
9
+ # Skip validation for nil values
10
+ return if raw_value.nil?
11
+
12
+ # Only allow Date, Time, or ActiveSupport::TimeWithZone objects
13
+ # Reject ANY string values regardless of format
14
+ if raw_value.is_a?(String) ||
15
+ (!raw_value.is_a?(Date) && !raw_value.is_a?(Time) && !raw_value.is_a?(ActiveSupport::TimeWithZone))
16
+ record.errors.add(attribute, "must be a valid date or datetime")
17
+ end
18
+ end
19
+ end
20
+
21
+ class_methods do
22
+ def validates_date(exclude_columns: [])
23
+ date_columns = columns.select { |col| [:date, :datetime].include?(col.type) }.map(&:name)
24
+ date_columns -= exclude_columns.map(&:to_s)
25
+
26
+ date_columns.each do |field|
27
+ # Use our custom validator on each date column
28
+ validates field, date: true
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ # Include the module in ActiveRecord::Base to make it available to all models
35
+ ActiveRecord::Base.include(ActiveRecordDateValidator)
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_date_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Palpandi Ramar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.11'
55
+ description: This gem provides a Rails concern to validate date fields in ActiveRecord
56
+ models, allowing the exclusion of specific fields if needed.
57
+ email:
58
+ - palpandi26@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - lib/active_record_date_validator.rb
64
+ - lib/active_record_date_validator/railtie.rb
65
+ - lib/active_record_date_validator/version.rb
66
+ homepage: https://github.com/Palpandi-Ramar/active_record_date_validator
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ source_code_uri: https://github.com/Palpandi-Ramar/active_record_date_validator
71
+ changelog_uri: https://github.com/Palpandi-Ramar/active_record_date_validator/blob/main/CHANGELOG.md
72
+ homepage_uri: https://github.com/Palpandi-Ramar/active_record_date_validator
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.1.6
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: A validator for date fields in ActiveRecord models.
92
+ test_files: []