Guest User

Untitled

a guest
May 20th, 2018
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 0.60 KB | None | 0 0
  1. # encoding: utf-8
  2. class Node
  3.  
  4.   include Mongoid::Document
  5.   include Mongoid::Tree
  6.  
  7.   # Fields
  8.   #
  9.   field :path, type: String
  10.   field :able, type: Hash, default: {}
  11.  
  12.   # Validations
  13.   #
  14.   validates :path, :presence => true, :uniqueness => true
  15.  
  16.   # Callbacks
  17.   #
  18.   after_initialize :setup_abilities
  19.  
  20.   def setup_abilities
  21.     able.keys.each do |ability|
  22.       self.class.send(:define_method, "is_#{ability}?".to_sym) do
  23.         able[ability]
  24.       end
  25.  
  26.       self.class.send(:define_method, "is_#{ability}=".to_sym) do |*arg|
  27.         able[ability] = arg
  28.       end
  29.     end
  30.   end
  31.  
  32. end
Add Comment
Please, Sign In to add comment