Initial models.
[ecs.git] / app / models / ressource.rb
blob0a741dda2e22c48f29425e3fea5463ad060f9d2b
1 # Copyright (C) 2007, 2008, 2009, 2010 Heiko Bernloehr (FreeIT.de).
2
3 # This file is part of ECS.
4
5 # ECS is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of
8 # the License, or (at your option) any later version.
9
10 # ECS is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Affero General Public License for more details.
14
15 # You should have received a copy of the GNU Affero General Public
16 # License along with ECS. If not, see <http://www.gnu.org/licenses/>.
19 class Ressource < ActiveRecord::Base
20   has_many :messages, :dependent => :destroy
21   validates_presence_of :namespace, :ressource 
22   after_save :rebuild_routes
23   after_destroy :rebuild_routes
25   named_scope :list, :order => "namespace, ressource ASC"
27   def self.validates_ressource_path(namespace, ressource)
28     r = Ressource.find_by_namespace_and_ressource(namespace, ressource)
29     raise(Ecs::InvalidRessourceUriException, "*** ressource uri error ***") unless r
30     if namespace.blank? or r.namespace.blank?
31       raise Ecs::InvalidRessourceUriException, "*** namespace error ***"
32     end
33     if ressource.blank? or r.ressource.blank?
34       raise Ecs::InvalidRessourceUriException, "*** ressource name error ***"
35     end
36     return r
37   end
39   def events?
40     self.events.blank? ? false : true
41   end
43 private
45   def rebuild_routes
46     logger.info("rebuild routes")
47     ActionController::Routing::Routes.reload!
48   end
50 end