Giter Club home page Giter Club logo

active_model_validates_intersection_of's Introduction

Active Model Validates Intersection Of

Gem Version Maintainability Test Coverage Coverage Status Travis

A custom validation for Active Model that check if an array is included in another one.

Identical to the method validates_inclusion_of from ActiveModel but for array comparison.

Consider an User model that have some set of "default" permissions.

class User < ActiveRecord::Base
  DEFAULT_PERMISSION = ["read", "write", "share"]
  validates_intersection_of :permission, in: DEFAULT_PERMISSION
end

If you want to validate your user based on an array:

user = User.new(permission: ["read", "share"])
user.valid? #true

user = User.new(permission: ["read", "admin"])
user.valid? #false

This active model custom validation is for you!

Installation

Add this line to your application's Gemfile:

gem "active_model_validates_intersection_of"

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_model_validates_intersection_of

If your framework doesn't auto require gems, don't forget to do it after require of active_model gem.

require "active_model"
require "active_model_validates_intersection_of"

Usage

Parameters

  • :in - Parameter is required. (Supports: Array, proc and lambda)
  • :within - Optional: A synonym(alias) for :in
  • :message - Optional: Specifies a custom error message

Validation

You can use the intersection validation in three differents ways: (Feel free to use what you liked more :))

class User < ActiveRecord::Base
  DEFAULT_PERMISSION = ["read", "write", "share"]

  # Using the alias validates_intersection_of
  validates_intersection_of :permission, in: DEFAULT_PERMISSION, message: "invalid permission"
  validates_intersection_of :permission, within: DEFAULT_PERMISSION, message: "invalid permission"

  # proc and lambda support
  validates_intersection_of :permission, in: proc { DEFAULT_PERMISSION }, message: "invalid permission"
  validates_intersection_of :permission, in: lambda { |l| DEFAULT_PERMISSION }, message: "invalid permission"

  # Using the validator explicit:
  validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:permission], in: DEFAULT_PERMISSION
  validates_with ActiveModelValidatesIntersectionOf::Validator, attributes: [:permission], within: DEFAULT_PERMISSION

  #Using the validator implicit:
  validates :permission, intersection: { in: DEFAULT_PERMISSION, message: "invalid permission" }
  validates :permission, intersection: { within: DEFAULT_PERMISSION, message: "invalid permission" }

end

License

The gem is available as open source under the terms of the MIT License.

Contributing

First of all, thank you for wanting to help!

  1. Fork it.
  2. Create a feature branch - git checkout -b more_magic
  3. Add tests and make your changes
  4. Check if tests are ok - rake spec
  5. Commit changes - git commit -am "Added more magic"
  6. Push to Github - git push origin more_magic
  7. Send a pull request! ❤️

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.