flexible_date 0.1.1 → 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.
- data/README.rdoc +37 -7
- data/lib/flexible_date.rb +32 -9
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -4,19 +4,49 @@ Make ActiveRecord understand any date format you want.
|
|
4
4
|
|
5
5
|
== How to use
|
6
6
|
|
7
|
-
|
7
|
+
Within your model, you should do:
|
8
8
|
|
9
|
-
flexible_date :my_date_field
|
9
|
+
flexible_date :my_date_field
|
10
10
|
|
11
|
-
|
11
|
+
|
12
|
+
And in config/locales/br.yml (or your native language):
|
13
|
+
|
14
|
+
br:
|
15
|
+
flexible_date:
|
16
|
+
configuration:
|
17
|
+
format: "%d/%m/%Y"
|
18
|
+
messages:
|
19
|
+
with_suffix:
|
20
|
+
invalid: "inválida."
|
21
|
+
empty: "não pode ser vazia."
|
22
|
+
without_suffix:
|
23
|
+
invalid: "inválida."
|
24
|
+
empty: "não pode ser vazia."
|
25
|
+
|
26
|
+
|
27
|
+
Or, in a reduced form, when messages for with or without suffix are the same:
|
28
|
+
|
29
|
+
br:
|
30
|
+
flexible_date:
|
31
|
+
configuration:
|
32
|
+
format: "%d/%m/%Y"
|
33
|
+
messages:
|
34
|
+
invalid: "inválida."
|
35
|
+
empty: "não pode ser vazia."
|
36
|
+
|
37
|
+
|
38
|
+
Date format follows the same pattern used by strptime and strftime. This will
|
12
39
|
generate a my_date_field_flex "property" in which you can enter the date as
|
13
40
|
string in the specified format and then the correct date will be stored in
|
14
41
|
my_date_field.
|
15
42
|
|
16
|
-
|
43
|
+
When an error occurs, the "with_suffix" messages are generated for the "flex"
|
44
|
+
fields and "without_suffix" messages are generated for the regular ones.
|
45
|
+
|
46
|
+
Talk is cheap, let's see the code (given any of the locale files above):
|
17
47
|
|
18
48
|
class Event < ActiveRecord::Base
|
19
|
-
flexible_date :start_date, :end_date
|
49
|
+
flexible_date :start_date, :end_date
|
20
50
|
end
|
21
51
|
|
22
52
|
event = Event.new
|
@@ -38,10 +68,10 @@ specified format:
|
|
38
68
|
Suffix can be customized:
|
39
69
|
|
40
70
|
class Event < ActiveRecord::Base
|
41
|
-
flexible_date :judgement_day, :
|
71
|
+
flexible_date :judgement_day, :suffix => 'yyz'
|
42
72
|
|
43
73
|
event = Event.new
|
44
|
-
event.judgement_day_yyz = "28
|
74
|
+
event.judgement_day_yyz = "28/02/2011"
|
45
75
|
event.judgement_day.should == Date.new(2011, 02, 28)
|
46
76
|
|
47
77
|
|
data/lib/flexible_date.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
1
2
|
module FlexibleDate
|
2
3
|
def flexible_date(*params)
|
3
|
-
options, fields = params.pop, params
|
4
|
-
format = options[:format]
|
4
|
+
params.last.kind_of?(Hash) ? (options, fields = params.pop, params) : (options, fields = {}, params)
|
5
5
|
suffix = options[:suffix] || "flex"
|
6
6
|
fields.each do |field|
|
7
7
|
unless methods.include?(:flexible_date_validations)
|
@@ -17,18 +17,41 @@ module FlexibleDate
|
|
17
17
|
end
|
18
18
|
|
19
19
|
define_method "#{field}_#{suffix}" do
|
20
|
+
format = I18n.t("flexible_date.configuration.format")
|
20
21
|
date = self.send("#{field}")
|
21
22
|
date.try(:strftime, format)
|
22
23
|
end
|
23
24
|
|
24
25
|
define_method "#{field}_#{suffix}=" do |value|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
26
|
+
try_t = lambda do |option1, option2|
|
27
|
+
begin
|
28
|
+
I18n.t option1, :raise => true
|
29
|
+
rescue I18n::MissingTranslationData
|
30
|
+
I18n.t option2
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
@flexible_date_errors ||= {}
|
35
|
+
if value.blank?
|
36
|
+
@flexible_date_errors["#{field}".to_sym] = try_t.call(
|
37
|
+
"flexible_date.messages.without_suffix.empty",
|
38
|
+
"flexible_date.messages.empty")
|
39
|
+
@flexible_date_errors["#{field}_#{suffix}".to_sym] = try_t.call(
|
40
|
+
"flexible_date.messages.with_suffix.empty",
|
41
|
+
"flexible_date.messages.empty")
|
42
|
+
else
|
43
|
+
begin
|
44
|
+
format = I18n.t("flexible_date.configuration.format")
|
45
|
+
self.send("#{field}=", Date.strptime(value, format))
|
46
|
+
rescue ArgumentError
|
47
|
+
self.send("#{field}=", nil)
|
48
|
+
@flexible_date_errors["#{field}".to_sym] = try_t.call(
|
49
|
+
"flexible_date.messages.without_suffix.invalid",
|
50
|
+
"flexible_date.messages.invalid")
|
51
|
+
@flexible_date_errors["#{field}_#{suffix}".to_sym] = try_t.call(
|
52
|
+
"flexible_date.messages.with_suffix.invalid",
|
53
|
+
"flexible_date.messages.invalid")
|
54
|
+
end
|
32
55
|
end
|
33
56
|
end
|
34
57
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: flexible_date
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Rodrigo Manh\xC3\xA3es"
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-05-21 00:00:00 -03:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
requirements:
|
44
44
|
- - ~>
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: 2.
|
46
|
+
version: 2.6.0
|
47
47
|
type: :development
|
48
48
|
version_requirements: *id003
|
49
49
|
description: Make ActiveRecord understand any date format you want.
|