active_record_date_formatted 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f991af11095bec503a41b9fa2a4878cf71d1f2cc
|
4
|
+
data.tar.gz: 3d89165fb84272b49bfcaed7a274451a284bf35b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fbf96b27f6058ce58f6b7f7d3551f3e3c3cfd61f6b566a851675152ce96c8545c1ba362ae4724780a39c07b317883d593d8041d860b9852b896343bf67240d66
|
7
|
+
data.tar.gz: e1429508768c1cc223456b598e0c9c4ab77674b7df5e3afb6a6a5f93f799f9807dd63808b8cbcffa3c93ea6042027be069fd155d2b55c8d04f3bce96ed685eb1
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module ActiveRecordDateFormatted
|
2
|
+
class DateFormatValidator < ActiveModel::EachValidator
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
return false if value.blank?
|
5
|
+
begin
|
6
|
+
Date.strptime(value, I18n.t("date.formats.default"))
|
7
|
+
rescue ArgumentError
|
8
|
+
record.errors[attribute] << :invalid
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_record_date_formatted/date_format_validator'
|
2
|
+
|
1
3
|
# Creates additional instance getter and setter methods for each date attribute with postfix _formatted
|
2
4
|
# These methods use date string formatted with the current locale.
|
3
5
|
module ActiveRecordDateFormatted
|
@@ -11,13 +13,21 @@ module ActiveRecordDateFormatted
|
|
11
13
|
def add_date_formatted_methods
|
12
14
|
self.column_types.each do |attr_name, c|
|
13
15
|
if c.type == :date
|
16
|
+
attr_accessor "#{attr_name}_formatted"
|
17
|
+
validates "#{attr_name}_formatted", "active_record_date_formatted/date_format" => true
|
18
|
+
before_save "save_formatted_#{attr_name}"
|
19
|
+
|
14
20
|
define_method "#{attr_name}_formatted" do
|
15
|
-
|
16
|
-
|
21
|
+
if instance_variable_get("@#{attr_name}_formatted").nil?
|
22
|
+
date_value = read_attribute(attr_name)
|
23
|
+
date_value.nil? ? nil : date_value.strftime(I18n.t "date.formats.default")
|
24
|
+
else
|
25
|
+
instance_variable_get("@#{attr_name}_formatted")
|
26
|
+
end
|
17
27
|
end
|
18
28
|
|
19
|
-
define_method "#{attr_name}
|
20
|
-
write_attribute(attr_name,
|
29
|
+
define_method "save_formatted_#{attr_name}" do
|
30
|
+
write_attribute(attr_name, self.send("#{attr_name}_formatted").blank? ? nil : Date.strptime(self.send("#{attr_name}_formatted"), I18n.t("date.formats.default")))
|
21
31
|
end
|
22
32
|
end
|
23
33
|
end
|
data/test/dummy/log/test.log
CHANGED
@@ -133,3 +133,48 @@ ActiveRecordDateFormattedTest: test_module_defined
|
|
133
133
|
[1m[36mSQL (0.1ms)[0m [1mINSERT INTO "people" ("birth_date", "created_at", "updated_at") VALUES (?, ?, ?)[0m [["birth_date", "1971-02-18"], ["created_at", "2015-10-06 06:41:45.076957"], ["updated_at", "2015-10-06 06:41:45.076957"]]
|
134
134
|
[1m[35m (0.0ms)[0m RELEASE SAVEPOINT active_record_1
|
135
135
|
[1m[36m (0.3ms)[0m [1mrollback transaction[0m
|
136
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.4ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
137
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.4ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
138
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.4ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
139
|
+
[1m[35m (0.1ms)[0m begin transaction
|
140
|
+
---------------------------------------------------------
|
141
|
+
ActiveRecordDateFormattedTest: test_date_formatted_getter
|
142
|
+
---------------------------------------------------------
|
143
|
+
[1m[36m (0.0ms)[0m [1mSAVEPOINT active_record_1[0m
|
144
|
+
[1m[35mSQL (0.9ms)[0m INSERT INTO "people" ("birth_date", "created_at", "updated_at") VALUES (?, ?, ?) [["birth_date", "1971-02-18"], ["created_at", "2016-06-03 23:25:17.633335"], ["updated_at", "2016-06-03 23:25:17.633335"]]
|
145
|
+
[1m[36m (0.0ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
146
|
+
[1m[35mPerson Load (0.1ms)[0m SELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1
|
147
|
+
[1m[36mPerson Load (0.0ms)[0m [1mSELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1[0m
|
148
|
+
[1m[35m (0.6ms)[0m rollback transaction
|
149
|
+
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
150
|
+
--------------------------------------------------
|
151
|
+
ActiveRecordDateFormattedTest: test_module_defined
|
152
|
+
--------------------------------------------------
|
153
|
+
[1m[35m (0.0ms)[0m SAVEPOINT active_record_1
|
154
|
+
[1m[36mSQL (0.2ms)[0m [1mINSERT INTO "people" ("birth_date", "created_at", "updated_at") VALUES (?, ?, ?)[0m [["birth_date", "1971-02-18"], ["created_at", "2016-06-03 23:25:17.639815"], ["updated_at", "2016-06-03 23:25:17.639815"]]
|
155
|
+
[1m[35m (0.0ms)[0m RELEASE SAVEPOINT active_record_1
|
156
|
+
[1m[36m (0.4ms)[0m [1mrollback transaction[0m
|
157
|
+
[1m[35m (0.0ms)[0m begin transaction
|
158
|
+
----------------------------------------------------------------
|
159
|
+
ActiveRecordDateFormattedTest: test_date_formatted_setter_german
|
160
|
+
----------------------------------------------------------------
|
161
|
+
[1m[36m (0.0ms)[0m [1mSAVEPOINT active_record_1[0m
|
162
|
+
[1m[35mSQL (0.1ms)[0m INSERT INTO "people" ("birth_date", "created_at", "updated_at") VALUES (?, ?, ?) [["birth_date", "1971-02-18"], ["created_at", "2016-06-03 23:25:17.641790"], ["updated_at", "2016-06-03 23:25:17.641790"]]
|
163
|
+
[1m[36m (0.0ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
164
|
+
[1m[35mPerson Load (0.1ms)[0m SELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1
|
165
|
+
[1m[36m (0.0ms)[0m [1mSAVEPOINT active_record_1[0m
|
166
|
+
[1m[35mSQL (0.8ms)[0m UPDATE "people" SET "birth_date" = ?, "updated_at" = ? WHERE "people"."id" = ? [["birth_date", "1971-02-24"], ["updated_at", "2016-06-03 23:25:17.642953"], ["id", 1]]
|
167
|
+
[1m[36m (0.1ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
168
|
+
[1m[35mPerson Load (0.1ms)[0m SELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1
|
169
|
+
[1m[36mPerson Load (0.0ms)[0m [1mSELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1[0m
|
170
|
+
[1m[35m (0.6ms)[0m rollback transaction
|
171
|
+
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
|
172
|
+
----------------------------------------------------------------
|
173
|
+
ActiveRecordDateFormattedTest: test_date_formatted_getter_german
|
174
|
+
----------------------------------------------------------------
|
175
|
+
[1m[35m (0.0ms)[0m SAVEPOINT active_record_1
|
176
|
+
[1m[36mSQL (0.1ms)[0m [1mINSERT INTO "people" ("birth_date", "created_at", "updated_at") VALUES (?, ?, ?)[0m [["birth_date", "1971-02-18"], ["created_at", "2016-06-03 23:25:17.649420"], ["updated_at", "2016-06-03 23:25:17.649420"]]
|
177
|
+
[1m[35m (0.0ms)[0m RELEASE SAVEPOINT active_record_1
|
178
|
+
[1m[36mPerson Load (0.0ms)[0m [1mSELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1[0m
|
179
|
+
[1m[35mPerson Load (0.0ms)[0m SELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1
|
180
|
+
[1m[36m (0.3ms)[0m [1mrollback transaction[0m
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_date_formatted
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Jancev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -63,6 +63,7 @@ files:
|
|
63
63
|
- README.md
|
64
64
|
- Rakefile
|
65
65
|
- lib/active_record_date_formatted.rb
|
66
|
+
- lib/active_record_date_formatted/date_format_validator.rb
|
66
67
|
- lib/active_record_date_formatted/model.rb
|
67
68
|
- lib/active_record_date_formatted/version.rb
|
68
69
|
- test/active_record_date_formatted_test.rb
|
@@ -178,4 +179,3 @@ test_files:
|
|
178
179
|
- test/dummy/test/fixtures/people.yml
|
179
180
|
- test/dummy/test/models/person_test.rb
|
180
181
|
- test/test_helper.rb
|
181
|
-
has_rdoc:
|