Comments (5)
I found that I was able to do this with write_attribute(). But I would still like to know the canonical or expected usage.
from latinum.
I'm still using this gem and I suppose you could call it maintained, it's just been stable enough that no changes were required.
The following code was implemented for support of serialize
:
latinum/lib/latinum/resource.rb
Lines 40 to 57 in acf12ea
A bank object is only needed for formatting and converting resources, it should not be needed for basic interactions with "I have X units of Y."
For actual values, you need to write:
entry.line_items.create!(amount: '40145.00 USD')
If you want rich input, you do need to use the bank configured with the currencies you want. Otherwise, how can you tell $5
is USD, NZD, AUD, etc.
In order to use the bank object, you should configure it in the controller to map the user input before creating the model with attribute values.
e.g.
params['amount'] = bank.parse(params['amount'])
I can write updated documentation with working examples if that helps.
from latinum.
I wonder if we should make Resource.load
also check for Resource
instances and return it directly to support the above use case if it isn't already handled by AR.
from latinum.
Okay, so with a few minor changes, we can make this work a bit more nicely:
Loading development environment (Rails 7.1.1)
irb(main):001> t = T.new
=> #<T:0x00007f2fb8b88ff0 id: nil, data: nil, amount: nil, created_at: nil, updated_at: nil>
irb(main):002> t.amount = "$5 USD"
=> "$5 USD"
irb(main):003> t.amount
=> #<Latinum::Resource "5.0 USD">
The code is:
require 'latinum/currencies/global'
class T < ApplicationRecord
BANK = Latinum::Bank.new.tap do |bank|
bank.import(Latinum::Currencies::Global)
end
serialize :amount, coder: BANK
end
This uses the bank as a coder, which accepts a wider range of inputs. However, if there is ambiguity, it is resolved according to the bank's internal priority, e.g. $
is used by several currencies.
from latinum.
I've released v1.8.0 with the required changes and added documentation: https://ioquatix.github.io/latinum/guides/activerecord-integration/index.html
from latinum.
Related Issues (3)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from latinum.