Module: DataMapper::Is::AwesomeSet
- Defined in:
- lib/dm-is-awesome_set.rb
Overview
Thanks for looking into dm-is-awesome_set. What makes it so awesome? Well, the fact that it actually works. At least I think it does. Give it a whirl, and if you come across any bugs let me know (check the readme file for information). Most of what you will be concerned with is the move method, though there are some other helper methods for selecting nodes. The way you use it in your model is like so:
def ModelName
include DataMapper::Resource
# ... set up your properties ...
is :awesome_set, :scope => [:col1, :col2], :child_key => [:parent_id]
end
Note that scope is optional, and :child_key’s default is [:parent_id]
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Instance Method Summary collapse
-
#is_awesome_set(options = {}) ⇒ Object
Available options for is awesome_set: :scope => array of keys for scope (default is []) :child_key => array of keys for setting the parent-child relationship (default is [:parent_id]).
Instance Method Details
#is_awesome_set(options = {}) ⇒ Object
Available options for is awesome_set:
:scope => array of keys for scope (default is [])
:child_key => array of keys for setting the parent-child relationship (default is [:parent_id])
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dm-is-awesome_set.rb', line 28 def is_awesome_set(={}) extend DataMapper::Is::AwesomeSet::ClassMethods include DataMapper::Is::AwesomeSet::InstanceMethods opts = () [:child_key, :scope].each {|var| raise "#{var} must be an Array" unless opts[var].is_a?(Array)} property :parent_id, Integer, :min => 0, :writer => :protected property :lft, Integer, :writer => :private, :index => true property :rgt, Integer, :writer => :private, :index => true class_opts = {:model => self.name, :child_key => opts[:child_key], :order => [:lft.asc] } belongs_to :parent, class_opts has n, :children, class_opts before :save do move_without_saving(:root) if lft.nil? #You don't want to use new_record? here. Trust me, you don't. end end |