Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Kuba Krzempek
@kubakrzempek
I tried with value(:organziation_id) but that returns something like an ast graph
Tim Riley
@timriley
@kubakrzempek if you want to work on the two at once, I think you need to make a "high level rule" (it's in the docs) which could then pass those two values to a predicate to check for uniqueness of both
Simon Schmid
@sled
not sure whether it works though ;)
Kuba Krzempek
@kubakrzempek
@timriley @sled thank you for your answers. As you suggested I went back on "high level rule" track and it looks like this now
https://gist.github.com/kubakrzempek/f6c15643b9adae7e3de27a6b946aee30
but it still doesn't work, because sequel queries expect organization_id and code to be values, not Dry::Validation::Schema::Rule objects.
I commented the unique? method's body and returned false explicitly, and the rule(:uniq_code_organization) failed, because it didn't expected to receive a FalseClass object.
Do you know any other way to implement block assigned to rule?
Andy Holland
@AMHOL
@kubakrzempek you know you passed the arguments to unique? in the wrong order?
@kubakrzempek I'm not sure how to do what you're trying to do but looking at http://dry-rb.org/gems/dry-validation/high-level-rules/ and http://dry-rb.org/gems/dry-validation/custom-predicates/ what you have so far isn't right
Christopher Dennl-Ortega Arrieta
@cdennl
@AMHOL no it is not wrong order
Andy Holland
@AMHOL
It's also better to have your repositories return materialized results, rather than having query logic leak out into other parts of your application
Christopher Dennl-Ortega Arrieta
@cdennl
@AMHOL the order is reversed
Andy Holland
@AMHOL
Ahh OK
Christopher Dennl-Ortega Arrieta
@cdennl
got trapped by this as well a week ago
but solnic meant this is intended
Simon Schmid
@sled
    rule(code_organization_uniq: [:code, :organization_id]) do |code, organization_id|
      unique?(code, organization_id)
    end
can you give me the .inspect output of the arguments?
is it a <#Rule ...> object?
Andy Holland
@AMHOL
It looks like the return value from rule should be a predicate
Simon Schmid
@sled
it looks like the .value method evaluates a predicate and just returns true/false
Andy Holland
@AMHOL
I'm thinking maybe this isn't supported yet
dry-rb/dry-validation#105
Christopher Dennl-Ortega Arrieta
@cdennl
@kubakrzempek your gist should work, if not then it is a sequel problem, because thats how i wrote my own predicates as well, take a look:
panigrah
@panigrah
in the new dry-v how do i check for property is empty or if present then it is inclusion: {in: 0..5}
Christopher Dennl-Ortega Arrieta
@cdennl
module Dagavel::Predicates
  include Dry::Logic::Predicates

  predicate( :is_conversion_correct? ) do |exchange_rate_usd_points, points, usd|
    points_in_usd = ( Auction.convert_points_to_money( points, exchange_rate_usd_points ) * 100 ).floor
    usd = ( usd * 100 ).floor

    usd == points_in_usd
  end
end

...

rule( start_bid_conversion_is_correct: [ :start_bid_in_points, :start_bid_in_usd, :exchange_rate_usd_points ] ) do
  |start_bid_in_points, start_bid_in_usd, exchange_rate_usd_points|
        ( start_bid_in_points.int? & start_bid_in_usd.float? & exchange_rate_usd_points.int? ).then(
            start_bid_in_usd.is_conversion_correct?( start_bid_in_points, exchange_rate_usd_points )
        )
      end
Andy Holland
@AMHOL
@cdennl @kubakrzempek what version/branch are you both using?
Christopher Dennl-Ortega Arrieta
@cdennl
reform 2-2 branch, dry-v 0.7.4
0.7.3 sorry
Andy Holland
@AMHOL
Your rule returns the result of a predicate tho, so it's not the same as @kubakrzempek's
Christopher Dennl-Ortega Arrieta
@cdennl
yes you have to define a predicate
but he defined one as well
he just used inline definition
where mine resides in a module because i need it several times
Andy Holland
@AMHOL

Looks like changing rule to

    rule(code_organization_uniq: [:code, :organization_id]) do |code, organization_id|
      code.unique?(organization_id)
    end

Should fix it

Left a comment on the gist @kubakrzempek
Kuba Krzempek
@kubakrzempek
@AMHOL that works!!!
Thank you a milion!
Andy Holland
@AMHOL
Cool, np, and thanks @cdennl for pointing that out
Simon Schmid
@sled
half of dry-*'s awesomeness is this channel ;)
Kuba Krzempek
@kubakrzempek
Thank all of you for your help!
Christopher Dennl-Ortega Arrieta
@cdennl
np
Andy Holland
@AMHOL
@sled :D
Christopher Dennl-Ortega Arrieta
@cdennl
yeah predicates must run on rules it seems
Andy Holland
@AMHOL
@panigrah what do you mean by "empty"?
nil, and empty array, an empty string?
panigrah
@panigrah
@AMHOL it can be nil or empty string
Andy Holland
@AMHOL
Or an integer?
Christopher Dennl-Ortega Arrieta
@cdennl
optional(:my_string).maybe(size?: 0..5) in the old syntax
@panigrah
sorry didnt read that you asked forn ew syntax
Andy Holland
@AMHOL
Ahh, sorry, I was thinking the value should be between 1 and 5