Created
January 9, 2012 19:46
-
-
Save rubysolo/1584568 to your computer and use it in GitHub Desktop.
AR Criteria
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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