module Selections class Selection < ActiveRecord::Base VERSION = "0.1" HIDDEN_POSITION = 999888777 acts_as_tree validate :name, :existence =>true validates_presence_of :name validates_uniqueness_of :name, :scope => :parent_id validates_uniqueness_of :system_code, :scope => :archived_at validates_format_of :system_code, :with => /^[a-z][a-zA-Z_0-9]*$/, :message => "can only contain alphanumeric characters and '_', not spaces" before_validation :auto_gen_system_code, :on => :create before_validation :disable_system_code_change, :on => :update after_validation :check_defaults default_scope :order => [:position_value, :name] scope :filter_archived_except_selected, lambda { |selected_id| {:conditions => ["archived_at is ? or id = ?", nil, selected_id]} } def to_s name.to_s end def position=(value) self.position_value = value || HIDDEN_POSITION end def disable_system_code_change errors.add(:system_code, "cannot be changed") if system_code_changed? end def position position_value unless position_value == HIDDEN_POSITION end def auto_gen_system_code unless system_code if parent self.system_code = parent.system_code + "_" + name.to_s.underscore.split(" ").join("_").singularize.underscore else self.system_code = name.to_s.underscore.split(" ").join("_").singularize.underscore end end end def check_defaults siblings_with_default_set.update_attribute(:is_default, false) if self.parent && siblings_with_default_set && self.is_default self.is_default = false if archived end def siblings_with_default_set self.parent.children.where(:is_default => true).where("id != #{self.id.to_i}").first end def self.method_missing lookup_code, *options if (scope = Selection.where(:system_code => lookup_code.to_s)) && scope.exists? scope.first elsif (scope = Selection.where(:system_code => lookup_code.to_s.singularize)) && scope.exists? scope.first.children else super end end def archived !!archived_at end def archived=(archived_set) if archived_set self.archived_at = Time.now unless archived_at else self.archived_at = nil end end end end