Coder Social home page Coder Social logo

cerbero's Introduction

Cerbero

Uniqueness validation does not guarantee uniqueness. Only the database constraints do this. But there is a flaw - when database constraints fail, an exception is thrown, and even when we catch it, the validation error is not set.

This gem solves the issue.

It catches the database constraints exception, revalidates and returns false from save. To use it, you should set both validation and database constraint. Then even if uniqueness validation mistakenly let you in, it will break the database constraint, then revalidate and the validation will tell you the truth.

Installation

Add this line to your application's Gemfile:

gem 'cerbero'

Usage

Say you need the uniqueness of email inside the company_id. In your model you should say:

validates :email, uniqueness: {scope: :company_id}

and in your migration:

add_index :users, [:email, :company_id], unique: true

Important: you should add both validation and db constraint. If you don't supply validation, you'll have save returning false, yet no validation errors will be displayed, which is a strange and obscure behavior.

Then if the gem is required, it makes the stuff work so that when the uniqueness validtion lets you in, but the database constraint then fails, save returns false, then uniqueness validation is re-run and the execution flow goes like the uniqueness validation worked perfectly.

Contributing

Use it! Provide me the issues if found. If you are really willing to provide some code:

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Contributors

TODO:

  • Apply a cleaner pattern to test via multiple databases.
  • Add travis-ci - it's broken for some reason.
  • Extract the save monkey-patch to module and apply the alias_method_chain pattern to avoid possible collisions.
  • Add an option to apply the gem per-model basis.

cerbero's People

Contributors

dmitriy-kiriyenko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

andrykonchin

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.