belongs_to :panel
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 :panel_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
def supply_default
+ self.comic_id = nil
+ self.panel_id = nil
self.t = nil
end
self.author_id = au.id
end
- def own? author
- return false unless author
- self.author_id == author.id
+ def own? roles
+ roles = [roles] unless roles.respond_to?(:each)
+ au = Story.get_author_from_roles roles
+ return false unless au
+ self.author_id == au.id
end
- def self.show sid, au = nil
- res = Story.find sid
- if au
- raise ActiveRecord::Forbidden unless res.own?(au)
+ def visible? roles
+ if MagicNumber['run_mode'] == 0
+ return false unless guest_role_check(roles)
+ else
+ return false unless reader_role_check(roles)
end
+ return true if self.comic.own?(roles)
+ self.comic.visible? roles
+ end
+
+ def self.default_panel_size
+ 30
+ end
+
+ def self.max_panel_size
+ 200
+ 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
+ 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
+ end
+
+ def self.default_page_size
+ 25
+ end
+
+ def self.max_page_size
+ 100
+ end
+
+ def self.page prm = nil
+ page = prm.to_i
+ page = 1 if page < 1
+ page
+ end
+
+ def self.page_size prm = self.default_page_size
+ page_size = prm.to_i
+ page_size = self.max_page_size if page_size > self.max_page_size
+ page_size = self.default_page_size if page_size < 1
+ page_size
+ end
+
+ def self.play_list_where cid
+ ['stories.comic_id = ?', cid]
+ end
+
+ def self.list_where
+ 'comics.visible > 0'
+ end
+
+ def self.mylist_where au
+ ['stories.author_id = ?', au.id]
+ end
+
+ def self.himlist_where au
+ ['stories.author_id = ? and comics.visible > 0', au.id]
+ end
+
+ def self.play_list comic, author, offset = 0, limit = Story.default_panel_size
+ Story.where(self.play_list_where(comic.id)).includes(Story.list_opt).order('stories.t').offset(offset).limit(limit)
+ end
+
+ def self.list page = 1, page_size = self.default_page_size
+ Story.where(self.list_where()).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
+ end
+
+ def self.mylist au, page = 1, page_size = Author.default_story_page_size
+ Story.where(self.mylist_where(au)).includes(Story.list_opt).order('stories.updated_at desc').offset((page -1) * page_size).limit(page_size)
+ end
+
+ def self.himlist au, page = 1, page_size = Author.default_story_page_size
+ Story.where(self.himlist_where(au)).includes(Story.list_opt).order('stories.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(Story.where(self.list_where()).includes(Story.list_opt).count, nil)).page(page).per(page_size)
+ end
+
+ def self.mylist_paginate au, page = 1, page_size = Author.default_story_page_size
+ Kaminari.paginate_array(Array.new(Story.where(self.mylist_where(au)).includes(Story.list_opt).count, nil)).page(page).per(page_size)
+ end
+
+ def self.himlist_paginate au, page = 1, page_size = Author.default_story_page_size
+ Kaminari.paginate_array(Array.new(Story.where(self.himlist_where(au)).includes(Story.list_opt).count, nil)).page(page).per(page_size)
+ end
+
+ def self.list_opt
+ {
+ :author => {},
+ :comic => {
+ :author => {}
+ },
+ :panel => {
+ :author => {},
+ :panel_pictures => {:picture => {:artist => {}, :license => {}}},
+ :speech_balloons =>{:balloon => {}, :speech => {}}
+ }
+ }
+ end
+
+ def self.list_json_opt
+ {:include => {
+ :author => {},
+ :comic => {
+ :author => {}
+ },
+ :panel => {
+ :author => {},
+ :panel_pictures => {:picture => {:artist => {}, :license => {}}},
+ :speech_balloons =>{:balloon => {}, :speech => {}}
+ }
+ }}
+ end
+
+ def self.show sid, roles
+ opt = {}
+ opt.merge!(Story.show_opt)
+ res = Story.find sid, opt
+ raise ActiveRecord::Forbidden unless res.visible?(roles)
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
+ end
+
+ def self.show_opt
+ {:include => {
+ :author => {},
+ :comic => {
+ :author => {}
+ },
+ :panel => {
+ :author => {},
+ :panel_pictures => {:picture => {:artist => {}, :license => {}}},
+ :speech_balloons =>{:balloon => {}, :speech => {}}
+ }
+ }}
+ end
+
+ def elements
+ self.panel.elements
+ 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}})
+ end
+
+ def self.list_as_json_text ary, au
+ '[' + ary.map {|i| i.story_as_json(au) }.join(',') + ']'
+ end
+
+ def self.licensed_pictures stories
+ r = {}
+ stories.each do |story|
+ r.merge!(story.panel.licensed_pictures) if story.panel
+ end
+ r
+ end
+
def self.new_t comic_id
r = Story.max_t(comic_id)
r.blank? ? 0 : r.to_i + 1
end
def allow?
- c = self.comic
- pl = self.panel
- c and c.own?(self.author) and pl and pl.usable?(self.author)
+ return nil if self.comic_id == nil or self.panel_id == nil
+ self.comic.own?(self.author) and self.panel.usable?(self.author)
end
def store old_t = nil
res = false
- raise ActiveRecord::Forbidden unless self.allow?
Story.transaction do
- self.rotate old_t
+ case self.allow?
+ 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)
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
+ res = true
end
- end
-
- def self.default_panel_size
- 30
- end
-
- def self.max_panel_size
- 200
- 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
- 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
- end
-
- def self.list comic, author, offset = 0, limit = Story.default_panel_size
- opt = self.list_opt
- opt.merge!({:conditions => ['stories.comic_id = ?', comic.id], :order => 'stories.t', :offset => offset, :limit => limit})
- Story.find(:all, opt)
- end
-
- def self.list_opt
- {:include => {
- :author => {},
- :comic => {
- :author => {}
- },
- :panel => {
- :author => {},
- :panel_pictures => {:resource_picture => {:artist => {}}},
- :speech_balloons =>{:balloons => {}, :speeches => {}}
- }
- }}
- end
-
- def self.list_json_opt
- {:include => {
- :author => {},
- :comic => {
- :author => {}
- },
- :panel => {
- :author => {},
- :panel_pictures => {:resource_picture => {:artist => {}}},
- :speech_balloons =>{:balloons => {}, :speeches => {}}
- }
- }}
- end
-
- def to_json_list
- self.to_json( :include => {:author => {}, :panels => {:methods => :panel_element}})
+ res
end