OSDN Git Service

t#:
[pettanr/pettanr.git] / lib / peta / content.rb
1 module Peta
2   class Content < Item
3     self.abstract_class = true
4     
5     # Dynamic Methods
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       self.parent_model 
17     end
18     
19     def self._owner_column
20       self._owner_type.to_s + '_id'
21     end
22     
23     def self.load_manifest
24       super
25       # Class Methods
26       ct = self._content?
27       define_singleton_method("content?") do 
28         ct
29       end
30       ot = self._owner_type
31       define_singleton_method("owner_type") do 
32         ot
33       end
34       om = self._owner_model
35       define_singleton_method("owner_model") do 
36         om
37       end
38       oc = self._owner_column
39       define_singleton_method("owner_column") do 
40         oc
41       end
42       # Instance Methods
43     end
44     
45     # Class Methods
46     
47     def self.operator operators
48       return nil unless self.owner_type
49       operators.__send__ self.owner_type
50     end
51     
52     def self.public_list_where
53       ''
54     end
55     
56     def self.edit content_id, operators
57       content = self.find content_id, self.show_opt
58       raise ActiveRecord::Forbidden unless content.own?(operators)
59       content
60     end
61     
62     # Instance Methods
63     
64     def owner_model
65       self.class.owner_model ? self.__send__(self.class.owner_model.item_name) : self
66     end
67     
68     # super return if my item
69     def visible? operators
70       super
71       return nil if owner_model.own?(operators)
72       true
73     end
74     
75     def own? operators
76       operator = self.class.operator operators
77       return false unless operator
78       owner_model.attributes[self.class.owner_column] == operator.id
79     end
80     
81   end
82 end
83