Validate Active Records, one attribute at a time.
Ruby HTML CSS JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.circleci
lib
spec
.gitignore
Gemfile
Gemfile.lock
MIT-LICENSE
README.md
Rakefile
valid_attribute.gemspec

README.md

valid_attribute Circle CI Code Climate

Let's say you have:

class Product < ActiveRecord::Base
  belongs_to :company

  validates :company, presence: true
  validates :name, format: { with: /\A[a-zA-Z]+\z/ }
  validates :name, uniqueness: { scope: :company }
  validates :legacy_code, format: { with: /\A[a-zA-Z]+\z/ }
end

This gem lets you test the validity of each attribute independentely:

company = Company.new
product = Product.new(company: company, name: "heyo")

# Test only one attribute:
product.valid_attribute?(:company)      # => true
product.valid_attribute?(:name)         # => true
product.valid_attribute?(:legacy_code)  # => false

# Test several attributes at once, like a boss:
product.valid_attribute?(:company, :name)               # => true
product.valid_attribute?(:company, :name, :legacy_code) # => false

# Wow, you can even filter down to a specific validator:
product.valid_attribute?(name: :format)                     # => true
product.valid_attribute?(name: [:format, :uniqueness])      # => true
product.valid_attribute?(name: :format, company: :presence) # => true

Time to go crazy:

gem 'valid_attribute', github: 'kevinbongart/valid_attribute'