Git Product home page Git Product logo

Comments (9)

robinbortlik avatar robinbortlik commented on June 30, 2024

Hello @simonasdev ,

first, thanks for nice report. I think you really found a bug. The tricky part is that when you ask enum attribute for value it will return you the value from array and not the value which comes to database.

I will try to look on it. For now you can use a workaround with proc. It should work that you will change your validation definition like this

validates :valid_from, :valid_until, overlap: { 
    exclude_edges: ['valid_from', 'valid_until'], 
    scope: ['object_parameter_id', 'resource_id', 'resource_type', kind: -> { |obj| obj.read_attribute(:kind) }]
  }

Let me know if it helped.

from validates_overlap.

robinbortlik avatar robinbortlik commented on June 30, 2024

Ok,

so I changed the send method to read_attribute if you will now set gem source from github then everything should start work with the validation definition as you had.

Let me please know if it helped

from validates_overlap.

robinbortlik avatar robinbortlik commented on June 30, 2024

Try to update to 0.5.2 .

from validates_overlap.

simonasdev avatar simonasdev commented on June 30, 2024

I updated the Gemfile and it did start working, thanks a lot. This should be sufficient, since we can always use lambdas to overwrite the default read_attribute. Hopefully it didn't break anyone else's code.

from validates_overlap.

tejamedasani avatar tejamedasani commented on June 30, 2024

Hi @robinbortlik, This bug fix is not working in Rails 5. read_attribute is returning values of enum after typecast. Only way i found to get the integer value is to use "read_attribute_before_typecast", but this breaks other columns like date.
Currently, I am using the passing the following proc to scope in my model as a workaround. My enum is named day.

:scope => {"day" => proc{|object| object.class.days[object.day] } }

As an aside, I am unable to declare procs when passing an array to the scope.
For instance, this is not working
:scope => ["attr1", attr2: => proc{|obj| obj.some_value}]
This is the workaround i am using to use regular scope along with proc
:scope => {"attr1" => 'false', attr2: => proc{|obj| obj.some_value}}

Is this the expected behaviour? If yes, I think the readme needs to better reflect it. Let me know what you think and i will update the readme.

Code in the gem dealing with arrays and hashes in scope
def add_attributes(record, attrs)
if attrs.is_a?(Array)
attrs.each { |attr| add_attribute(record, attr) }
elsif attrs.is_a?(Hash)
attrs.each do |attr_name, value|
add_attribute(record, attr_name, value)
end
else
add_attribute(record, attrs)
end
end

from validates_overlap.

robinbortlik avatar robinbortlik commented on June 30, 2024

Hi @tejamedasani I can take a look, but I' pretty busy at this moment, so it will not be earlier then next week or maybe week after. The PRs are always welcome.

from validates_overlap.

robinbortlik avatar robinbortlik commented on June 30, 2024

Hi @tejamedasani the bug should be fixed by #38 . You can update gem version to 0.8.1 and everything should work.

from validates_overlap.

tejamedasani avatar tejamedasani commented on June 30, 2024

from validates_overlap.

Zmokizmoghi avatar Zmokizmoghi commented on June 30, 2024

Hello, @robinbortlik i'v just found this bug again.

class Lession < ApplicationRecord
  enum day: {
    monday: 0,
    tuesday: 1,
    wednesday: 2,
    thursday: 3,
    friday: 4,
    saturday: 5,
    sunday: 6
  }
  validates :start_at, :end_at, overlap: { exclude_edges: ["starts_at", "ends_at"], scope: [:day]}
end

in that case function is_enum_attribute? will return false, because record.class.defined_enums[attr_name.to_s].present? return false.

But, if use scope: ['day'] methods above will return true.

Rails 5.1.4
validates_overlap at master@7be2f5e

PR Attached
Thanks.

from validates_overlap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.