AR Rules

A Ruby gem engine that allows you to add customizable business rules to any ActiveRecord model. Rules integrates with ActiveAdmin to make it trivial to allow admin users to create rules on the fly for your models.

Installation

Add it to your Gemfile:

gem "rules", :github => 'azach/rules'

Update your schema:

rake rules:install:migrations
rake db:migrate

to create the required tables to store your rules.

Setting Up Rules

To use rules on a model, include Rules::HasRules. You can also optionally define any attributes that are available for that model using has_rule_attributes.

This will allow the user to build rules against this attribute. For example, you may want to allow users to build rules against the email address in the order. In this case, your model would look like:

class Order < ActiveRecord::Base
  include Rules::HasRules

  has_rule_attributes({
    customer_email: {
      name: "customer email address"
    }
  })
end

To evaluate a set of a rules, use the rules_pass? method on the instance. You may also pass the values of the attributes that you allowed users to define rules against at this point.

For example:

order = Order.new
order.email_address = "[email protected]"
order.evaluate(customer_email: order.email_address)

Defining Rules

Rules are meant to be defined by business users using an admin interface. For this reason, the gem provides integration with ActiveAdmin to make this easier.

There are two helper methods you can use, one for your show action and one for your form.

For the show action:

show_rules

For the form action:

f.has_rules

This will give you something like:

ActiveAdmin form for editing rules

Default Constants

TODO

Default Evaluators

TODO