#コミック
class Comic < ActiveRecord::Base
- has_many :stories
+ has_many :stories, :order => 't'
belongs_to :author
validates :title, :presence => true, :length => {:maximum => 100}
validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
+ before_validation :valid_encode
+
+ def valid_encode
+ ['title', 'description'].each do |a|
+ next if attributes[a] == nil
+ raise Pettanr::BadRequest unless attributes[a].valid_encoding?
+ end
+ end
def supply_default
self.visible = 0 if self.visible.blank?
self.author_id = au.id
end
- def own? au
- return false unless au.is_a?(Author)
+ def own? roles
+ roles = [roles] unless roles.respond_to?(:each)
+ au = Comic.get_author_from_roles roles
+ return false unless au
self.author_id == au.id
end
- def visible? au
- if au == nil
- return false if MagicNumber['run_mode'] == 1
- elsif au.is_a?(Author)
- return true if self.own?(au)
+ def visible? roles
+ if MagicNumber['run_mode'] == 0
+ return false unless guest_role_check(roles)
else
- return false
+ return false unless reader_role_check(roles)
end
+ return true if self.own?(roles)
self.visible > 0
end
page_size
end
- def self.list page = 1, page_size = self.default_page_size
- opt = {}
- opt.merge!(Comic.list_opt)
- opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
- opt.merge!({:conditions => ['visible > 0'], :order => 'updated_at desc'})
- Comic.find(:all, opt)
+ def self.list_where
+ 'comics.visible > 0'
end
- def self.list_opt
- {:include => {:stories => {:panel => {}}, :author => {}}}
+ def self.mylist_where au
+ ['comics.author_id = ?', au.id]
end
- def self.list_json_opt
- {:include => {:stories => {:include => {:panel => {}}}, :author => {}}}
+ def self.himlist_where au
+ ['comics.author_id = ? and comics.visible > 0', au.id]
+ end
+
+ def self.list page = 1, page_size = self.default_page_size
+ Comic.where(self.list_where()).includes(Comic.list_opt).order('comics.updated_at desc').offset((page -1) * page_size).limit(page_size)
end
def self.mylist au, page = 1, page_size = Author.default_comic_page_size
- opt = {}
- opt.merge!(Comic.list_opt)
- opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
- opt.merge!({:conditions => ['comics.author_id = ?', au.id], :order => 'comics.updated_at desc'})
- Comic.find(:all, opt)
+ Comic.where(self.mylist_where(au)).includes(Comic.list_opt).order('comics.updated_at desc').offset((page -1) * page_size).limit(page_size)
+ end
+
+ def self.himlist au, page = 1, page_size = Author.default_comic_page_size
+ Comic.where(self.himlist_where(au)).includes(Comic.list_opt).order('comics.updated_at desc').offset((page -1) * page_size).limit(page_size)
+ end
+
+ def self.list_paginate page = 1, page_size = self.default_page_size
+ Kaminari.paginate_array(Array.new(Comic.where(self.list_where()).count, nil)).page(page).per(page_size)
+ end
+
+ def self.mylist_paginate au, page = 1, page_size = Author.default_comic_page_size
+ Kaminari.paginate_array(Array.new(Comic.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
+ end
+
+ def self.himlist_paginate au, page = 1, page_size = Author.default_comic_page_size
+ Kaminari.paginate_array(Array.new(Comic.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
+ end
+
+ def self.list_opt
+ {:stories => {}, :author => {} }
end
- def self.show cid, au
+ def self.list_json_opt
+ {:include => {:stories => {}, :author => {}} }
+ end
+
+ def self.show cid, roles
opt = {}
opt.merge!(Comic.show_opt)
res = Comic.find(cid, opt)
- raise ActiveRecord::Forbidden unless res.visible?(au)
+ raise ActiveRecord::Forbidden unless res.visible?(roles)
res
end
end
def self.show_opt
- {:include => {:stories => {:panel => {}}, :author => {}}}
+ {:include => {:stories => {}, :author => {}} }
end
def self.show_json_opt
- {:include => {:stories => {:include => {:panel => {}}}, :author => {}}}
+ {:include => {:stories => {}, :author => {}} }
end
def self.visible_count
res = false
Comic.transaction do
self.stories.each do |story|
- raise ActiveRecord::Rollback unless story.destroy
+ raise ActiveRecord::Rollback unless story.destroy_and_shorten
end
raise ActiveRecord::Rollback unless self.destroy
res = true
res
end
+ def scenario
+ panels.map {|panel|
+ panel.scenario
+ }.join
+ end
+
+ def plain_scenario
+ panels.map {|panel|
+ panel.plain_scenario
+ }.join
+ end
+
end