No commit activity in last 3 years
No release in over 3 years
Provides subset validation for serialized arrays or sets in Active Record
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

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