validates :comic_id, :presence => true, :numericality => true, :existence => {:both => false}
validates :story_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}
+ scope :with_comic, -> do
+ includes(:comic)
+ end
+
+ scope :find_index, -> do
+ with_comic.where(Comic.arel_table[:visible].gt 0).references(:comic)
+ end
+
+ scope :find_private, -> (operators) do
+ with_comic.where(Comic.arel_table[:author_id].eq operators.author.id).references(:comic)
+ end
+
+ scope :by_comic, -> (comic_id) do
+ where(comic_id: comic_id)
+ end
+
+ scope :find_by_comic, -> (comic_id) do
+ find_index.by_comic(comic_id)
+ end
+
+ scope :find_by_story, -> (story_id) do
+ find_index.where(story_id: story_id).references(:comic)
+ end
+
+ scope :find_by_author, -> (author_id) do
+ find_index.where(Comic.arel_table[:author_id].eq author_id).references(:comic)
+ end
+
+ scope :find_play, -> (comic_id) do
+ find_by_comic(comic_id)
+ end
+
+ scope :find_private_play, -> (comic_id, operators) do
+ find_private(operators).by_comic(comic_id)
+ end
+
def supply_default
self.comic_id = nil
self.story_id = nil
end
def overwrite operators
- return false unless operators.author
- self.author_id = operators.author.id
end
def disp_t
self.disp_t_by_text + ':' + self.story.title
end
- def self.public_list_where
+ def self.public_list_where list
'comics.visible > 0'
end
- def self.list_opt
+ def self.by_author_list_includes
{
:comic => {
- :author => {},
+ :author => {}
}
}
end
- def self.list_json_opt
- {:include => {
- :comic => {
- :author => {},
- }
- }}
- end
-
def self.show_opt
{:include => {
:comic => {
+ :author => {}
}
}}
end
self.comic.own?(operators) and self.story.own?(operators)
end
- def store operators, old_t = nil
- res = false
- self.class.transaction do
- 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 = self.class.validate_t(self.comic_id)
- unless res
- self.errors.add :t, 'unserialized'
- raise ActiveRecord::Rollback
- end
- end
- res
- end
-
end