OSDN Git Service

work
[pettanr/pettanr.git] / lib / content.rb
1 class Pettanr::Content < Pettanr::Item
2   self.abstract_class = true
3   # ClassMethods
4   
5   def self.owner_model
6     nil
7   end
8   
9   def self.owner_type
10     :author
11   end
12   
13   def self.content?
14     true
15   end
16   
17   def self.operator operators
18     case self.owner_type
19     when :author
20       operators.author
21     when :artist
22       operators.artist
23     else
24       nil
25     end
26   end
27   
28   def self.owner_column
29     self.owner_type.to_s + '_id'
30   end
31   
32   def self.edit content_id, operators
33     content = self.find content_id, self.show_opt
34     raise ActiveRecord::Forbidden unless content.own?(operators)
35     content
36   end
37   
38   #InstanceMethods
39   
40   def owner_model
41     self.class.owner_model ? self.__send__(self.class.owner_model.item_name) : self
42   end
43   
44   def visible? operators
45     super
46     return true if owner_model.own?(operators)
47     true
48   end
49   
50   def own? operators
51     operator = self.class.operator operators
52     return false unless operator
53     owner_model.attributes[self.class.owner_column] == operator.id
54   end
55   
56 end
57