No release in over 3 years
Low commit activity in last 3 years
Delocalize date, time and number model attributes
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
>= 4
~> 13.0
~> 3.0
= 0.80.1
 Project Readme

ActiveRecordDelocalize

active_record_delocalize provides localized date, time and number parsing functionality for active_record attributes.

What does it do?

Sometimes you want to save localized data (like date or number) to database:

order = Order.new
order.cost = "$ 6 999,99"
order.save

But first you need to parse them to active-record acceptable format. You may manual override attribute accessors:

class Order < ActiveRecord::Base
  def cost=(cost)
    write_attribute(:cost, cost.gsub(/\$|\s/, "").gsub(',', '.'))
  end
end

But active_record_delocalize make it easier:

class Order < ActiveRecord::Base
  delocalize cost: :number
end

Installation

Add this line to your application's Gemfile:

gem 'active_record_delocalize'

And then execute:

$ bundle

Usage

You need specify delocalize value per each model using the following declarative DSL:

class Order < ActiveRecord::Base
  delocalize cost: :number, delivered_on: :date
end

Where cost and delivered_on is attribute names and number and date is data types. Now active_record_delocalize support three types: date, time and number. You can also pass a data hash instead of type, where the key is the data type and the value is the format, or format name from your localize file, e.g.:

class Order < ActiveRecord::Base
  delocalize cost: { delimiter: ",", separator: " " }, delivered_on: :short
end

Configuration

In addition to your model setup, you also need to configure your locale file(s). Assuming you want to use all of delocalize's parsers (date, time, number), the required keys are:

  • number.default.format.delimiter
  • number.default.format.separator
  • date.formats.default
  • time.formats.default

For example:

ru:
  number:
    default:
      format:
        delimiter: "."
        separator: " "
  time:
    formats:
      default: '%d.%m.%Y в %H:%M'
  date:
    formats:
      default: '%d.%m.%Y'

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/heckfy/active_record_delocalize. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.