Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Dynamic, Active-Record based business rules engine
branch: master
Failed to load latest commit information.
db/migrate Revert "Make parameter/evaluator keys into first class DB columns"
lib Bump version to 0.1.2
spec
.gitignore
.rspec Gem cleanup
.travis.yml
Gemfile
Gemfile.lock
MIT-LICENSE Initial commit
README.md Allow evaluators to also cast rhs
Rakefile
rules.gemspec

README.md

Rules

build status Code Climate

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"

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"
      type: :string   # see Rules::Parameters::Parameter::VALID_TYPES for a full list
    }
  })
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 = "morbo@example.com"
order.rules_pass?(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

However, rules are defined using keys and values, so you can easily use your own custom solution.

rule = Rules::Rule.new(lhs_parameter_key: 'day_of_week', evaluator_key: 'equals', rhs_parameter_raw: 'Sunday')
rule.lhs_parameter_value
=> 'Sunday' # (or the current day of week)
rule.evaluate
=> true

Order.has_rule_attributes(customer_email: { name: "Customer's email address" })
order = Order.new
rule_set = Rules::RuleSet.new(source: order)

rule = Rules::Rule.new(rule_set: rule_set, lhs_parameter_key: 'customer_email', evaluator_key: 'matches', rhs_parameter_raw: 'example.com$')
rule.lhs_parameter_value
=> nil  # we didn't pass anything in
rule.rhs_parameter_value
=> /example.com$/  # this is a Regexp class
rule.evaluate(customer_email: 'john@example.com')
=> true
rule.evaluate(customer_email: 'john@example.net')
=> false

Configuration

You can add an initializer to configure default options.

Rules.configure do |config|
  config.errors_are_false           = true  # return false if an evaluator raises an error (true by default)
  config.missing_attributes_are_nil = true  # return nil when a value is not passed for an attribute parameter
end

Default Constants

TODO

Default Evaluators

TODO

Something went wrong with that request. Please try again.