Module: DmSvn::Svn::Categorized::ClassMethods

Defined in:
lib/dm-svn/svn/categorized.rb

Instance Method Summary collapse

Instance Method Details

#get(path_or_id, get_parent = false) ⇒ Object

Add get_parent argument. If true and a path is not found for the model, try to find path in parent model (if any).



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dm-svn/svn/categorized.rb', line 94

def get(path_or_id, get_parent = false)
  if path_or_id.is_a?(String)
    i = get_by_path(path_or_id)
    if i || !get_parent
      return i 
    else # if get_parent
      category_model = Object.const_get(@svn_category_model)
      return nil if category_model == self.class
      category_model.get_by_path(path_or_id)
    end
  else
    super(path_or_id)
  end
end

#get_by_path(value) ⇒ Object

Get by path, which gets parent (possibly recursively) first.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dm-svn/svn/categorized.rb', line 76

def get_by_path(value)
  value = value[1..-1] while value[0..0] == "/"
  ary = value.split("/")
  immediate = ary.pop
  parent = ary.join("/")
  
  if parent.blank?
    first(:svn_name => immediate, "#{self.svn_category}_id".to_sym => nil)
  else
    category_model = Object.const_get(self.svn_category_model)
    category = category_model.get_by_path(parent)
    return nil if category.nil?
    first(:svn_name => immediate, "#{self.svn_category}_id".to_sym => category.id)
  end
end