paulnsorensen / active_subset_validator

Provides subset validation for serialized arrays or sets in Active Record

Home Page:https://github.com/paulnsorensen/active_subset_validator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ActiveSubsetValidator

Code Climate Dependency Status Gem Version Build Status Coverage Status

Provides subset validation for serialized arrays or sets in Active Record. Checks whether given values for a serialized array or set are a subset of a given array, set or proc against which to validate.

Installation

Add this line to your application's Gemfile:

gem 'active_subset_validator'

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_subset_validator

Usage

Add to your models like so:

    class Article < ActiveRecord::Base
      validates :categories, subset: { of: %w(foo bar baz ) }
    end

The :of parameter may contain an Array or Set or a Proc or lambda that returns either type.

The validator in this gem is a subclass ActiveModel::EachValidator so you should have all the nifty options to pass to the validates method as well.

    class Comment < ActiveRecord::Base
      attr_accessible :content, :tags,
      validates :tags,
        subset: { of: %w(some list of strings) },
        if: ->(comment) { comment.some_method? },
        allow_nil: false
    end

Testing

You may write your tests similar to the following to ensure that the subset validator is working. Here's an example using FactoryGirl and RSpec:

    it "ensures times_taken contains only values within the proper set" do
     med = build(:medication)
     med.times_taken = %w(as_needed breakfast lunch dinner bedtime)
     med.should have(:no).errors_on(:times_taken)
     med.times_taken << "midnight"
     med.errors_on(:times_taken).should include("is not a subset of the list")
    end

Alternatively, if you use shoulda-matchers, you could do this:

    it { should allow_value(%w(good-val-0 good-val-1)).for(foo) }
    it { should_not allow_value(%w(bad-val-0 bad-val-1)).for(:foo) }

Contributing

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

Bitdeli Badge

About

Provides subset validation for serialized arrays or sets in Active Record

https://github.com/paulnsorensen/active_subset_validator

License:MIT License


Languages

Language:Ruby 100.0%