Next-gen ruby libs! » github.com/dry-rb » website: https://dry-rb.org » forum: https://discourse.dry-rb.org
Following on from experimenting with dry-validations…
How would you verify uniqueness when scoped to another record?
Say “account” has many “users”, and I want users.name to be unqiue within an account….
unique_within_account?(account_id, user_name)
name.unique_within_account?(account_id)
def unique_within?(first, second, the_value)
name.unique_within?(“foo”, “bar”)
, first
would be ”foo”
, second
would be ”bar”
required(:email).filled(scoped_unique?: :email, scope?: { active: true })
def scoped_unique?(attr_name, scope, value)
.filled(prdicate_name?: :args_go_here)
so… if there are two args`.filled(prdicate_name?: :args_go_here)
would it be:
`.filled(prdicate_name?: [:arg1, :arg2])
required(:email).filled(scoped_unique?: :email, scope?: { active: true })
should be
required(:email).filled(scoped_unique?: [:email, { active: true }])
One more thing — is it possible to infer the key of the predicate being tested?
that “required(:email).filled(unique?: :email)” has to repeat the ‘email’ key as an arg
hey there, a ruby question, how do we change namespace scope..
I remember you did something like
something < self
end
then you could define without saying def self.method
Module::Class
somewhere
module Ranking
class Ranking
# bla bla bla stuff
def get_score(user)
Metrics::METRICS.reduce(0) do |points_total, (metric, points)|
points_total + metric.calculate(user, @scope) * points
end
end
end
end
module Ranking
module Metrics
METRICS = {
Metrics::Messages => 30,
Metrics::Activity => 40,
......
}
end
end
Metrics::Xxxxxx
in the METRICS
constant
Ranking
class that is in a different naming scope would fail to find them
metric = Object.const_get("Metrics").const_get(metric.to_s)
, hurts my eyes
Metrics.const_get(metric)