Git Product home page Git Product logo

validates_overlap's People

Contributors

bloopletech avatar geoffharcourt avatar kamipo avatar kb avatar mikdiet avatar newuser1992 avatar nitrodist avatar pacso avatar rafaltrojanowski avatar robinbortlik avatar royalicing avatar sildur avatar supertinou avatar tawan avatar thibaut avatar victorbueno avatar zmokizmoghi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

validates_overlap's Issues

Using ActiveRecord enum column in overlap scope option

Hey @robinbortlik, just discovered a possible bug. It occurs, when one of the columns specified in the scope option is an ActiveRecord enum. Value from the model (which is a string/symbol) is not being converted to the corresponding integer, so SQL casts that string into integer, which will always be zero, so validations are invalid. Here is the setup code:

class ObjectParameterValue < ActiveRecord::Base
  enum kind: [:contract, :fact, :draft]

  validates :valid_from, :valid_until, overlap: { 
    exclude_edges: ['valid_from', 'valid_until'], 
    scope: ['object_parameter_id', 'resource_id', 'resource_type', 'kind']
  }
end

Here is the generated SQL, when kind: :fact

SELECT  1 AS one FROM `object_parameter_values` 
WHERE ((object_parameter_values.valid_until IS NULL OR object_parameter_values.valid_until > '2002-11-05') 
AND (object_parameter_values.valid_from IS NULL OR object_parameter_values.valid_from < '2003-12-01') 
AND object_parameter_values.object_parameter_id  = 1 
AND object_parameter_values.resource_id  = 613 
AND object_parameter_values.resource_type  = 'Place' 
AND object_parameter_values.kind  = 'fact')

In the last line, you can see that condition is object_parameter_values.kind = 'fact', when it should be object_parameter_values.kind = 1

There are none configuration options for this, so I suspect this must be a bug either in ActiveRecord itself, or somewhere in this library. What do you think?

Allow override of validation

Hello,

This gem is great. I was curious if it's possible to use this gem outside of a model validation context so that I could optionally bypass (for example, if a user wants to book a conflicting appointment anyways). I assume I could probably set the model.validation code with an :if statement to run or not. But I was thinking of having a custom controller #validate action that would just return true or false in which I could render in the view back to the user without having to try and actually save records.

Missing value while forming the SQL prepared statement

I'm seeing an error that is intriguing me, doesn't seem to happen all the time.

I have my overlap conditions defined as:

  scope :active, (-> { where(deleted: false, available: true) })

  validates :starttime, :endtime, overlap: {
    query_options: { active: nil },
    scope: :user_id,
    exclude_edges: ["starttime", "endtime"],
    load_overlapped: true,
    message_title: 'overlap',
    message_content: 'overlaps with another stop',
  }

And I've been getting the following error recently:

ActiveRecord::PreparedStatementInvalid: missing value for :user_id_value in (stops.endtime IS NULL OR stops.endtime > :starts_at_value) AND (stops.starttime IS NULL OR stops.starttime < :ends_at_value) AND stops.user_id  = :user_id_value
  from active_record/sanitization.rb:198:in `block in replace_named_bind_variables'
  from active_record/sanitization.rb:192:in `gsub'
  from active_record/sanitization.rb:192:in `replace_named_bind_variables'
  from active_record/sanitization.rb:126:in `sanitize_sql_array'
  from active_record/sanitization.rb:26:in `sanitize_sql_for_conditions'
  from active_record/relation/where_clause_factory.rb:14:in `build'
  from active_record/relation/query_methods.rb:591:in `where!'
  from active_record/relation/finder_methods.rb:370:in `construct_relation_for_exists'
  from active_record/relation/finder_methods.rb:320:in `exists?'
  from validates_overlap/overlap_validator.rb:52:in `overlapped_exists?'
  from validates_overlap/overlap_validator.rb:21:in `validate'
  ...

I've verified and user_id used on the :scope is never nil (no records like that, and validations protect against that. Also looked into the conditions on overlap_validator and don't see anything wrong. The save statement triggering this runs inside a transaction.

How to get conflicting records?

I would like to reference, as part of the validation error message, which records are conflicting.
Let's say: User attends event x, but is assigned to y in the same slot. In order to make it easier to resolve the conflict, I would want to show information (id, start, enddate, desc) of the record x .
Is this possible?

Support for nil?

How does this gem treat nil values? Ideally I would like it to treat ended_at = nil as until the end of time and started_at = nil as from the beginning of time.

Possibility to add a scope

It would be nice to add a scope to the validation.
Example:
I like to check if there is a collision in Booking, but only look in the accepted booking.

class Booking < ActiveRecord::Base

scope :accepted, where(state: "accepted")

validates :start_date, :end_date, overlap: { scope: :accepted }

Explanation

Hi,

Could you add an explanation of what this does to the readme? Does this exist somewhere else?

Adam

Is it possible that start time is the end time of a previous entry?

I have a schedule where times can start on same time as previous end time. For example i have a meeting from 12:00 to 13:00 then its allowed to schedule a new meeting from 13:00 - x.

I tried to add a meeting from 21:00 to 21:35 this with the following entries but get a overlaps error.

validates :starts_at, :ends_at, :overlap => true

20:00 - 21:00 Meeting One
21:35 - 21:40 Meeting Two

scope with enum symbol causing `undefined method `[]' for nil:NilClass`

# foo.rb
class Foo < ApplicationRecord
  enum day: [:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday]

  validates :start_time, :end_time, overlap: {
    scope: [:day, :another_column]
  }
end

Here's what I got when validation fails:

undefined method `[]' for nil:NilClass
Extracted source (around line #184):

#182 
#183       if is_enum_attribute?(record, attr_name)
*184         value = record.class.defined_enums[attr_name][value]
#185       end
#186 
#187       value
...

I think you need to cast the attr_name to string something like this attr_name.to_s

value = record.class.defined_enums[attr_name][value]

Active Model Errors

Hey,

I'm wondering if this code:

  validates :start_at, :end_at, overlap: { ... }

shouldn't add errors for :start_at and :end_at? Actually it adds error only for :start_at. What do you think?

Should we index these database calls?

I'm using this gem and it works fine. Here is my Model code:

validates :start_time, :end_time, overlap: { scope: "device_id", exclude_edges: ["start_time", "end_time"] }

And here is the SQL it triggers:

SELECT  1 AS one FROM "bookings"  WHERE 
((bookings.end_time IS NULL OR bookings.end_time > '2014-04-11 13:00:00.000000') AND
(bookings.start_time IS NULL OR bookings.start_time < '2014-04-11 16:00:00.000000') AND
bookings.device_id  = 20) LIMIT 1

I just want to know if I should be adding an index in my postgres database that covers start_time, end_time and device_id, or something similar? e.g. something like this:

add_index :bookings, [:device_id, :start_time, :end_time], unique: true

Right behaviour or bug?

Hey,

I'm using your gem in a project, very handy, but i found a behaviour that i don't know if its a feature or a bug.

Let's say you have an app where we have a user parent, that has children. The children users can open and then close multiple 'movements', in this case the scope is the children_id, and a different children can overlap another, but can't overlap themselves.

Ok, i achieve that with something like this:

validates :start_date, :end_date, :overlap => { :scope => 'child_id'}

But if i try to create a movement in a time before my last movement, it says it overlaps, even if i didn't set the end_date yet. For example, i set a movement with start_date at 01/01/2015 10AM and close it at end_date 01/01/2015 11AM, then if for some reason i need to create another movement at 01/01/2015 8AM, with end_date not set, it says start_date overlaps.

It's kind of a ultra-safe behaviour to avoid overlap or a scenario that was not planned before? Any tips how can i achieve that?

Rails 4.1.5 and Ruby 2.1.x

Overwriting an existing time_overlap

I need to allow Users to edit object bookings: Let's say a booking is from 5pm to 10pm... then the user edits it so that now it is from 8pm to midnight. Validates_overlap will block this because it overlaps with the original (but in this case I deliberately want an overwrite). How can I stop validations on self from happening?

A bug if the second object is contained entirely inside one that is already persisted

Hi,

I think there's a bug in the validation. Either that or I misunderstand the intention of the gem.

If I create an appointment object, call it obj1, with a start time of 2014-2-5 12:00 and end time of 2014-2-5 13:00 and save it, everything is fine. If I create a second object, call it obj2, with a start time of 2014-2-5 12:15 and and end time of 2014-2-5 12:45, this second object validates even though it overlaps with the first.

Please let me know if I am clear in what I'm describing. And if this scenario behaves as expected, please help me understand why by updating the documentation.

Accessing overlapped records in validation message

Hi,

Thank you for the gem, its been really useful. One question though.

I saw that we can customise the error message using message_content or message_title. Is there a way I can show details of the overlapped record in the error message. My question is how to display if the error message to be shown as below:

This date overlaps with another event. Please change the date after Oct 15(overlapping event date).

Can you let how to do that? When I tried accessing overlapped_records in message_content, its throwing error.

Thanks once again,
Leena

Start_in date field cannot be nil

It seems like the field containing the from date (start date) has to be present or the validation for overlapping dates will be fired. I assume this is a bug as not all records necessarily need to have a date period specified.

I'm using ruby version 2.2.1 and Rails 4.2.1

Rails 6 scoping deprecation warning

Since Rails 6 using the overlap validation gives a deprecation warning when calling ActiveRecord's find_or_create_by:

DEPRECATION WARNING: Class level methods will no longer inherit scoping from `create` in Rails 6.1. To continue using the scoped relation, pass it into the block directly. To instead access the full set of models, as Rails 6.1 will, use `Model.unscoped`. (called from __pry__ at (pry):19)

To repoduce, suppose we have an ActiveRecord model:

class Model < ApplicationRecord
  validates :starts_at, :ends_at, overlap: true
end

Then calling Model.find_or_create_by(...) or Model.find_or_create_by!(...) results in the deprecation warning. It doesn't matter which attributes are given as long as there isn't a record with those attributes yet and a create is attempted. The warning does not occur when using find_or_initialize_by.

More information about the deprecation warning: rails/rails#35280

Allow Edges seems broken

Hey!

If you allow for edges there might be a weird bug.
If you recreate the exact same object with the same starts_at and ends_at, the save goes through.

Gemspec clearing

Hi @supertinou , I noticed, that you previously did some clearing of gemspec file. I agree with this change and I like it.
But I'm using jeweler gem https://github.com/technicalpickles/jeweler for building and releasing my gems to rubygems, and this library regenerate gemspec everytime. So it put this mess inside. And I would like to ask you. Is it wrong to have it as it is? I'm only asking, because this is something what I'm not sure I understand properly.

Thanks for your answer and explanation.

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.