Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fnando/formatted_attributes
Sometimes you need to expose a helper method that will convert its value before saving it to the database. This gem will add `convert_with` and `convert_from` suffixed methods to the attributes you specify.
https://github.com/fnando/formatted_attributes
activerecord formatter ruby
Last synced: 28 days ago
JSON representation
Sometimes you need to expose a helper method that will convert its value before saving it to the database. This gem will add `convert_with` and `convert_from` suffixed methods to the attributes you specify.
- Host: GitHub
- URL: https://github.com/fnando/formatted_attributes
- Owner: fnando
- License: mit
- Created: 2010-11-06T15:51:03.000Z (about 14 years ago)
- Default Branch: main
- Last Pushed: 2023-09-04T19:36:37.000Z (about 1 year ago)
- Last Synced: 2024-10-06T04:27:40.916Z (about 1 month ago)
- Topics: activerecord, formatter, ruby
- Language: Ruby
- Homepage: http://rubygems.org/gems/formatted_attributes
- Size: 27.3 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# formatted_attributes
[![Tests](https://github.com/fnando/formatted_attributes/workflows/ruby-tests/badge.svg)](https://github.com/fnando/formatted_attributes)
[![Gem](https://img.shields.io/gem/v/formatted_attributes.svg)](https://rubygems.org/gems/formatted_attributes)
[![Gem](https://img.shields.io/gem/dt/formatted_attributes.svg)](https://rubygems.org/gems/formatted_attributes)Add methods that format attributes from/to helper methods. Works with
ActiveRecord and non-ActiveRecord classes.## Installation
```bash
gem install formatted_attributes
```Or add the following line to your project's Gemfile:
```ruby
gem "formatted_attributes"
```## Usage
A formatted attribute will require two methods: `convert_with_#{formatter}` and
`convert_from_#{formatter}`. The `convert_from_*` method will convert the
formatted value to the original value and the `convert_with_*` method will do
the other way around, converting the original value to the formatted version.See an example on how to format prices from strings like 1,23 to cents like 123
and vice-versa.```ruby
class Product < ActiveRecord::Base
include ActionView::Helpers::NumberHelperformatted :price, with: :number_format
private
def convert_with_number_format(amount)
number = BigDecimal(number.to_s) / 100
number_to_currency(number, unit: "", separator: ",", delimiter: "")
enddef convert_from_number_format(amount)
_, operator, number, precision = *number.to_s.match(/^([+-])?(\d+)(?:[,.](\d+))?$/)
(BigDecimal("#{operator}#{number}.#{precision.to_i}") * 100).to_i
end
endproduct = Product.new(formatted_price: "1,23")
product.price
#=> 123product.price = 456
product.formatted_price
#=> "4,56"
```## Maintainer
- [Nando Vieira](https://github.com/fnando)
## Contributors
- https://github.com/fnando/formatted_attributes/contributors
## Contributing
For more details about how to contribute, please read
https://github.com/fnando/formatted_attributes/blob/main/CONTRIBUTING.md.## License
The gem is available as open source under the terms of the
[MIT License](https://opensource.org/licenses/MIT). A copy of the license can be
found at https://github.com/fnando/formatted_attributes/blob/main/LICENSE.md.## Code of Conduct
Everyone interacting in the formatted_attributes project's codebases, issue
trackers, chat rooms and mailing lists is expected to follow the
[code of conduct](https://github.com/fnando/formatted_attributes/blob/main/CODE_OF_CONDUCT.md).