X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fmodels%2Fstory.rb;h=2d09d53a080cd4b537adb6a2066321092e77d277;hb=31124ac1df4b11438308bb83525753b3e40e355a;hp=3a7f329d9af16b71ff0a296787b812d75f9dabc0;hpb=a71270cfc4ab2491edc15a281b1ea3780c96f4da;p=pettanr%2Fpettanr.git diff --git a/app/models/story.rb b/app/models/story.rb index 3a7f329d..2d09d53a 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -1,159 +1,90 @@ #ストーリー -class Story < ActiveRecord::Base - belongs_to :author - belongs_to :panel +class Story < Peta::Binder + load_manifest + has_many :story_sheets belongs_to :comic - validates :comic_id, :presence => true, :numericality => true, :existence => true - validates :panel_id, :presence => true, :numericality => true, :existence => true - validates :author_id, :presence => true, :numericality => true, :existence => true + validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false} + validates :title, :presence => true, :length => {:maximum => 100} + validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1} validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0} def supply_default + self.comic_id = nil + self.visible = 0 if self.visible.blank? self.t = nil end - def overwrite au - return false unless au - self.author_id = au.id + def overwrite end - def own? au - return false unless au.is_a?(Author) - self.author_id == au.id + def visible? operators + return false unless super + self.visible > 0 end - def visible? au - if au == nil - return false if MagicNumber['run_mode'] == 1 - elsif au.is_a?(Author) - return true if self.comic.own?(au) - else - return false - end - self.comic.visible? au + def disp_t + self.t + 1 end - def self.default_panel_size - 30 + def disp_t_by_text + I18n.t('stories.show.t', :t => self.disp_t) end - def self.max_panel_size - 200 + def title_with_t + self.disp_t_by_text + ':' + self.title end - def self.offset cnt, prm = nil - offset = prm.to_i - offset = cnt - 1 if offset >= cnt - offset = cnt - offset.abs if offset < 0 - offset = 0 if offset < 0 - offset + def story_sheets_count + StorySheet.where(['story_sheets.story_id = ?', self.id]).count end - def self.panel_count cnt, prm = self.default_panel_size - count = prm.to_i - count = self.max_panel_size if count > self.max_panel_size - count = self.default_panel_size if count < 1 - count + def self.public_list_where + 'stories.visible > 0' end - def self.play_list comic, author, offset = 0, limit = Story.default_panel_size - opt = {} - opt.merge!(Story.list_opt) - opt.merge!({:offset => offset, :limit => limit}) if limit > 0 - opt.merge!({:conditions => ['stories.comic_id = ?', comic.id], :order => 'stories.t'}) - Story.find(:all, opt) + def self.list_order + 'stories.updated_at desc' end def self.list_opt - {:include => { - :author => {}, - :comic => { - :author => {} - }, - :panel => { - :author => {}, - :panel_pictures => {:picture => {:artist => {}, :license => {}}}, - :speech_balloons =>{:balloons => {}, :speeches => {}} - } - }} + {:comic => {:author => {}} } end - def self.list_json_opt - {:include => { - :author => {}, + def self.by_author_list_includes + { :comic => { :author => {} - }, - :panel => { - :author => {}, - :panel_pictures => {:picture => {:artist => {}, :license => {}}}, - :speech_balloons =>{:balloons => {}, :speeches => {}} } - }} - end - - def self.mylist au, page = 1, page_size = Author.default_story_page_size - opt = {} - opt.merge!(Story.list_opt) - opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0 - opt.merge!({:conditions => ['stories.author_id = ?', au.id], :order => 'stories.updated_at desc'}) - Story.find(:all, opt) + } end - def self.show sid, au - opt = {} - opt.merge!(Story.show_opt) - res = Story.find sid, opt - raise ActiveRecord::Forbidden unless res.visible?(au) - res - end - - def self.edit sid, au - opt = {} - opt.merge!(Story.show_opt) - res = Story.find sid, opt - raise ActiveRecord::Forbidden unless res.own?(au) - res + def self.list_json_opt + {:include => {:comic => {:include => {:author => {}}} }} end def self.show_opt - {:include => { - :author => {}, - :comic => { - :author => {} - }, - :panel => { - :author => {}, - :panel_pictures => {:picture => {:artist => {}, :license => {}}}, - :speech_balloons =>{:balloons => {}, :speeches => {}} - } - }} - end - - def elements - self.panel.elements + {:include => {:comic => {:author => {}} }} end - def story_as_json au - panel_include = if self.panel and self.panel.visible?(au) - {:include => {:author => {}}, :methods => :elements} - else - {:include => {:author => {}}} - end - self.to_json({:include => {:comic => {:include => {:author => {}}}, :author => {}, :panel => panel_include}}) + def self.show_json_opt + {:include => {:comic => {:include => {:author => {}}} }} end - def self.list_as_json_text ary, au - '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']' + def self.visible_count + Story.count 'visible > 0' end - def self.licensed_pictures stories - r = {} - stories.each do |story| - r.merge! story.panel.licensed_pictures + def destroy_with_story_sheet + res = false + Story.transaction do + self.story_sheets.each do |story_sheet| + raise ActiveRecord::Rollback unless story_sheet.destroy + end + raise ActiveRecord::Rollback unless self.destroy + res = true end - r + res end def self.new_t comic_id @@ -226,17 +157,21 @@ class Story < ActiveRecord::Base end end - def allow? - c = self.comic - pl = self.panel - c and c.own?(self.author) and pl and pl.usable?(self.author) + def allow? operators + return nil if self.comic_id == nil + self.comic.own?(operators) end - def store old_t = nil + def store operators, old_t = nil res = false - raise ActiveRecord::Forbidden unless self.allow? Story.transaction do - self.rotate old_t + case self.allow? operators + when true + self.rotate old_t + when false + raise ActiveRecord::Forbidden + else + end res = self.save raise ActiveRecord::Rollback unless res res = Story.validate_t(self) @@ -249,10 +184,13 @@ class Story < ActiveRecord::Base end def destroy_and_shorten + res = false Story.transaction do Story.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t]) - raise ActiveRecord::Rollback unless self.destroy + raise ActiveRecord::Rollback unless self.destroy_with_story_sheet + res = true end + res end