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}
22 self.visible = 0 if self.visible.blank?
25 def overwrite operators
26 return false unless operators.author
27 self.author_id = operators.author.id
31 roles = [roles] unless roles.respond_to?(:each)
32 au = Scroll.get_author_from_roles roles
33 return false unless au
34 self.author_id == au.id
37 def visible? operators
38 return false unless super
42 def scroll_panels_count
43 ScrollPanel.where(['scroll_panels.scroll_id = ?', self.id]).count
47 'scrolls.updated_at desc'
55 {:scroll_panels => {:panel => {}}, :author => {} }
58 def self.list_json_opt
59 {:include => {:scroll_panels => {:include => {:panel => {}}}, :author => {}}}
62 def self.show sid, operators
64 opt.merge!(Scroll.show_opt)
65 res = Scroll.find(sid, opt)
66 raise ActiveRecord::Forbidden unless res.visible?(operators)
72 opt.merge!(Scroll.show_opt)
73 res = Scroll.find(sid, opt)
74 raise ActiveRecord::Forbidden unless res.own?(au)
79 {:include => {:scroll_panels => {:panel => {}}, :author => {}}}
82 def self.show_json_opt
83 {:include => {:scroll_panels => {:include => {:panel => {}}}, :author => {}}}
86 def self.visible_count
87 Scroll.count 'visible > 0'
90 def destroy_with_scroll_panel
93 self.scroll_panels.each do |scroll_panel|
94 raise ActiveRecord::Rollback unless scroll_panel.destroy
96 raise ActiveRecord::Rollback unless self.destroy