Skip to content

Instantly share code, notes, and snippets.

@rubysolo
Created January 9, 2012 19:46
Show Gist options
  • Save rubysolo/1584568 to your computer and use it in GitHub Desktop.
Save rubysolo/1584568 to your computer and use it in GitHub Desktop.
AR Criteria
class Invoice < ActiveRecord::Base
has_many :line_items
end
class LineItemTemplate < ActiveRecord::Base
has_many :criteria
end
class Criteria < ActiveRecord::Base
belongs_to :line_item_template
# would have attributes something like:
# source_field (attribute on invoice to examine e.g. :total)
# comparison (equal, less_than, greater_than)
# value (number or expression)
end
# Here's an example of ANDed criteria
t = LineItemTemplate.create(:description => "overweight shipping charge to Canada", :require_all_criteria => true)
t.criteria.create(
:source_field => "total_weight",
:comparison => "greater_than",
:value => "100"
)
t.criteria.create(
:source_field => "destination_country",
:comparison => "equal",
:value => "CA"
)
# If I was to build this, I'd probably make criteria acts_as_nested_set to model arbitrary (condition AND (condition OR condition)) stuff...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment