{"id":15405173,"url":"https://github.com/fnando/formatted_attributes","last_synced_at":"2025-08-09T05:32:12.453Z","repository":{"id":43046456,"uuid":"1057065","full_name":"fnando/formatted_attributes","owner":"fnando","description":"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.","archived":false,"fork":false,"pushed_at":"2023-09-04T19:36:37.000Z","size":28,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-08T08:47:17.726Z","etag":null,"topics":["activerecord","formatter","ruby"],"latest_commit_sha":null,"homepage":"http://rubygems.org/gems/formatted_attributes","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fnando.png","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","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["fnando"],"custom":["https://paypal.me/nandovieira/🍕"]}},"created_at":"2010-11-06T15:51:03.000Z","updated_at":"2022-01-13T19:38:38.000Z","dependencies_parsed_at":"2024-10-19T11:19:01.567Z","dependency_job_id":null,"html_url":"https://github.com/fnando/formatted_attributes","commit_stats":{"total_commits":7,"total_committers":3,"mean_commits":"2.3333333333333335","dds":0.5714285714285714,"last_synced_commit":"b50d959ecabec84408f4acf2a75530affb9fe11d"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fformatted_attributes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fformatted_attributes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fformatted_attributes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fformatted_attributes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fnando","download_url":"https://codeload.github.com/fnando/formatted_attributes/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249294881,"owners_count":21246008,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","formatter","ruby"],"created_at":"2024-10-01T16:15:19.449Z","updated_at":"2025-04-17T00:51:14.056Z","avatar_url":"https://github.com/fnando.png","language":"Ruby","funding_links":["https://github.com/sponsors/fnando","https://paypal.me/nandovieira/🍕"],"categories":[],"sub_categories":[],"readme":"# formatted_attributes\n\n[![Tests](https://github.com/fnando/formatted_attributes/workflows/ruby-tests/badge.svg)](https://github.com/fnando/formatted_attributes)\n[![Gem](https://img.shields.io/gem/v/formatted_attributes.svg)](https://rubygems.org/gems/formatted_attributes)\n[![Gem](https://img.shields.io/gem/dt/formatted_attributes.svg)](https://rubygems.org/gems/formatted_attributes)\n\nAdd methods that format attributes from/to helper methods. Works with\nActiveRecord and non-ActiveRecord classes.\n\n## Installation\n\n```bash\ngem install formatted_attributes\n```\n\nOr add the following line to your project's Gemfile:\n\n```ruby\ngem \"formatted_attributes\"\n```\n\n## Usage\n\nA formatted attribute will require two methods: `convert_with_#{formatter}` and\n`convert_from_#{formatter}`. The `convert_from_*` method will convert the\nformatted value to the original value and the `convert_with_*` method will do\nthe other way around, converting the original value to the formatted version.\n\nSee an example on how to format prices from strings like 1,23 to cents like 123\nand vice-versa.\n\n```ruby\nclass Product \u003c ActiveRecord::Base\n  include ActionView::Helpers::NumberHelper\n\n  formatted :price, with: :number_format\n\n  private\n  def convert_with_number_format(amount)\n    number = BigDecimal(number.to_s) / 100\n    number_to_currency(number, unit: \"\", separator: \",\", delimiter: \"\")\n  end\n\n  def convert_from_number_format(amount)\n    _, operator, number, precision = *number.to_s.match(/^([+-])?(\\d+)(?:[,.](\\d+))?$/)\n    (BigDecimal(\"#{operator}#{number}.#{precision.to_i}\") * 100).to_i\n  end\nend\n\nproduct = Product.new(formatted_price: \"1,23\")\nproduct.price\n#=\u003e 123\n\nproduct.price = 456\nproduct.formatted_price\n#=\u003e \"4,56\"\n```\n\n## Maintainer\n\n- [Nando Vieira](https://github.com/fnando)\n\n## Contributors\n\n- https://github.com/fnando/formatted_attributes/contributors\n\n## Contributing\n\nFor more details about how to contribute, please read\nhttps://github.com/fnando/formatted_attributes/blob/main/CONTRIBUTING.md.\n\n## License\n\nThe gem is available as open source under the terms of the\n[MIT License](https://opensource.org/licenses/MIT). A copy of the license can be\nfound at https://github.com/fnando/formatted_attributes/blob/main/LICENSE.md.\n\n## Code of Conduct\n\nEveryone interacting in the formatted_attributes project's codebases, issue\ntrackers, chat rooms and mailing lists is expected to follow the\n[code of conduct](https://github.com/fnando/formatted_attributes/blob/main/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Fformatted_attributes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffnando%2Fformatted_attributes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Fformatted_attributes/lists"}