delocalize 0.1.4 → 0.1.5

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -0,0 +1,17 @@
1
+ # TODO:
2
+ # * proper documentation (comments)
3
+ module Delocalize
4
+ class LocalizedNumericParser
5
+ class << self
6
+ # Parse numbers removing unneeded characters and replacing decimal separator
7
+ # through I18n. This will return a valid Ruby Numeric value (as String).
8
+ def parse(value)
9
+ if value.is_a?(String)
10
+ separator = I18n.t(:'number.format.separator')
11
+ value = value.gsub(/[^0-9\-#{separator}]/, '').gsub(separator, '.')
12
+ end
13
+ value
14
+ end
15
+ end
16
+ end
17
+ end
@@ -7,10 +7,6 @@ ActiveRecord::ConnectionAdapters::Column.class_eval do
7
7
  def time?
8
8
  klass == Time
9
9
  end
10
-
11
- def decimal?
12
- klass == BigDecimal
13
- end
14
10
  end
15
11
 
16
12
  ActiveRecord::Base.class_eval do
@@ -20,9 +16,8 @@ ActiveRecord::Base.class_eval do
20
16
  value = Date.parse_localized(value)
21
17
  elsif column.time?
22
18
  value = Time.parse_localized(value)
23
- elsif column.decimal?
24
- value = convert_number_column_value_with_localization(value)
25
- value = BigDecimal(value) if value.is_a?(String)
19
+ elsif column.number?
20
+ value = column.type_cast(convert_number_column_value_with_localization(value))
26
21
  end
27
22
  end
28
23
  write_attribute_without_localization(attr_name, value)
@@ -45,9 +40,7 @@ ActiveRecord::Base.class_eval do
45
40
 
46
41
  def convert_number_column_value_with_localization(value)
47
42
  value = convert_number_column_value_without_localization(value)
48
- if I18n.delocalization_enabled? && value.is_a?(String)
49
- value = value.gsub(/[^0-9\-#{I18n.t(:'number.format.separator')}]/, '').gsub(I18n.t(:'number.format.separator'), '.')
50
- end
43
+ value = Numeric.parse_localized(value) if I18n.delocalization_enabled?
51
44
  value
52
45
  end
53
46
  alias_method_chain :convert_number_column_value, :localization
@@ -1,3 +1,4 @@
1
1
  require 'delocalize/ruby_ext/date'
2
2
  require 'delocalize/ruby_ext/datetime'
3
- require 'delocalize/ruby_ext/time'
3
+ require 'delocalize/ruby_ext/time'
4
+ require 'delocalize/ruby_ext/numeric'
@@ -0,0 +1,9 @@
1
+ require 'delocalize/localized_numeric_parser'
2
+
3
+ Numeric.class_eval do
4
+ class << self
5
+ def parse_localized(value)
6
+ Delocalize::LocalizedNumericParser.parse(value)
7
+ end
8
+ end
9
+ end
@@ -14,20 +14,24 @@ class DelocalizeActiveRecordTest < ActiveRecord::TestCase
14
14
  assert_equal -1299.99, @product.price
15
15
  end
16
16
 
17
- test "delocalizes localized date" do
17
+ test "delocalizes localized date with year" do
18
18
  date = Date.civil(2009, 10, 19)
19
19
 
20
20
  @product.released_on = '19. Oktober 2009'
21
21
  assert_equal date, @product.released_on
22
22
 
23
- @product.released_on = '19. Okt'
23
+ @product.released_on = '19.10.2009'
24
24
  assert_equal date, @product.released_on
25
+ end
25
26
 
26
- @product.released_on = '19.10.2009'
27
+ test "delocalizes localized date without year" do
28
+ date = Date.civil(Date.today.year, 10, 19)
29
+
30
+ @product.released_on = '19. Okt'
27
31
  assert_equal date, @product.released_on
28
32
  end
29
33
 
30
- test "delocalizes localized datetime" do
34
+ test "delocalizes localized datetime with year" do
31
35
  time = Time.local(2009, 3, 1, 12, 0, 0)
32
36
 
33
37
  @product.published_at = 'Sonntag, 1. März 2009, 12:00 Uhr'
@@ -35,6 +39,10 @@ class DelocalizeActiveRecordTest < ActiveRecord::TestCase
35
39
 
36
40
  @product.published_at = '1. März 2009, 12:00 Uhr'
37
41
  assert_equal time, @product.published_at
42
+ end
43
+
44
+ test "delocalizes localized datetime without year" do
45
+ time = Time.local(Date.today.year, 3, 1, 12, 0, 0)
38
46
 
39
47
  @product.published_at = '1. März, 12:00 Uhr'
40
48
  assert_equal time, @product.published_at
@@ -116,6 +124,13 @@ class DelocalizeActiveRecordTest < ActiveRecord::TestCase
116
124
  @product.price = "10,34"
117
125
  assert @product.price_changed?
118
126
  end
127
+
128
+ test "dirty attributes must detect changes in float columns" do
129
+ @product.weight = 10
130
+ @product.save
131
+ @product.weight = "10,34"
132
+ assert @product.weight_changed?
133
+ end
119
134
  end
120
135
 
121
136
  class DelocalizeActionViewTest < ActionView::TestCase
@@ -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
@@ -0,0 +1,4 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ map.connect ':controller/:action/:id'
3
+ map.connect ':controller/:action/:id.:format'
4
+ end
data/test/test_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  ENV["RAILS_ENV"] = "test"
2
- require File.expand_path(File.dirname(__FILE__) + "/../../../../config/environment")
2
+ #require File.expand_path(File.dirname(__FILE__) + "/../../../../config/environment")
3
+ require File.expand_path(File.dirname(__FILE__) + "/rails_app/config/environment")
3
4
  require 'test_help'
4
5
 
5
6
  require 'rubygems'
@@ -58,7 +59,7 @@ end
58
59
  class Product < ActiveRecord::Base
59
60
  end
60
61
 
61
- config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
62
+ config = YAML.load_file(File.dirname(__FILE__) + '/database.yml')
62
63
  ActiveRecord::Base.establish_connection(config['test'])
63
64
 
64
65
  ActiveRecord::Base.connection.create_table :products do |t|
@@ -67,5 +68,6 @@ ActiveRecord::Base.connection.create_table :products do |t|
67
68
  t.datetime :published_at
68
69
  t.time :cant_think_of_a_sensible_time_field
69
70
  t.decimal :price
71
+ t.float :weight
70
72
  t.integer :times_sold
71
- end
73
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delocalize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clemens Kofler
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-25 00:00:00 +01:00
12
+ date: 2010-01-17 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -30,6 +30,7 @@ files:
30
30
  - lib/delocalize.rb
31
31
  - lib/delocalize/i18n_ext.rb
32
32
  - lib/delocalize/localized_date_time_parser.rb
33
+ - lib/delocalize/localized_numeric_parser.rb
33
34
  - lib/delocalize/rails_ext.rb
34
35
  - lib/delocalize/rails_ext/action_view.rb
35
36
  - lib/delocalize/rails_ext/active_record.rb
@@ -37,6 +38,7 @@ files:
37
38
  - lib/delocalize/ruby_ext.rb
38
39
  - lib/delocalize/ruby_ext/date.rb
39
40
  - lib/delocalize/ruby_ext/datetime.rb
41
+ - lib/delocalize/ruby_ext/numeric.rb
40
42
  - lib/delocalize/ruby_ext/time.rb
41
43
  - tasks/distribution.rb
42
44
  - tasks/documentation.rb
@@ -71,4 +73,11 @@ specification_version: 3
71
73
  summary: Localized date/time and number parsing
72
74
  test_files:
73
75
  - test/delocalize_test.rb
76
+ - test/rails_app/app/controllers/application_controller.rb
77
+ - test/rails_app/config/boot.rb
78
+ - test/rails_app/config/environment.rb
79
+ - test/rails_app/config/environments/test.rb
80
+ - test/rails_app/config/initializers/new_rails_defaults.rb
81
+ - test/rails_app/config/initializers/session_store.rb
82
+ - test/rails_app/config/routes.rb
74
83
  - test/test_helper.rb