Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement I18n::Alchemy#unlocalize to return the original target #50

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Ruby 2.6 support (no changes required)
* Rails 6 support ([@mayordwells](https://github.com/mayordwells))
* Add support for `update`/`update!` Active Record methods ([@tbk303](https://github.com/tbk303))
* Add unlocalize to get the original target ([@sobrinho](https://github.com/sobrinho))

## v0.3.0 - 2018-09-08

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ I18n.with_locale :pt do
end
```

Keep in mind that `product.localized` is a proxy object and if you need the original value, you need either to use the original object or unlocalize the proxy for action view helpers like `date_select`:

```ruby
# In case you have the original target.
f.date_select :released_at, selected: @product.released_at

# In case you don't have the original target, you can use `unlocalize` to get it back.
f.date_select :released_at, selected: @localized.unlocalize.released_at
```

This is necessary because such Rails helpers expects a Date/Time object and not a localized string.

### Localizing methods

Given a product model with a `total` method, that is a simple calculation of `quantity * price`, you can tell **I18n::Alchemy** to localize that method for you together with the attributes:
Expand Down
6 changes: 6 additions & 0 deletions lib/i18n_alchemy/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def initialize(target, attributes = nil)
assign_attributes(attributes) if attributes
end

# Returns the original target. This is useful for action view helpers that
# expects the unlocalized value like a +Date+ or +Time+.
def unlocalize
@target
end

# Override to_param to always return the +proxy.to_param+. This allow us
# to integrate with action view.
def to_param
Expand Down
4 changes: 4 additions & 0 deletions test/i18n_alchemy/proxy_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require "test_helper"

class ProxyTest < I18n::Alchemy::ProxyTestCase
def test_unlocalize
assert_equal @product, @localized.unlocalize
end

def test_delegates_orm_methods_to_target_object
assert @product.new_record?
assert @localized.save!(name: "foo", price: 1.99)
Expand Down