1 class Scroll < Pettanr::Item
2 has_many :scroll_panels
5 validates :title, :presence => true, :length => {:maximum => 100}
6 validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
7 validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
21 def self.valid_encode_columns
26 self.visible = 0 if self.visible.blank?
29 def overwrite operators
30 return false unless operators.author
31 self.author_id = operators.author.id
35 roles = [roles] unless roles.respond_to?(:each)
36 au = Scroll.get_author_from_roles roles
37 return false unless au
38 self.author_id == au.id
41 def visible? operators
42 return false unless super
46 def scroll_panels_count
47 ScrollPanel.where(['scroll_panels.scroll_id = ?', self.id]).count
51 'scrolls.updated_at desc'
59 {:scroll_panels => {:panel => {}}, :author => {} }
62 def self.list_json_opt
63 {:include => {:scroll_panels => {:include => {:panel => {}}}, :author => {}}}
66 def self.show sid, operators
68 opt.merge!(Scroll.show_opt)
69 res = Scroll.find(sid, opt)
70 raise ActiveRecord::Forbidden unless res.visible?(operators)
76 opt.merge!(Scroll.show_opt)
77 res = Scroll.find(sid, opt)
78 raise ActiveRecord::Forbidden unless res.own?(au)
83 {:include => {:scroll_panels => {:panel => {}}, :author => {}}}
86 def self.show_json_opt
87 {:include => {:scroll_panels => {:include => {:panel => {}}}, :author => {}}}
90 def self.visible_count
91 Scroll.count 'visible > 0'
94 def destroy_with_scroll_panel
97 self.scroll_panels.each do |scroll_panel|
98 raise ActiveRecord::Rollback unless scroll_panel.destroy
100 raise ActiveRecord::Rollback unless self.destroy