#コミック
-class Comic < Pettanr::Content
- has_many :stories, :order => 't'
+class Comic < Peta::Binder
+ load_manifest
+ has_many :comic_stories
belongs_to :author
validates :title, :presence => true, :length => {:maximum => 100}
validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
- @@valid_encode_columns += ['title', 'description']
- @@visible_count_options = {:conditions => 'visible > 0'}
-
- def supply_default
- self.visible = 0 if self.visible.blank?
- end
-
- def overwrite au
- return false unless au
- self.author_id = au.id
- end
-
- def visible? operators
- super
- self.visible > 0
- end
-
- def comic_stories_count
- Story.where(['stories.comic_id = ?', self.id]).count
+ scope :find_index, -> do
+ where(arel_table[:visible].gt 0)
end
- def symbol_filename
+ scope :find_private, -> (operators) do
+ where(author_id: operators.author.id)
end
- def self.list_where
- 'comics.visible > 0'
+ scope :find_by_author, -> (author_id) do
+ find_index.where(author_id: author_id)
end
- def self.list_order
- 'comics.updated_at desc'
+ scope :with_stories, -> do
+ includes(comic_stories: :story)
end
- def self.mylist_where me
- ['comics.author_id = ?', me.id]
+ scope :find_by_story, -> (story_id) do
+ with_stories.find_index.where(Story.arel_table[:id].eq story_id).references(:story)
end
- def self.mylist_order
- 'comics.updated_at desc'
+ # scope of find_play
+ def self.find_play(id)
+ ComicStory.find_play(id)
end
- def self.himlist_where anybody
- ['comics.author_id = ? and comics.visible > 0', anybody.id]
+ # scope of find_private_play
+ def self.find_private_play(id, operators)
+ ComicStory.find_private_play(id, operators)
end
- def self.himlist_order
- 'comics.updated_at desc'
+ def supply_default
+ self.visible = 0 if self.visible.blank?
+ self.author_id = nil
end
- def self.list_by_author_where author_id
- ['comics.author_id = ?', author_id]
+ def overwrite operators
+ return false unless operators.author
+ self.author_id = operators.author.id
end
- def self.list_by_author author_id, roles, page = 1, page_size = self.default_page_size
- self.where(self.list_by_author_where(author_id)).includes(self.list_opt).order('comics.updated_at desc').offset((page -1) * page_size).limit(page_size)
+ def visible? operators
+ case super
+ when nil # super return
+ return true
+ when false
+ return false
+ else
+ self.visible > 0
+ end
end
- def self.list_opt
- {:stories => {}, :author => {} }
+ def symbol_filename
end
- def self.list_json_opt
- {:include => {:stories => {}, :author => {}} }
+ def self.public_list_where list
+ 'comics.visible > 0'
end
def self.show_opt
- {:include => {:stories => {}, :author => {}} }
- end
-
- def self.show_json_opt
- {:include => {:stories => {}, :author => {}} }
- end
-
- def destroy_with_story
- res = false
- Comic.transaction do
- self.stories.each do |story|
- raise ActiveRecord::Rollback unless story.destroy_and_shorten
- end
- raise ActiveRecord::Rollback unless self.destroy
- res = true
- end
- res
+ {:include => {:comic_stories => {:story => {}}, :author => {}}}
end
def scenario