OSDN Git Service

337daa4a297528d1a94082a1860344a18c87dc16
[pettanr/pettanr.git] / lib / peta / content.rb
1 module Peta
2   class Content < Item
3     self.abstract_class = true
4     
5     # Dynamic ClassMethods
6     
7     def self._content?
8       self.my_manifest.content_model
9     end
10     
11     def self._owner_type
12       self.my_manifest.owner_type
13     end
14     
15     def self._owner_model
16       t = self.my_manifest.tree['owner']
17       return nil unless t
18       t.parent_model 
19     end
20     
21     def self._owner_column
22       self.owner_type.to_s + '_id'
23     end
24     
25     def self.load_manifest
26       super
27       f = self._content?
28       define_singleton_method("content?") do 
29         f
30       end
31       t = self._owner_type
32       define_singleton_method("owner_type") do 
33         t
34       end
35       m = self._owner_model
36       define_singleton_method("owner_model") do 
37         m
38       end
39       define_singleton_method("parent_model") do |tree_name|
40         self.my_manifest.tree[tree_name].parent_model
41       end
42       o = self._owner_column
43       define_singleton_method("owner_column") do 
44         o
45       end
46     end
47     
48     # ClassMethods
49     
50     def self.operator operators
51       return nil unless self.owner_type
52       operators.__send__ self.owner_type
53     end
54     
55     def self.public_list_where
56       ''
57     end
58     
59     def self.edit content_id, operators
60       content = self.find content_id, self.show_opt
61       raise ActiveRecord::Forbidden unless content.own?(operators)
62       content
63     end
64     #InstanceMethods
65     
66     def owner_model
67       self.class.owner_model ? self.__send__(self.class.owner_model.item_name) : self
68     end
69     
70     def visible? operators
71       super
72       return true if owner_model.own?(operators)
73       true
74     end
75     
76     def own? operators
77       operator = self.class.operator operators
78       return false unless operator
79       owner_model.attributes[self.class.owner_column] == operator.id
80     end
81     
82   end
83 end
84