foolabs-delocalize 0.2.1
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/MIT-LICENSE +20 -0
- data/README +117 -0
- data/Rakefile +10 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/delocalize.rb +4 -0
- data/lib/delocalize/i18n_ext.rb +28 -0
- data/lib/delocalize/localized_date_time_parser.rb +101 -0
- data/lib/delocalize/localized_numeric_parser.rb +25 -0
- data/lib/delocalize/rails_ext.rb +4 -0
- data/lib/delocalize/rails_ext/action_view.rb +50 -0
- data/lib/delocalize/rails_ext/active_record.rb +55 -0
- data/lib/delocalize/rails_ext/time_zone.rb +7 -0
- data/lib/delocalize/rails_ext/validation.rb +91 -0
- data/lib/delocalize/ruby_ext.rb +4 -0
- data/lib/delocalize/ruby_ext/date.rb +9 -0
- data/lib/delocalize/ruby_ext/datetime.rb +9 -0
- data/lib/delocalize/ruby_ext/numeric.rb +9 -0
- data/lib/delocalize/ruby_ext/time.rb +9 -0
- data/tasks/distribution.rb +21 -0
- data/tasks/documentation.rb +8 -0
- data/tasks/testing.rb +7 -0
- data/test/delocalize_test.rb +257 -0
- data/test/rails_app/app/controllers/application_controller.rb +10 -0
- data/test/rails_app/config/boot.rb +110 -0
- data/test/rails_app/config/environment.rb +17 -0
- data/test/rails_app/config/environments/test.rb +28 -0
- data/test/rails_app/config/initializers/new_rails_defaults.rb +21 -0
- data/test/rails_app/config/initializers/session_store.rb +15 -0
- data/test/rails_app/config/routes.rb +4 -0
- data/test/test_helper.rb +78 -0
- metadata +96 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Clemens Kofler
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
delocalize
|
2
|
+
==========
|
3
|
+
|
4
|
+
delocalize provides localized date/time and number parsing functionality for Rails.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
============
|
8
|
+
|
9
|
+
You can use delocalize either as a gem (preferred) or as a Rails plugin.
|
10
|
+
|
11
|
+
To use the gem version, put the following gem requirement in your environment.rb:
|
12
|
+
|
13
|
+
config.gem "delocalize", :source => 'http://gemcutter.org'
|
14
|
+
|
15
|
+
To install it as a plugin, fire up your terminal, go to your Rails app and type:
|
16
|
+
|
17
|
+
$ ruby script/plugin install git://github.com/clemens/delocalize.git
|
18
|
+
|
19
|
+
|
20
|
+
What does it do? And how do I use it?
|
21
|
+
=====================================
|
22
|
+
|
23
|
+
Delocalize, just as the name suggest, does pretty much the opposite of localize.
|
24
|
+
|
25
|
+
In the grey past, ff you want your users to be able to input localized data, such as dates and numbers, you had to manually override attribute accessors:
|
26
|
+
|
27
|
+
def price=(price)
|
28
|
+
write_attribute(:price, price.gsub(',', '.'))
|
29
|
+
end
|
30
|
+
|
31
|
+
delocalize does this under the covers - all you need is your regular translation data (as YAML or Ruby file) where you need Rails' standard translations:
|
32
|
+
|
33
|
+
de:
|
34
|
+
number:
|
35
|
+
format:
|
36
|
+
separator: ','
|
37
|
+
delimiter: '.'
|
38
|
+
date:
|
39
|
+
input:
|
40
|
+
formats: [:default, :long, :short] # <- this and ...
|
41
|
+
|
42
|
+
formats:
|
43
|
+
default: "%d.%m.%Y"
|
44
|
+
short: "%e. %b"
|
45
|
+
long: "%e. %B %Y"
|
46
|
+
only_day: "%e"
|
47
|
+
|
48
|
+
day_names: [Sonntag, Montag, Dienstag, Mittwoch, Donnerstag, Freitag, Samstag]
|
49
|
+
abbr_day_names: [So, Mo, Di, Mi, Do, Fr, Sa]
|
50
|
+
month_names: [~, Januar, Februar, März, April, Mai, Juni, Juli, August, September, Oktober, November, Dezember]
|
51
|
+
abbr_month_names: [~, Jan, Feb, Mär, Apr, Mai, Jun, Jul, Aug, Sep, Okt, Nov, Dez]
|
52
|
+
order: [ :day, :month, :year ]
|
53
|
+
|
54
|
+
time:
|
55
|
+
input:
|
56
|
+
formats: [:long, :medium, :short, :default, :time] # <- ... this are the only non-standard keys
|
57
|
+
formats:
|
58
|
+
default: "%A, %e. %B %Y, %H:%M Uhr"
|
59
|
+
short: "%e. %B, %H:%M Uhr"
|
60
|
+
long: "%A, %e. %B %Y, %H:%M Uhr"
|
61
|
+
time: "%H:%M"
|
62
|
+
|
63
|
+
am: "vormittags"
|
64
|
+
pm: "nachmittags"
|
65
|
+
|
66
|
+
For dates and times, you have to define input formats which are taken from the actual formats. The important thing here is to define input formats sorted by descending complexity - in other words: The format which contains the most (preferably non-numeric) information should be first in the list because it can produce the most reliable match. Exception: If you think there most complex format is not the one that most users will input, you can put the most-used in front so you save unnecessary iterations.
|
67
|
+
|
68
|
+
Careful with formats containing only numbers: It's very hard to produce reliable matches if you provide multiple strictly numeric formats!
|
69
|
+
|
70
|
+
delocalize then overrides to_input_field_tag in ActionView's InstanceTag so you can use localized text fields:
|
71
|
+
|
72
|
+
<% form_for @product do |f| %>
|
73
|
+
<%= f.text_field :name %>
|
74
|
+
<%= f.text_field :released_on %>
|
75
|
+
<%= f.text_field :price %>
|
76
|
+
<% end %>
|
77
|
+
|
78
|
+
In this example, a user can enter the release date and the price just like he's used to in his language, for example:
|
79
|
+
|
80
|
+
Name: "Couch"
|
81
|
+
Released on: "12. Oktober 2009"
|
82
|
+
Price: "2.999,90"
|
83
|
+
|
84
|
+
When saved, ActiveRecord automatically converts these to a regular Ruby date and number.
|
85
|
+
|
86
|
+
Edit forms then also show localized dates/numbers. By default, dates and times are localized using the format named :default in your locale file. So with the above locale file, dates would use "%d.%m.%Y" and times would use "%A, %e. %B %Y, %H:%M Uhr". Numbers are also formatted using your locale's thousands delimiter and decimal separator.
|
87
|
+
|
88
|
+
You can also customize the output using some options:
|
89
|
+
|
90
|
+
The price should always show two decimal digits and we don't need the delimiter:
|
91
|
+
<%= f.text_field :price, :precision => 2, :delimiter => '' %>
|
92
|
+
|
93
|
+
The released_on date should be shown in the :full format:
|
94
|
+
<%= f.text_field :released_on, :format => :full %>
|
95
|
+
|
96
|
+
Since I18n.localize supportes localizing strftime strings, we can also do this:
|
97
|
+
<%= f.text_field :released_on, :format => "%B %Y" %>
|
98
|
+
|
99
|
+
Note
|
100
|
+
====
|
101
|
+
|
102
|
+
delocalize is most definitely not enterprise-ready! ;-)
|
103
|
+
Or as Yaroslav says: Contains small pieces. Not good for children of age 3 and less. Not enterprise-ready.
|
104
|
+
|
105
|
+
TODO
|
106
|
+
====
|
107
|
+
|
108
|
+
* Improve test coverage
|
109
|
+
* Separate Ruby/Rails stuff to make it usable outside Rails
|
110
|
+
* Verify correct behavior with time zones
|
111
|
+
* Decide on other ActionView hacks (e.g. text_field_tag)
|
112
|
+
* Implement AM/PM support
|
113
|
+
* Cleanup, cleanup, cleanup ...
|
114
|
+
|
115
|
+
Copyright (c) 2009 Clemens Kofler <clemens@railway.at>
|
116
|
+
www.railway.at
|
117
|
+
Released under the MIT license
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.1
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'delocalize'
|
data/lib/delocalize.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module I18n
|
2
|
+
mattr_accessor :enable_delocalization
|
3
|
+
I18n.enable_delocalization = true
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def delocalization_enabled?
|
7
|
+
!!I18n.enable_delocalization
|
8
|
+
end
|
9
|
+
|
10
|
+
def delocalization_disabled?
|
11
|
+
!delocalization_enabled?
|
12
|
+
end
|
13
|
+
|
14
|
+
def with_delocalization_disabled(&block)
|
15
|
+
old_value = I18n.enable_delocalization
|
16
|
+
I18n.enable_delocalization = false
|
17
|
+
yield
|
18
|
+
I18n.enable_delocalization = old_value
|
19
|
+
end
|
20
|
+
|
21
|
+
def with_delocalization_enabled(&block)
|
22
|
+
old_value = I18n.enable_delocalization
|
23
|
+
I18n.enable_delocalization = true
|
24
|
+
yield
|
25
|
+
I18n.enable_delocalization = old_value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# TODO:
|
2
|
+
# * AM/PM calculation
|
3
|
+
# * proper documentation (comments)
|
4
|
+
module Delocalize
|
5
|
+
class LocalizedDateTimeParser
|
6
|
+
class << self
|
7
|
+
def parse(raw_datetime, type)
|
8
|
+
return unless raw_datetime
|
9
|
+
return raw_datetime if raw_datetime.respond_to?(:strftime) # already a Date/Time object -> no need to parse it
|
10
|
+
|
11
|
+
input_formats(type).each do |original_format, regex_format|
|
12
|
+
next unless raw_datetime =~ /^#{regex_format}$/
|
13
|
+
|
14
|
+
begin
|
15
|
+
translated = translate_month_and_day_names(raw_datetime)
|
16
|
+
datetime = DateTime.strptime(translated, original_format)
|
17
|
+
rescue ArgumentError => e
|
18
|
+
return default_parse(raw_datetime, type)
|
19
|
+
end
|
20
|
+
|
21
|
+
if Date == type
|
22
|
+
return datetime.to_date
|
23
|
+
else
|
24
|
+
return Time.local(
|
25
|
+
datetime.year, datetime.mon, datetime.mday,
|
26
|
+
datetime.hour, datetime.min, datetime.sec
|
27
|
+
)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
default_parse(raw_datetime, type)
|
32
|
+
end
|
33
|
+
|
34
|
+
def valid_format?(raw_datetime, type)
|
35
|
+
return false unless raw_datetime
|
36
|
+
return true if raw_datetime.respond_to?(:strftime) # already a Date/Time object -> no need to parse it
|
37
|
+
|
38
|
+
return input_formats(type).any? do |original_format, regex_format|
|
39
|
+
begin
|
40
|
+
next unless raw_datetime =~ /^#{regex_format}$/
|
41
|
+
translated = translate_month_and_day_names(raw_datetime)
|
42
|
+
|
43
|
+
DateTime.strptime(translated, original_format)
|
44
|
+
rescue ArgumentError => e
|
45
|
+
next
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
def default_parse(datetime, type)
|
52
|
+
return if datetime.blank?
|
53
|
+
begin
|
54
|
+
today = Date.current
|
55
|
+
parsed = Date._parse(datetime)
|
56
|
+
return if parsed.empty? # the datetime value is invalid
|
57
|
+
# set default year, month and day if not found
|
58
|
+
parsed.reverse_merge!(:year => today.year, :mon => today.mon, :mday => today.mday)
|
59
|
+
datetime = Time.local(*parsed.values_at(:year, :mon, :mday, :hour, :min, :sec))
|
60
|
+
Date == type ? datetime.to_date : datetime
|
61
|
+
rescue
|
62
|
+
datetime
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def input_formats(type)
|
67
|
+
# Date uses date formats, all others use time formats
|
68
|
+
type = (type == Date) ? :date : :time
|
69
|
+
@input_formats ||= {}
|
70
|
+
@input_formats[I18n.locale] ||= {}
|
71
|
+
@input_formats[I18n.locale][type] ||= I18n.t(:"#{type}.formats").values.compact.map { |f| [f, apply_regex(f)] }
|
72
|
+
end
|
73
|
+
|
74
|
+
def translate_month_and_day_names(datetime)
|
75
|
+
translated = I18n.t([:month_names, :abbr_month_names, :day_names, :abbr_day_names], :scope => :date).flatten.compact
|
76
|
+
original = (Date::MONTHNAMES + Date::ABBR_MONTHNAMES + Date::DAYNAMES + Date::ABBR_DAYNAMES).compact
|
77
|
+
word_map = {}
|
78
|
+
translated.each_with_index{ |k,i| word_map[k] = original[i] }
|
79
|
+
datetime.gsub(/[^_\W]+/u){|x| word_map[x] || x}
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
def apply_regex(format)
|
84
|
+
format.gsub('%B', "(#{I18n.t('date.month_names').compact.join('|')})"). # long month name
|
85
|
+
gsub('%b', "(#{I18n.t('date.abbr_month_names').compact.join('|')})"). # short month name
|
86
|
+
gsub('%m', "(\\d{1,2})"). # numeric month
|
87
|
+
gsub('%A', "(#{I18n.t('date.day_names').compact.join('|')})"). # full day name
|
88
|
+
gsub('%a', "(#{I18n.t('date.abbr_day_names').compact.join('|')})"). # short day name
|
89
|
+
gsub('%Y', "(\\d{4})"). # long year
|
90
|
+
gsub('%y', "(\\d{2})"). # short year
|
91
|
+
gsub('%e', "(\\w?\\d{1,2})"). # short day
|
92
|
+
gsub('%d', "(\\d{1,2})"). # full day
|
93
|
+
gsub('%H', "(\\d{1,2})"). # hour (24)
|
94
|
+
gsub('%d', "(\\d{1,2})"). # full day
|
95
|
+
gsub('%H', "(\\d{1,2})"). # hour (24)
|
96
|
+
gsub('%M', "(\\d{1,2})"). # minute
|
97
|
+
gsub('%S', "(\\d{1,2})") # second
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# TODO:
|
2
|
+
# * proper documentation (comments)
|
3
|
+
module Delocalize
|
4
|
+
class LocalizedNumericParser
|
5
|
+
class << self
|
6
|
+
# Parse numbers replacing locale specific delimeters and separators with
|
7
|
+
# standard ruby _ and .
|
8
|
+
def parse(value)
|
9
|
+
if value == false
|
10
|
+
0
|
11
|
+
elsif value == true
|
12
|
+
1
|
13
|
+
elsif value.is_a?(String) && value.blank?
|
14
|
+
nil
|
15
|
+
elsif value.is_a?(String)
|
16
|
+
separator = I18n.t(:'number.format.separator')
|
17
|
+
delimeter = I18n.t(:'number.format.delimiter')
|
18
|
+
value.strip.tr("#{separator}#{delimeter}", "._")
|
19
|
+
else
|
20
|
+
value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# TODO: also override other methods like to_check_box_tag since they might contain numeric values?
|
2
|
+
# ActionView needs some patching too
|
3
|
+
|
4
|
+
ActionView::Helpers::InstanceTag.class_eval do
|
5
|
+
include ActionView::Helpers::NumberHelper
|
6
|
+
|
7
|
+
alias original_to_input_field_tag to_input_field_tag
|
8
|
+
def to_input_field_tag(field_type, options = {})
|
9
|
+
options.symbolize_keys!
|
10
|
+
# numbers and dates/times should be localized unless value is already defined
|
11
|
+
if object && options[:value].blank? && object.respond_to?(:column_for_attribute) && column = object.column_for_attribute(method_name)
|
12
|
+
# a little verbose
|
13
|
+
if column.number? || column.date? || column.time?
|
14
|
+
value = object.send(method_name)
|
15
|
+
|
16
|
+
if column.number?
|
17
|
+
# formats the number only if it has errors
|
18
|
+
if object.respond_to?(:errors) && !object.errors.invalid?(method_name)
|
19
|
+
number_options = I18n.t(:'number.format')
|
20
|
+
separator = options.delete(:separator) || number_options[:separator]
|
21
|
+
delimiter = options.delete(:delimiter) || number_options[:delimiter]
|
22
|
+
precision = options.delete(:precision) || number_options[:precision]
|
23
|
+
opts = { :separator => separator, :delimiter => delimiter, :precision => precision }
|
24
|
+
# integers don't need a precision
|
25
|
+
opts.merge!(:precision => 0) if column.type == :integer
|
26
|
+
options[:value] = number_with_precision(value, opts)
|
27
|
+
end
|
28
|
+
elsif column.date? || column.time?
|
29
|
+
options[:value] = value ? I18n.l(value, :format => options.delete(:format)) : nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
original_to_input_field_tag(field_type, options)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# TODO: does it make sense to also override FormTagHelper methods?
|
39
|
+
# ActionView::Helpers::FormTagHelper.class_eval do
|
40
|
+
# include ActionView::Helpers::NumberHelper
|
41
|
+
#
|
42
|
+
# alias original_text_field_tag text_field_tag
|
43
|
+
# def text_field_tag(name, value = nil, options = {})
|
44
|
+
# value = options.delete(:value) if options.key?(:value)
|
45
|
+
# if value.is_a?(Numeric)
|
46
|
+
# value = number_with_delimiter(value)
|
47
|
+
# end
|
48
|
+
# original_text_field_tag(name, value, options)
|
49
|
+
# end
|
50
|
+
# end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# let's hack into ActiveRecord a bit - everything at the lowest possible level,
|
2
|
+
# of course, so we minimalize side effects
|
3
|
+
ActiveRecord::ConnectionAdapters::Column.class_eval do
|
4
|
+
def date?
|
5
|
+
klass == Date
|
6
|
+
end
|
7
|
+
|
8
|
+
def time?
|
9
|
+
klass == Time
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveRecord::Base.class_eval do
|
14
|
+
def convert_number_column_value_with_localization(value)
|
15
|
+
if I18n.delocalization_enabled?
|
16
|
+
Numeric.parse_localized(value)
|
17
|
+
else
|
18
|
+
convert_number_column_value_without_localization(value)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
alias_method_chain :convert_number_column_value, :localization
|
22
|
+
end
|
23
|
+
|
24
|
+
ActiveRecord::Dirty.module_eval do
|
25
|
+
# overriding to convert numbers with localization
|
26
|
+
# this method belongs to Dirty module
|
27
|
+
def field_changed?(attr, old, value)
|
28
|
+
if column = column_for_attribute(attr)
|
29
|
+
if column.number?
|
30
|
+
if column.null && (old.nil? || old == 0) && value.blank?
|
31
|
+
# For nullable numeric columns, NULL gets stored in database for blank (i.e. '') values.
|
32
|
+
# Hence we don't record it as a change if the value changes from nil to ''.
|
33
|
+
# If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
|
34
|
+
# be typecast back to 0 (''.to_i => 0)
|
35
|
+
value = nil
|
36
|
+
else
|
37
|
+
value = Numeric.parse_localized(value)
|
38
|
+
end
|
39
|
+
else
|
40
|
+
value = column.type_cast(value)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
old != value
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
column_class = ActiveRecord::ConnectionAdapters::Column
|
49
|
+
def column_class.string_to_date(value)
|
50
|
+
Date.parse_localized(value)
|
51
|
+
end
|
52
|
+
|
53
|
+
def column_class.string_to_time(value)
|
54
|
+
Time.parse_localized(value)
|
55
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module ActiveRecord::Validations::ClassMethods
|
2
|
+
# Validates whether the value of the specified attribute is numeric by trying to convert it to
|
3
|
+
# a float with Kernel.Float (if <tt>only_integer</tt> is false) or applying it to the regular expression
|
4
|
+
# <tt>/\A[\+\-]?\d+\Z/</tt> (if <tt>only_integer</tt> is set to true).
|
5
|
+
#
|
6
|
+
# class Person < ActiveRecord::Base
|
7
|
+
# validates_numericality_of :value, :on => :create
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# Configuration options:
|
11
|
+
# * <tt>:message</tt> - A custom error message (default is: "is not a number").
|
12
|
+
# * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).
|
13
|
+
# * <tt>:only_integer</tt> - Specifies whether the value has to be an integer, e.g. an integral value (default is +false+).
|
14
|
+
# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+ (default is +false+). Notice that for fixnum and float columns empty strings are converted to +nil+.
|
15
|
+
# * <tt>:greater_than</tt> - Specifies the value must be greater than the supplied value.
|
16
|
+
# * <tt>:greater_than_or_equal_to</tt> - Specifies the value must be greater than or equal the supplied value.
|
17
|
+
# * <tt>:equal_to</tt> - Specifies the value must be equal to the supplied value.
|
18
|
+
# * <tt>:less_than</tt> - Specifies the value must be less than the supplied value.
|
19
|
+
# * <tt>:less_than_or_equal_to</tt> - Specifies the value must be less than or equal the supplied value.
|
20
|
+
# * <tt>:odd</tt> - Specifies the value must be an odd number.
|
21
|
+
# * <tt>:even</tt> - Specifies the value must be an even number.
|
22
|
+
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
|
23
|
+
# occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
|
24
|
+
# method, proc or string should return or evaluate to a true or false value.
|
25
|
+
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
|
26
|
+
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
|
27
|
+
# method, proc or string should return or evaluate to a true or false value.
|
28
|
+
def validates_numericality_of(*attr_names)
|
29
|
+
configuration = { :on => :save, :only_integer => false, :allow_nil => false }
|
30
|
+
configuration.update(attr_names.extract_options!)
|
31
|
+
|
32
|
+
|
33
|
+
numericality_options = ALL_NUMERICALITY_CHECKS.keys & configuration.keys
|
34
|
+
|
35
|
+
(numericality_options - [ :odd, :even ]).each do |option|
|
36
|
+
raise ArgumentError, ":#{option} must be a number" unless configuration[option].is_a?(Numeric)
|
37
|
+
end
|
38
|
+
|
39
|
+
validates_each(attr_names,configuration) do |record, attr_name, value|
|
40
|
+
raw_value = record.send("#{attr_name}_before_type_cast") || value
|
41
|
+
|
42
|
+
next if configuration[:allow_nil] and raw_value.nil?
|
43
|
+
|
44
|
+
unless raw_value.is_a?(Numeric)
|
45
|
+
if configuration[:only_integer]
|
46
|
+
matcher = /^[-+]?\d+([_]\d{3})*$/
|
47
|
+
else
|
48
|
+
matcher = /^[-+]?\d+([_]\d{3})*[.]?\d*$/
|
49
|
+
end
|
50
|
+
|
51
|
+
unless matcher.match(raw_value.to_s.strip)
|
52
|
+
record.errors.add(attr_name, :not_a_number, :value => raw_value, :default => configuration[:message])
|
53
|
+
next
|
54
|
+
end
|
55
|
+
|
56
|
+
raw_value = configuration[:only_integer] ? raw_value.to_i : raw_value.to_f
|
57
|
+
end
|
58
|
+
|
59
|
+
numericality_options.each do |option|
|
60
|
+
case option
|
61
|
+
when :odd, :even
|
62
|
+
unless raw_value.to_i.method(ALL_NUMERICALITY_CHECKS[option])[]
|
63
|
+
record.errors.add(attr_name, option, :value => raw_value, :default => configuration[:message])
|
64
|
+
end
|
65
|
+
else
|
66
|
+
record.errors.add(attr_name, option, :default => configuration[:message], :value => raw_value, :count => configuration[option]) unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def validates_date_format_of(*attr_names)
|
73
|
+
configuration = { :on => :save, :allow_nil => false }
|
74
|
+
configuration.update(attr_names.extract_options!)
|
75
|
+
|
76
|
+
validates_each(attr_names,configuration) do |record, attr_name, value|
|
77
|
+
raw_value = record.send("#{attr_name}_before_type_cast") || value
|
78
|
+
Delocalize::LocalizedDateTimeParser.valid_format?(raw_value, Date)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def validates_time_format_of(*attr_names)
|
83
|
+
configuration = { :on => :save, :allow_nil => false }
|
84
|
+
configuration.update(attr_names.extract_options!)
|
85
|
+
|
86
|
+
validates_each(attr_names,configuration) do |record, attr_name, value|
|
87
|
+
raw_value = record.send("#{attr_name}_before_type_cast") || value
|
88
|
+
Delocalize::LocalizedDateTimeParser.valid_format?(raw_value, Time)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |s|
|
4
|
+
s.name = "foolabs-delocalize"
|
5
|
+
s.summary = "Localized date/time and number parsing"
|
6
|
+
s.email = ["clemens@railway.at", "marcin.raczkowski@gmail.com", "fernandoluizao@gmail.com"]
|
7
|
+
s.homepage = "http://github.com/clemens/delocalize"
|
8
|
+
s.description = "Delocalize is a tool for parsing localized dates/times and numbers."
|
9
|
+
s.authors = ["Clemens Kofler", "Marcin Raczkowski", "Fernando Migliorini Luizão"]
|
10
|
+
s.files = FileList["init.rb",
|
11
|
+
"lib/**/*.rb",
|
12
|
+
"MIT-LICENSE",
|
13
|
+
"Rakefile",
|
14
|
+
"README",
|
15
|
+
"tasks/**/*.rb",
|
16
|
+
"VERSION"]
|
17
|
+
s.test_files = FileList["test/**/*.rb"]
|
18
|
+
end
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
21
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
desc 'Generate documentation for the delocalize plugin.'
|
2
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
3
|
+
rdoc.rdoc_dir = 'rdoc'
|
4
|
+
rdoc.title = 'Delocalize'
|
5
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
6
|
+
rdoc.rdoc_files.include('README')
|
7
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
8
|
+
end
|
data/tasks/testing.rb
ADDED
@@ -0,0 +1,257 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class DelocalizeActiveRecordTest < ActiveRecord::TestCase
|
4
|
+
def setup
|
5
|
+
Time.zone = 'Berlin' # make sure everything works as expected with TimeWithZone
|
6
|
+
@product = Product.new
|
7
|
+
end
|
8
|
+
|
9
|
+
test "delocalizes localized number" do
|
10
|
+
@product.price = '1.299,99'
|
11
|
+
assert_equal 1299.99, @product.price
|
12
|
+
|
13
|
+
@product.price = '-1.299,99'
|
14
|
+
assert_equal -1299.99, @product.price
|
15
|
+
end
|
16
|
+
|
17
|
+
test "delocalizes localized date with year" do
|
18
|
+
date = Date.civil(2009, 3, 19)
|
19
|
+
|
20
|
+
@product.released_on = '19. März 2009'
|
21
|
+
assert_equal date, @product.released_on
|
22
|
+
|
23
|
+
@product.released_on = '19.3.2009'
|
24
|
+
assert_equal date, @product.released_on
|
25
|
+
end
|
26
|
+
|
27
|
+
test "delocalizes localized date without year" do
|
28
|
+
date = Date.civil(Date.today.year, 10, 19)
|
29
|
+
|
30
|
+
@product.released_on = '19. Okt'
|
31
|
+
assert_equal date, @product.released_on
|
32
|
+
end
|
33
|
+
|
34
|
+
test "delocalizes localized datetime with year" do
|
35
|
+
time = Time.zone.local(2009, 3, 1, 12, 0, 0)
|
36
|
+
|
37
|
+
@product.published_at = 'Sonntag, 1. März 2009, 12:00 Uhr'
|
38
|
+
assert_equal time, @product.published_at
|
39
|
+
|
40
|
+
@product.published_at = '1. März 2009, 12:00 Uhr'
|
41
|
+
assert_equal time, @product.published_at
|
42
|
+
end
|
43
|
+
|
44
|
+
test "delocalizes localized datetime without year" do
|
45
|
+
time = Time.zone.local(Date.today.year, 3, 1, 12, 0, 0)
|
46
|
+
|
47
|
+
@product.published_at = '1. März, 12:00 Uhr'
|
48
|
+
assert_equal time, @product.published_at
|
49
|
+
end
|
50
|
+
|
51
|
+
test "delocalizes localized time" do
|
52
|
+
time = Time.zone.local(2000, 1, 1, 9, 0, 0)
|
53
|
+
@product.cant_think_of_a_sensible_time_field = '09:00 Uhr'
|
54
|
+
assert_equal time, @product.cant_think_of_a_sensible_time_field
|
55
|
+
end
|
56
|
+
|
57
|
+
test "uses default parse if format isn't found" do
|
58
|
+
date = Date.civil(2009, 10, 19)
|
59
|
+
|
60
|
+
@product.released_on = '2009/10/19'
|
61
|
+
assert_equal date, @product.released_on
|
62
|
+
|
63
|
+
time = Time.zone.local(2009, 3, 1, 12, 0, 0)
|
64
|
+
@product.published_at = '2009/03/01 12:00'
|
65
|
+
assert_equal time, @product.published_at
|
66
|
+
|
67
|
+
now = Time.current
|
68
|
+
time = Time.zone.local(2000, 1, 1, 9, 0, 0)
|
69
|
+
@product.cant_think_of_a_sensible_time_field = '09:00'
|
70
|
+
assert_equal time, @product.cant_think_of_a_sensible_time_field
|
71
|
+
end
|
72
|
+
|
73
|
+
test "should return nil if the input is empty or invalid" do
|
74
|
+
@product.released_on = ""
|
75
|
+
assert_nil @product.released_on
|
76
|
+
|
77
|
+
@product.released_on = "aa"
|
78
|
+
assert_nil @product.released_on
|
79
|
+
end
|
80
|
+
|
81
|
+
test "doesn't raise when attribute is nil" do
|
82
|
+
assert_nothing_raised {
|
83
|
+
@product.price = nil
|
84
|
+
@product.released_on = nil
|
85
|
+
@product.published_at = nil
|
86
|
+
@product.cant_think_of_a_sensible_time_field = nil
|
87
|
+
}
|
88
|
+
end
|
89
|
+
|
90
|
+
test "uses default formats if enable_delocalization is false" do
|
91
|
+
I18n.enable_delocalization = false
|
92
|
+
|
93
|
+
@product.price = '1299.99'
|
94
|
+
assert_equal 1299.99, @product.price
|
95
|
+
|
96
|
+
@product.price = '-1299.99'
|
97
|
+
assert_equal -1299.99, @product.price
|
98
|
+
end
|
99
|
+
|
100
|
+
test "uses default formats if called with with_delocalization_disabled" do
|
101
|
+
I18n.with_delocalization_disabled do
|
102
|
+
@product.price = '1299.99'
|
103
|
+
assert_equal 1299.99, @product.price
|
104
|
+
|
105
|
+
@product.price = '-1299.99'
|
106
|
+
assert_equal -1299.99, @product.price
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
test "uses localized parsing if called with with_delocalization_enabled" do
|
111
|
+
I18n.with_delocalization_enabled do
|
112
|
+
@product.price = '1.299,99'
|
113
|
+
assert_equal 1299.99, @product.price
|
114
|
+
|
115
|
+
@product.price = '-1.299,99'
|
116
|
+
assert_equal -1299.99, @product.price
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
test "dirty attributes must detect changes in decimal columns" do
|
121
|
+
@product.price = 10
|
122
|
+
@product.save
|
123
|
+
@product.price = "10,34"
|
124
|
+
assert_equal("10.34", @product.price_before_type_cast)
|
125
|
+
assert_equal BigDecimal("10.34"), @product.price
|
126
|
+
assert @product.price_changed?
|
127
|
+
end
|
128
|
+
|
129
|
+
test "dirty attributes must detect changes in float columns" do
|
130
|
+
@product.weight = 10
|
131
|
+
@product.save
|
132
|
+
@product.weight = "10,34"
|
133
|
+
assert_equal 10.34, @product.weight
|
134
|
+
assert @product.weight_changed?
|
135
|
+
end
|
136
|
+
|
137
|
+
test "serialization and deserialization of 'timestamp' should be symetric" do
|
138
|
+
@product.the_timestamp = 1.day.from_now.to_s(:db) # date
|
139
|
+
@product.save
|
140
|
+
@product.reload
|
141
|
+
assert_equal(1.day.from_now.to_i, @product.the_timestamp.to_i)
|
142
|
+
end
|
143
|
+
|
144
|
+
test "before type casting of timestamp" do
|
145
|
+
@product.the_timestamp = 1.day.from_now.to_s(:db) # date
|
146
|
+
assert_equal(1.day.from_now.to_s(:db), @product.the_timestamp_before_type_cast)
|
147
|
+
end
|
148
|
+
|
149
|
+
test "before type casting of date" do
|
150
|
+
@product.released_on = 1.day.from_now.to_s(:db) # date
|
151
|
+
assert_equal(1.day.from_now.to_s(:db), @product.released_on_before_type_cast)
|
152
|
+
assert @product.released_on_before_type_cast.is_a?(String)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
class DelocalizeActionViewTest < ActionView::TestCase
|
157
|
+
include ActionView::Helpers::FormHelper
|
158
|
+
|
159
|
+
def setup
|
160
|
+
Time.zone = 'Berlin' # make sure everything works as expected with TimeWithZone
|
161
|
+
@product = Product.new
|
162
|
+
end
|
163
|
+
|
164
|
+
test "shows text field using formatted number" do
|
165
|
+
@product.price = 1299.9
|
166
|
+
assert_dom_equal '<input id="product_price" name="product[price]" size="30" type="text" value="1.299,90" />',
|
167
|
+
text_field(:product, :price)
|
168
|
+
end
|
169
|
+
|
170
|
+
test "shows text field using formatted number with options" do
|
171
|
+
@product.price = 1299.995
|
172
|
+
assert_dom_equal '<input id="product_price" name="product[price]" size="30" type="text" value="1,299.995" />',
|
173
|
+
text_field(:product, :price, :precision => 3, :delimiter => ',', :separator => '.')
|
174
|
+
end
|
175
|
+
|
176
|
+
test "shows text field using formatted number without precision if column is an integer" do
|
177
|
+
@product.times_sold = 20
|
178
|
+
assert_dom_equal '<input id="product_times_sold" name="product[times_sold]" size="30" type="text" value="20" />',
|
179
|
+
text_field(:product, :times_sold)
|
180
|
+
|
181
|
+
@product.times_sold = 2000
|
182
|
+
assert_dom_equal '<input id="product_times_sold" name="product[times_sold]" size="30" type="text" value="2.000" />',
|
183
|
+
text_field(:product, :times_sold)
|
184
|
+
end
|
185
|
+
|
186
|
+
test "shows text field using formatted date" do
|
187
|
+
@product.released_on = Date.civil(2009, 10, 19)
|
188
|
+
assert_dom_equal '<input id="product_released_on" name="product[released_on]" size="30" type="text" value="19.10.2009" />',
|
189
|
+
text_field(:product, :released_on)
|
190
|
+
end
|
191
|
+
|
192
|
+
test "shows text field using formatted date and time" do
|
193
|
+
@product.published_at = Time.zone.local(2009, 3, 1, 12, 0, 0)
|
194
|
+
# careful - leading whitespace with %e
|
195
|
+
assert_dom_equal '<input id="product_published_at" name="product[published_at]" size="30" type="text" value="Sonntag, 1. März 2009, 12:00 Uhr" />',
|
196
|
+
text_field(:product, :published_at)
|
197
|
+
end
|
198
|
+
|
199
|
+
test "shows text field using formatted date with format" do
|
200
|
+
@product.released_on = Date.civil(2009, 10, 19)
|
201
|
+
assert_dom_equal '<input id="product_released_on" name="product[released_on]" size="30" type="text" value="19. Oktober 2009" />',
|
202
|
+
text_field(:product, :released_on, :format => :long)
|
203
|
+
end
|
204
|
+
|
205
|
+
test "shows text field using formatted date and time with format" do
|
206
|
+
@product.published_at = Time.zone.local(2009, 3, 1, 12, 0, 0)
|
207
|
+
# careful - leading whitespace with %e
|
208
|
+
assert_dom_equal '<input id="product_published_at" name="product[published_at]" size="30" type="text" value=" 1. März, 12:00 Uhr" />',
|
209
|
+
text_field(:product, :published_at, :format => :short)
|
210
|
+
end
|
211
|
+
|
212
|
+
test "shows text field using formatted time with format" do
|
213
|
+
@product.cant_think_of_a_sensible_time_field = Time.zone.local(2009, 3, 1, 9, 0, 0)
|
214
|
+
assert_dom_equal '<input id="product_cant_think_of_a_sensible_time_field" name="product[cant_think_of_a_sensible_time_field]" size="30" type="text" value="09:00 Uhr" />',
|
215
|
+
text_field(:product, :cant_think_of_a_sensible_time_field, :format => :time)
|
216
|
+
end
|
217
|
+
|
218
|
+
test "doesn't raise an exception when object is nil" do
|
219
|
+
assert_nothing_raised {
|
220
|
+
text_field(:not_here, :a_text_field)
|
221
|
+
}
|
222
|
+
end
|
223
|
+
|
224
|
+
test "doesn't raise for nil Date/Time" do
|
225
|
+
@product.published_at, @product.released_on, @product.cant_think_of_a_sensible_time_field = nil
|
226
|
+
assert_nothing_raised {
|
227
|
+
text_field(:product, :published_at)
|
228
|
+
text_field(:product, :released_on)
|
229
|
+
text_field(:product, :cant_think_of_a_sensible_time_field)
|
230
|
+
}
|
231
|
+
end
|
232
|
+
|
233
|
+
test "doesn't override given :value" do
|
234
|
+
@product.price = 1299.9
|
235
|
+
assert_dom_equal '<input id="product_price" name="product[price]" size="30" type="text" value="1.499,90" />',
|
236
|
+
text_field(:product, :price, :value => "1.499,90")
|
237
|
+
end
|
238
|
+
|
239
|
+
test "don't convert type if field has errors" do
|
240
|
+
@product = ProductWithValidation.new(:price => 'this is not a number')
|
241
|
+
@product.valid?
|
242
|
+
assert_dom_equal '<div class="fieldWithErrors"><input id="product_price" name="product[price]" size="30" type="text" value="this is not a number" /></div>',
|
243
|
+
text_field(:product, :price)
|
244
|
+
end
|
245
|
+
|
246
|
+
test "doesn't raise an exception when object isn't an ActiveReccord" do
|
247
|
+
@product = NonArProduct.new
|
248
|
+
assert_nothing_raised {
|
249
|
+
text_field(:product, :name)
|
250
|
+
text_field(:product, :times_sold)
|
251
|
+
text_field(:product, :published_at)
|
252
|
+
text_field(:product, :released_on)
|
253
|
+
text_field(:product, :cant_think_of_a_sensible_time_field)
|
254
|
+
text_field(:product, :price, :value => "1.499,90")
|
255
|
+
}
|
256
|
+
end
|
257
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# Filters added to this controller apply to all controllers in the application.
|
2
|
+
# Likewise, all the methods added will be available for all controllers.
|
3
|
+
|
4
|
+
class ApplicationController < ActionController::Base
|
5
|
+
helper :all # include all helpers, all the time
|
6
|
+
protect_from_forgery # See ActionController::RequestForgeryProtection for details
|
7
|
+
|
8
|
+
# Scrub sensitive parameters from your log
|
9
|
+
filter_parameter_logging :password
|
10
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# Don't change this file!
|
2
|
+
# Configure your app in config/environment.rb and config/environments/*.rb
|
3
|
+
|
4
|
+
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
|
5
|
+
|
6
|
+
module Rails
|
7
|
+
class << self
|
8
|
+
def boot!
|
9
|
+
unless booted?
|
10
|
+
preinitialize
|
11
|
+
pick_boot.run
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def booted?
|
16
|
+
defined? Rails::Initializer
|
17
|
+
end
|
18
|
+
|
19
|
+
def pick_boot
|
20
|
+
(vendor_rails? ? VendorBoot : GemBoot).new
|
21
|
+
end
|
22
|
+
|
23
|
+
def vendor_rails?
|
24
|
+
File.exist?("#{RAILS_ROOT}/vendor/rails")
|
25
|
+
end
|
26
|
+
|
27
|
+
def preinitialize
|
28
|
+
load(preinitializer_path) if File.exist?(preinitializer_path)
|
29
|
+
end
|
30
|
+
|
31
|
+
def preinitializer_path
|
32
|
+
"#{RAILS_ROOT}/config/preinitializer.rb"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Boot
|
37
|
+
def run
|
38
|
+
load_initializer
|
39
|
+
Rails::Initializer.run(:set_load_path)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class VendorBoot < Boot
|
44
|
+
def load_initializer
|
45
|
+
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
|
46
|
+
Rails::Initializer.run(:install_gem_spec_stubs)
|
47
|
+
Rails::GemDependency.add_frozen_gem_path
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class GemBoot < Boot
|
52
|
+
def load_initializer
|
53
|
+
self.class.load_rubygems
|
54
|
+
load_rails_gem
|
55
|
+
require 'initializer'
|
56
|
+
end
|
57
|
+
|
58
|
+
def load_rails_gem
|
59
|
+
if version = self.class.gem_version
|
60
|
+
gem 'rails', version
|
61
|
+
else
|
62
|
+
gem 'rails'
|
63
|
+
end
|
64
|
+
rescue Gem::LoadError => load_error
|
65
|
+
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
|
66
|
+
exit 1
|
67
|
+
end
|
68
|
+
|
69
|
+
class << self
|
70
|
+
def rubygems_version
|
71
|
+
Gem::RubyGemsVersion rescue nil
|
72
|
+
end
|
73
|
+
|
74
|
+
def gem_version
|
75
|
+
if defined? RAILS_GEM_VERSION
|
76
|
+
RAILS_GEM_VERSION
|
77
|
+
elsif ENV.include?('RAILS_GEM_VERSION')
|
78
|
+
ENV['RAILS_GEM_VERSION']
|
79
|
+
else
|
80
|
+
parse_gem_version(read_environment_rb)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def load_rubygems
|
85
|
+
min_version = '1.3.2'
|
86
|
+
require 'rubygems'
|
87
|
+
unless rubygems_version >= min_version
|
88
|
+
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
|
89
|
+
exit 1
|
90
|
+
end
|
91
|
+
|
92
|
+
rescue LoadError
|
93
|
+
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
|
94
|
+
exit 1
|
95
|
+
end
|
96
|
+
|
97
|
+
def parse_gem_version(text)
|
98
|
+
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
def read_environment_rb
|
103
|
+
File.read("#{RAILS_ROOT}/config/environment.rb")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# All that for this:
|
110
|
+
Rails.boot!
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file
|
2
|
+
|
3
|
+
# Specifies gem version of Rails to use when vendor/rails is not present
|
4
|
+
RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
|
5
|
+
|
6
|
+
# Bootstrap the Rails environment, frameworks, and default configuration
|
7
|
+
require File.join(File.dirname(__FILE__), 'boot')
|
8
|
+
|
9
|
+
Rails::Initializer.run do |config|
|
10
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
11
|
+
# Run "rake -D time" for a list of tasks for finding time zone names.
|
12
|
+
# config.time_zone = 'UTC'
|
13
|
+
|
14
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
15
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
|
16
|
+
# config.i18n.default_locale = :en
|
17
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# Settings specified here will take precedence over those in config/environment.rb
|
2
|
+
|
3
|
+
# The test environment is used exclusively to run your application's
|
4
|
+
# test suite. You never need to work with it otherwise. Remember that
|
5
|
+
# your test database is "scratch space" for the test suite and is wiped
|
6
|
+
# and recreated between test runs. Don't rely on the data there!
|
7
|
+
config.cache_classes = true
|
8
|
+
|
9
|
+
# Log error messages when you accidentally call methods on nil.
|
10
|
+
config.whiny_nils = true
|
11
|
+
|
12
|
+
# Show full error reports and disable caching
|
13
|
+
config.action_controller.consider_all_requests_local = true
|
14
|
+
config.action_controller.perform_caching = false
|
15
|
+
config.action_view.cache_template_loading = true
|
16
|
+
|
17
|
+
# Disable request forgery protection in test environment
|
18
|
+
config.action_controller.allow_forgery_protection = false
|
19
|
+
|
20
|
+
# Tell Action Mailer not to deliver emails to the real world.
|
21
|
+
# The :test delivery method accumulates sent emails in the
|
22
|
+
# ActionMailer::Base.deliveries array.
|
23
|
+
config.action_mailer.delivery_method = :test
|
24
|
+
|
25
|
+
# Use SQL instead of Active Record's schema dumper when creating the test database.
|
26
|
+
# This is necessary if your schema can't be completely dumped by the schema dumper,
|
27
|
+
# like if you have constraints or database-specific column types
|
28
|
+
# config.active_record.schema_format = :sql
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
# These settings change the behavior of Rails 2 apps and will be defaults
|
4
|
+
# for Rails 3. You can remove this initializer when Rails 3 is released.
|
5
|
+
|
6
|
+
if defined?(ActiveRecord)
|
7
|
+
# Include Active Record class name as root for JSON serialized output.
|
8
|
+
ActiveRecord::Base.include_root_in_json = true
|
9
|
+
|
10
|
+
# Store the full class name (including module namespace) in STI type column.
|
11
|
+
ActiveRecord::Base.store_full_sti_class = true
|
12
|
+
end
|
13
|
+
|
14
|
+
ActionController::Routing.generate_best_match = false
|
15
|
+
|
16
|
+
# Use ISO 8601 format for JSON serialized times and dates.
|
17
|
+
ActiveSupport.use_standard_json_time_format = true
|
18
|
+
|
19
|
+
# Don't escape HTML entities in JSON, leave that for the #json_escape helper.
|
20
|
+
# if you're including raw json in an HTML page.
|
21
|
+
ActiveSupport.escape_html_entities_in_json = false
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
# Your secret key for verifying cookie session data integrity.
|
4
|
+
# If you change this key, all old sessions will become invalid!
|
5
|
+
# Make sure the secret is at least 30 characters and all random,
|
6
|
+
# no regular words or you'll be exposed to dictionary attacks.
|
7
|
+
ActionController::Base.session = {
|
8
|
+
:key => '_rails_app_session',
|
9
|
+
:secret => '89e8147901a0d7c221ac130e0ded3eeab6dab4a97127255909f08fedaae371918b41dec9d4d75c5b27a55c3772d43c2b6a3cbac232c5cc2ce4b8ec22242f5e60'
|
10
|
+
}
|
11
|
+
|
12
|
+
# Use the database for sessions instead of the cookie-based default,
|
13
|
+
# which shouldn't be used to store highly confidential information
|
14
|
+
# (create the session table with "rake db:sessions:create")
|
15
|
+
# ActionController::Base.session_store = :active_record_store
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
ENV["RAILS_ENV"] = "test"
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/rails_app/config/environment")
|
3
|
+
require 'test_help'
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'active_record'
|
7
|
+
require 'active_record/test_case'
|
8
|
+
require 'action_view'
|
9
|
+
require 'action_view/test_case'
|
10
|
+
|
11
|
+
I18n.backend.store_translations :de, {
|
12
|
+
:date => {
|
13
|
+
:input => {
|
14
|
+
:formats => [:long, :short, :default]
|
15
|
+
},
|
16
|
+
:formats => {
|
17
|
+
:default => "%d.%m.%Y",
|
18
|
+
:short => "%e. %b",
|
19
|
+
:long => "%e. %B %Y",
|
20
|
+
:only_day => "%e"
|
21
|
+
},
|
22
|
+
:day_names => %w(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag),
|
23
|
+
:abbr_day_names => %w(So Mo Di Mi Do Fr Sa),
|
24
|
+
:month_names => [nil] + %w(Januar Februar März April Mai Juni Juli August September Oktober November Dezember),
|
25
|
+
:abbr_month_names => [nil] + %w(Jan Feb Mär Apr Mai Jun Jul Aug Sep Okt Nov Dez)
|
26
|
+
},
|
27
|
+
:time => {
|
28
|
+
:input => {
|
29
|
+
:formats => [:long, :medium, :short, :default, :time]
|
30
|
+
},
|
31
|
+
:formats => {
|
32
|
+
:default => "%A, %e. %B %Y, %H:%M Uhr",
|
33
|
+
:short => "%e. %B, %H:%M Uhr",
|
34
|
+
:medium => "%e. %B %Y, %H:%M Uhr",
|
35
|
+
:long => "%A, %e. %B %Y, %H:%M Uhr",
|
36
|
+
:time => "%H:%M Uhr"
|
37
|
+
},
|
38
|
+
:am => 'vormittags',
|
39
|
+
:pm => 'nachmittags'
|
40
|
+
},
|
41
|
+
:number => {
|
42
|
+
:format => {
|
43
|
+
:precision => 2,
|
44
|
+
:separator => ',',
|
45
|
+
:delimiter => '.'
|
46
|
+
}
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
I18n.locale = :de
|
51
|
+
|
52
|
+
class NonArProduct
|
53
|
+
attr_accessor :name, :price, :times_sold,
|
54
|
+
:cant_think_of_a_sensible_time_field,
|
55
|
+
:released_on, :published_at
|
56
|
+
end
|
57
|
+
|
58
|
+
class Product < ActiveRecord::Base
|
59
|
+
end
|
60
|
+
|
61
|
+
class ProductWithValidation < Product
|
62
|
+
validates_numericality_of :price
|
63
|
+
validates_presence_of :price
|
64
|
+
end
|
65
|
+
|
66
|
+
config = YAML.load_file(File.dirname(__FILE__) + '/database.yml')
|
67
|
+
ActiveRecord::Base.establish_connection(config['test'])
|
68
|
+
|
69
|
+
ActiveRecord::Base.connection.create_table :products do |t|
|
70
|
+
t.string :name
|
71
|
+
t.date :released_on
|
72
|
+
t.datetime :published_at
|
73
|
+
t.time :cant_think_of_a_sensible_time_field
|
74
|
+
t.timestamp :the_timestamp
|
75
|
+
t.decimal :price
|
76
|
+
t.float :weight
|
77
|
+
t.integer :times_sold
|
78
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: foolabs-delocalize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 1
|
9
|
+
version: 0.2.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Clemens Kofler
|
13
|
+
- Marcin Raczkowski
|
14
|
+
- "Fernando Migliorini Luiz\xC3\xA3o"
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-04-09 00:00:00 +02:00
|
20
|
+
default_executable:
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: Delocalize is a tool for parsing localized dates/times and numbers.
|
24
|
+
email:
|
25
|
+
- clemens@railway.at
|
26
|
+
- marcin.raczkowski@gmail.com
|
27
|
+
- fernandoluizao@gmail.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- README
|
34
|
+
files:
|
35
|
+
- MIT-LICENSE
|
36
|
+
- README
|
37
|
+
- Rakefile
|
38
|
+
- VERSION
|
39
|
+
- init.rb
|
40
|
+
- lib/delocalize.rb
|
41
|
+
- lib/delocalize/i18n_ext.rb
|
42
|
+
- lib/delocalize/localized_date_time_parser.rb
|
43
|
+
- lib/delocalize/localized_numeric_parser.rb
|
44
|
+
- lib/delocalize/rails_ext.rb
|
45
|
+
- lib/delocalize/rails_ext/action_view.rb
|
46
|
+
- lib/delocalize/rails_ext/active_record.rb
|
47
|
+
- lib/delocalize/rails_ext/time_zone.rb
|
48
|
+
- lib/delocalize/rails_ext/validation.rb
|
49
|
+
- lib/delocalize/ruby_ext.rb
|
50
|
+
- lib/delocalize/ruby_ext/date.rb
|
51
|
+
- lib/delocalize/ruby_ext/datetime.rb
|
52
|
+
- lib/delocalize/ruby_ext/numeric.rb
|
53
|
+
- lib/delocalize/ruby_ext/time.rb
|
54
|
+
- tasks/distribution.rb
|
55
|
+
- tasks/documentation.rb
|
56
|
+
- tasks/testing.rb
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: http://github.com/clemens/delocalize
|
59
|
+
licenses: []
|
60
|
+
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options:
|
63
|
+
- --charset=UTF-8
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.6
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Localized date/time and number parsing
|
87
|
+
test_files:
|
88
|
+
- test/delocalize_test.rb
|
89
|
+
- test/rails_app/app/controllers/application_controller.rb
|
90
|
+
- test/rails_app/config/boot.rb
|
91
|
+
- test/rails_app/config/environment.rb
|
92
|
+
- test/rails_app/config/environments/test.rb
|
93
|
+
- test/rails_app/config/initializers/new_rails_defaults.rb
|
94
|
+
- test/rails_app/config/initializers/session_store.rb
|
95
|
+
- test/rails_app/config/routes.rb
|
96
|
+
- test/test_helper.rb
|