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: 2f776eb64d262844143f9ba7ee7b132604e592ec
4
- data.tar.gz: 71700d89eaebc4e58d6b1cc1ce1a88e1702eeec0
3
+ metadata.gz: f991af11095bec503a41b9fa2a4878cf71d1f2cc
4
+ data.tar.gz: 3d89165fb84272b49bfcaed7a274451a284bf35b
5
5
  SHA512:
6
- metadata.gz: 6daa4b115c4e002c35eda27b760df4efdcc7d16185d385d50e7362670f4795388d6ae686f14b0fbce7bb7bae6ba2d354adc45a1015ee7019ff616699ff606d4c
7
- data.tar.gz: a8e5bacd6f3d10819b294af4d351516679c975417679357e40aa058408cacc28931de264f98d30dd12f35109710afaf05e6ca41633f22a6d0e1ea522aa2bc1d5
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
- date_value = read_attribute(attr_name)
16
- date_value.nil? ? nil : date_value.strftime(I18n.t "date.formats.default")
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}_formatted=" do |date_formatted|
20
- write_attribute(attr_name, date_formatted.blank? ? nil : Date.strptime(date_formatted, I18n.t("date.formats.default")))
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
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordDateFormatted
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -133,3 +133,48 @@ ActiveRecordDateFormattedTest: test_module_defined
133
133
  SQL (0.1ms) INSERT INTO "people" ("birth_date", "created_at", "updated_at") VALUES (?, ?, ?) [["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
   (0.0ms) RELEASE SAVEPOINT active_record_1
135
135
   (0.3ms) rollback transaction
136
+ ActiveRecord::SchemaMigration Load (0.4ms) SELECT "schema_migrations".* FROM "schema_migrations"
137
+ ActiveRecord::SchemaMigration Load (0.4ms) SELECT "schema_migrations".* FROM "schema_migrations"
138
+ ActiveRecord::SchemaMigration Load (0.4ms) SELECT "schema_migrations".* FROM "schema_migrations"
139
+  (0.1ms) begin transaction
140
+ ---------------------------------------------------------
141
+ ActiveRecordDateFormattedTest: test_date_formatted_getter
142
+ ---------------------------------------------------------
143
+  (0.0ms) SAVEPOINT active_record_1
144
+ SQL (0.9ms) 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
+  (0.0ms) RELEASE SAVEPOINT active_record_1
146
+ Person Load (0.1ms) SELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1
147
+ Person Load (0.0ms) SELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1
148
+  (0.6ms) rollback transaction
149
+  (0.0ms) begin transaction
150
+ --------------------------------------------------
151
+ ActiveRecordDateFormattedTest: test_module_defined
152
+ --------------------------------------------------
153
+  (0.0ms) SAVEPOINT active_record_1
154
+ SQL (0.2ms) INSERT INTO "people" ("birth_date", "created_at", "updated_at") VALUES (?, ?, ?) [["birth_date", "1971-02-18"], ["created_at", "2016-06-03 23:25:17.639815"], ["updated_at", "2016-06-03 23:25:17.639815"]]
155
+  (0.0ms) RELEASE SAVEPOINT active_record_1
156
+  (0.4ms) rollback transaction
157
+  (0.0ms) begin transaction
158
+ ----------------------------------------------------------------
159
+ ActiveRecordDateFormattedTest: test_date_formatted_setter_german
160
+ ----------------------------------------------------------------
161
+  (0.0ms) SAVEPOINT active_record_1
162
+ SQL (0.1ms) 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
+  (0.0ms) RELEASE SAVEPOINT active_record_1
164
+ Person Load (0.1ms) SELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1
165
+  (0.0ms) SAVEPOINT active_record_1
166
+ SQL (0.8ms) 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
+  (0.1ms) RELEASE SAVEPOINT active_record_1
168
+ Person Load (0.1ms) SELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1
169
+ Person Load (0.0ms) SELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1
170
+  (0.6ms) rollback transaction
171
+  (0.0ms) begin transaction
172
+ ----------------------------------------------------------------
173
+ ActiveRecordDateFormattedTest: test_date_formatted_getter_german
174
+ ----------------------------------------------------------------
175
+  (0.0ms) SAVEPOINT active_record_1
176
+ SQL (0.1ms) INSERT INTO "people" ("birth_date", "created_at", "updated_at") VALUES (?, ?, ?) [["birth_date", "1971-02-18"], ["created_at", "2016-06-03 23:25:17.649420"], ["updated_at", "2016-06-03 23:25:17.649420"]]
177
+  (0.0ms) RELEASE SAVEPOINT active_record_1
178
+ Person Load (0.0ms) SELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1
179
+ Person Load (0.0ms) SELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1
180
+  (0.3ms) rollback transaction
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.7
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: 2015-10-06 00:00:00.000000000 Z
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: