OSDN Git Service

fix: fetch fail
[pettanr/pettanr.git] / app / models / sheet.rb
index a3b8bff..6d5360e 100644 (file)
-class Sheet < ActiveRecord::Base
+class Sheet < Peta::Root
+  load_manifest
   has_many :sheet_panels
   has_many :story_sheets
   belongs_to :author
   
   validates :caption, :presence => true, :length => {:maximum => 100}
+  validates :width, :presence => true, :numericality => true, :natural_number => true
+  validates :height, :presence => true, :numericality => true, :natural_number => true
   validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0..1}
-  before_validation :valid_encode
+  accepts_nested_attributes_for :sheet_panels, :allow_destroy => true
   
-  def valid_encode
-    ['caption', '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?
-  end
-  
-  def overwrite au
-    return false unless au
-    self.author_id = au.id
-  end
-  
-  def own? roles
-    roles = [roles] unless roles.respond_to?(:each)
-    au = Sheet.get_author_from_roles roles
-    return false unless au
-    self.author_id == au.id
-  end
-  
-  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.own?(roles)
-    self.visible > 0
-  end
-  
-  def usable? au
-    visible? au
-  end
-  
-  def self.default_page_size
-    25
+  scope :find_index, -> do
+    where(arel_table[:visible].gt 0)
   end
   
-  def self.max_page_size
-    100
+  scope :find_private, -> (operators) do 
+    where(author_id: operators.author.id)
   end
   
-  def self.default_panel_size
-    30
+  scope :find_by_author, -> (author_id) do 
+    find_index.where(author_id: author_id)
   end
   
-  def self.max_panel_size
-    200
+  scope :with_panels, -> do
+    includes(sheet_panels: :panel)
   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.list_where
-    'sheets.visible > 0'
+  scope :find_by_panel, -> (panel_id) do 
+    with_panels.find_index.where(Panel.arel_table[:id].eq panel_id).references(:panel)
   end
   
-  def self.mylist_where au
-    ['sheets.author_id = ?', au.id]
+  scope :with_stories, -> do
+    includes(story_sheets: :story)
   end
   
-  def self.himlist_where au
-    ['sheets.author_id = ? and sheets.visible > 0', au.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.list page = 1, page_size = self.default_page_size
-    Sheet.where(self.list_where()).includes(Sheet.list_opt).order('sheets.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  # scope of find_play
+  def self.find_play(id)
+    SheetPanel.find_play(id)
   end
   
-  def self.mylist au, page = 1, page_size = Author.default_sheet_page_size
-    Sheet.where(self.mylist_where(au)).includes(Sheet.list_opt).order('sheets.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  # scope of find_private_play
+  def self.find_private_play(id, operators)
+    SheetPanel.find_private_play(id, operators)
   end
   
-  def self.himlist au, page = 1, page_size = Author.default_sheet_page_size
-    Sheet.where(self.himlist_where(au)).includes(Sheet.list_opt).order('sheets.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(Sheet.where(self.list_where()).count, nil)).page(page).per(page_size)
-  end
-  
-  def self.mylist_paginate au, page = 1, page_size = Author.default_sheet_page_size
-    Kaminari.paginate_array(Array.new(Sheet.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
+  def supply_default
+    self.visible = 0 if self.visible.blank?
+    self.author_id = nil
   end
   
-  def self.himlist_paginate au, page = 1, page_size = Author.default_sheet_page_size
-    Kaminari.paginate_array(Array.new(Sheet.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
+  def overwrite operators
+    return false unless operators.author
+    self.author_id = operators.author.id
   end
   
-  def self.list_opt
-    {:sheet_panels => {:panel => {}}, :author => {} }
+  def visible? operators
+    case super
+    when nil # super return
+      return true
+    when false
+      return false
+    else
+      self.visible > 0
+    end
   end
   
-  def self.list_json_opt
-    {:include => {:sheet_panels => {:include => {:panel => {}}}, :author => {}}}
+  def usable? operators
+    self.visible? operators
   end
   
-  def self.show sid, roles
-    opt = {}
-    opt.merge!(Sheet.show_opt)
-    res = Sheet.find(sid, opt)
-    raise ActiveRecord::Forbidden unless res.visible?(roles)
-    res
+  def symbol_filename
   end
   
-  def self.edit sid, au
-    opt = {}
-    opt.merge!(Sheet.show_opt)
-    res = Sheet.find(sid, opt)
-    raise ActiveRecord::Forbidden unless res.own?(au)
-    res
+  def self.public_list_where list
+    'sheets.visible > 0'
   end
   
   def self.show_opt
@@ -141,22 +88,6 @@ class Sheet < ActiveRecord::Base
     {:include => {:sheet_panels => {:include => {:panel => {}}}, :author => {}}}
   end
   
-  def self.visible_count
-    Sheet.count 'visible > 0'
-  end
-  
-  def destroy_with_sheet_panel
-    res = false
-    Sheet.transaction do
-      self.sheet_panels.each do |sheet_panel|
-        raise ActiveRecord::Rollback unless sheet_panel.destroy
-      end
-      raise ActiveRecord::Rollback unless self.destroy
-      res = true
-    end
-    res
-  end
-  
   def scenario
     panels.map {|panel|
       panel.scenario
@@ -169,4 +100,22 @@ class Sheet < ActiveRecord::Base
     }.join
   end
   
+  def licensed_pictures
+    r = {}
+    self.scenario_elements.each do |elm|
+      r.merge!(elm.licensed_pictures)
+    end
+    r
+  end
+  
+  def copyable?
+    r = true
+    self.elements_items.each do |elm|
+      next if elm.copyable?
+      r = false
+      break
+    end
+    r
+  end
+  
 end