The project is in a healthy, maintained state
Like with_options but merges :if and :unless conditions. Convenient for Active Model or Active Record validations and callbacks.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

ActiveModel::WithConditions

Adds with_conditions to Rails apps, or other projects using Active Model or Active Record.

with_conditions is like Active Support's with_options, but only supports :if and :unless, and it merges these.

So you can do:

with_conditions(if: :feature_x_is_on?) do
  validate :free_plans_must_have_x, if: :free_plan?
end

It supports all forms of :if and :unless – symbols, lambdas (with or without a block argument), and arrays of the same.

Compared to with_options

This improves on Active Support's with_options, where the inner :if would overwrite the outer one, and you'd need to work around it with something like:

with_options(unless: -> { !feature_x_is_on? }) do
  validate :free_plans_must_have_x, if: :free_plan?
end

You can still use with_options for other things (though this gem's author only ever uses it for validation and callback conditions). You could even combine them:

with_conditions(if: :feature_x_is_on?) do
  with_options(presence: true) do
    validates :x
  end
end

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add activemodel-with_conditions

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install activemodel-with_conditions

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome at https://github.com/henrik/activemodel-with_conditions.

License

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