OSDN Git Service

fix: racer
[pettanr/pettanr.git] / app / models / panel.rb
index c69d879..daa7bf7 100644 (file)
 #コマ
-class Panel < ActiveRecord::Base
+class Panel < Peta::Root
+  load_manifest
   belongs_to :author
-  belongs_to :resource_picture
-  has_many :stories
+  has_many :scroll_panels
+  has_many :sheet_panels
   has_many :panel_pictures, :dependent => :destroy
   has_many :speech_balloons, :dependent => :destroy
   has_many :ground_pictures, :dependent => :destroy
   has_many :ground_colors, :dependent => :destroy
-  has_many :panel_colors, :dependent => :destroy
   accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
   accepts_nested_attributes_for :speech_balloons, :allow_destroy => true
   accepts_nested_attributes_for :ground_pictures, :allow_destroy => true
   accepts_nested_attributes_for :ground_colors, :allow_destroy => true
-  accepts_nested_attributes_for :panel_colors, :allow_destroy => true
 
   validates :width, :presence => true, :numericality => true, :natural_number => true
   validates :height, :presence => true, :numericality => true, :natural_number => true
   validates :border, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
-  validates :x, :numericality => {:allow_blank => true}
-  validates :y, :numericality => {:allow_blank => true}
-  validates :z, :numericality => {:allow_blank => true, :greater_than => 0}
-  validates :author_id, :presence => true, :numericality => true, :existence => true
+  validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
   validates :publish, :presence => true, :numericality => true
   
-  def supply_default
-    self.border = 2
-    self.publish = 0
-  end
-  
-  def overwrite au
-    self.author_id = au.id
-  end
-  
-  def own? au
-    return false unless au.is_a?(Author)
-    self.author_id == au.id
-  end
-  
-  def visible? au
-    if au == nil
-      return false if MagicNumber['run_mode'] == 1
-    elsif au.is_a?(Author)
-      return true if self.own?(au)
-    else
-      return false
-    end
-    self.publish?
-  end
-  
-  def usable? au
-    visible? au
+  scope :find_index, -> do
+    where(arel_table[:publish].gt 0)
   end
   
-  def publish?
-    self.publish > 0
-  end
-  
-  def self.default_page_size
-    25
+  scope :find_private, -> (operators) do 
+    where(author_id: operators.author.id)
   end
   
-  def self.max_page_size
-    100
+  scope :find_by_author, -> (author_id) do 
+    find_index.where(author_id: author_id)
   end
   
-  def self.page prm = nil
-    page = prm.to_i
-    page = 1 if page < 1
-    page
+  scope :with_scrolls, -> do
+    includes(scroll_panels: :scroll)
   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
+  scope :find_by_scroll, -> (scroll_id) do 
+    with_scrolls.find_index.where(Scroll.arel_table[:id].eq scroll_id).references(:scroll)
   end
   
-  def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(self.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => 'panels.publish > 0', :order => 'panels.updated_at desc'})
-    Panel.find(:all, opt)
-  end
-  
-  def self.list_opt
-    {:include => {
-      :panel_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :speech_balloons => {:balloons => {}, :speeches => {}},
-      :ground_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :ground_colors => {
-        :color => {}
-      }, 
-      :panel_colors => {
-      }, 
-      :author => {}
-    }}
+  scope :with_sheets, -> do
+    includes(sheet_panels: :sheet)
   end
   
-  def self.mylist au, page = 1, page_size = Author.default_panel_page_size
-    opt = {}
-    opt.merge!(self.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'panels.updated_at desc'})
-    Panel.find(:all, opt)
+  scope :find_by_sheet, -> (sheet_id) do 
+    with_sheets.find_index.where(Sheet.arel_table[:id].eq sheet_id).references(:sheet)
   end
   
-  def self.show rid, au
-    opt = {}
-    opt.merge!(Panel.show_opt)
-    res = Panel.find(rid, opt)
-    raise ActiveRecord::Forbidden unless res.visible?(au)
-    res
+  scope :with_speech_balloons, -> do
+    includes(:speech_balloons)
   end
   
-  def self.edit rid, au
-    opt = {}
-    opt.merge!(Panel.show_opt)
-    res = Panel.find(rid, opt)
-    raise ActiveRecord::Forbidden unless res.own?(au)
-    res
+  scope :find_by_speech_balloon_template, -> (speech_balloon_template_id) do 
+    with_speech_balloons.find_index.where(SpeechBalloon.arel_table[:speech_balloon_template_id].eq speech_balloon_template_id).references(:speech_balloon)
   end
   
-  def self.show_opt
-    {:include => {
-      :panel_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :speech_balloons => {:balloons => {}, :speeches => {}},
-      :ground_pictures => {
-        :picture => {:artist => {}, :license => {}}
-      }, 
-      :ground_colors => {
-        :color => {}
-      }, 
-      :panel_colors => {
-      }, 
-      :author => {}
-    }}
+  def supply_default
+    self.border = 2
+    self.publish = 0
   end
   
-  def parts_element
-    ((self.panel_pictures || []) + (self.speech_balloons || [])).compact
+  def overwrite operators
+    return false unless operators.author
+    self.author_id = operators.author.id
   end
   
-  def parts
-    res = []
-    self.parts_element.each do |e|
-      res[e.t] = e
+  def visible? operators
+    case super
+    when nil # super return
+      return true
+    when false
+      return false
+    else
+      return true if self.new_record?
+      self.publish?
     end
-    res
   end
   
-  def grounds
-    ((self.ground_colors || []) + (self.ground_pictures || []) + (self.panel_colors || [])).compact
+  def usable? operators
+    self.visible? operators
   end
   
-  def panel_elements
-    parts + grounds
-  end
-  
-  @@elm_json_opt = {
-    'PanelPicture' => {
-      :picture => {:artist => {}, :license => {}}
-    }, 
-    'SpeechBalloon' => {:balloons => {}, :speeches => {}},
-    'GroundPicture' => {
-      :picture => {:artist => {}, :license => {}}
-    }, 
-    'GroundColor' => {
-      :color => {}
-    }, 
-    'PanelColor' => {
-    } 
-  }
-  
-  def self.elm_json_opt e
-    @@elm_json_opt[e.class.to_s]
+  def publish?
+    self.publish > 0
   end
   
-  def elements
-    self.panel_elements.map {|e|
-      #(-_-;)<... kore wa hidoi
-      JSON.parse e.to_json({:include => Panel.elm_json_opt(e)})
+  def style
+    {
+      'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px', 
+      'border-style' => 'solid', 'border-width' => self.border.to_s + 'px', 
+      'border-color' => 'black', 'background-color' => 'white'
     }
   end
   
-  def panel_elements_as_json
-    self.to_json({:include => {:author => {}}, :methods => :elements})
-  end
-  
-  def self.list_as_json_text ary
-    '[' + ary.map {|i| i.panel_elements_as_json }.join(',') + ']'
-  end
-  
-  def self.visible_count
-    Panel.count
+  # ground_picture element template 
+  def style_wh
+    {
+      'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px'
+    }
   end
   
-  def self.collect_element_value elements, name
-    elements.map {|e|
-      e.map {|o|
-        o[name]
-      }
-    }.flatten
+  def self.public_list_where list
+    'panels.publish > 0'
   end
   
-  def self.validate_t ary
-    i = 0
-    ary.compact.sort.each do |t|
-      break false unless t == i
-      i += 1
+  def self.show_opt
+    r = {
+      :author => {}
+    }
+    self.child_models.each do |child_model|
+      r.merge!(child_model.show_opt_for_panel)
     end
-    ary.compact.size == i
+    {:include => r}
   end
   
-  def self.validate_element_t elements, name
-    Panel.validate_t(Panel.collect_element_value(elements, name))
+  def scenario
+    scenario_elements.map { |e|
+      e.scenario
+    }.join
   end
   
-  def self.validate_elements_t c
-    c.map {|conf|
-      Panel.validate_element_t(conf[:elements], conf[:name]) ? nil : false
-    }.compact.empty?
+  def plain_scenario
+    scenario_elements.map { |e|
+      e.plain_scenario
+    }.join
   end
   
-  def validate_t_list
-    [
-      {:elements => [self.panel_pictures, self.speech_balloons], :name => :t}
-    ]
-  end
-  def validate_child
-#    r1 = Panel.validate_elements_id validate_id_list
-    Panel.validate_elements_t validate_t_list
-  end
-  
-  def store attr, au
-    if attr == false
-      self.errors.add :base, 'invalid json'
-      return false
-    end
-    self.attributes = attr
-    self.overwrite au
-    res = false
-    Panel.transaction do
-      unless validate_child
-        self.errors.add :base , 'invalid time'
-        raise ActiveRecord::Rollback
-      end
-      res = self.save
+  def self.licensed_pictures panels
+    r = {}
+    panels.each do |panel|
+      r.merge!(panel.licensed_pictures)
     end
-    res
-  end
-  
-=begin\r
-  def self.validate_id ary, pid
-    ary.map {|v|
-      if pid
-        (v == pid or v == nil) ? nil : false
-      else
-        v ? false : nil
-      end
-    }.compact.empty?
-  end
-  
-  def self.validate_element_id elements, name, pid
-    Panel.validate_id(Panel.collect_element_value(elements, name), pid)
-  end
-  
-  def self.validate_elements_id c
-    c.map {|conf|
-      Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false
-    }.compact.empty?
+    r
   end
   
-  def validate_id_list
-    r = self.speech_balloons.map {|sb|
-      {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id}
-    }
-    r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id})
+  def licensed_pictures
+    r = {}
+    self.scenario_elements.each do |elm|
+      next unless elm.class.has_picture?
+      r[elm.picture_id] = elm.picture unless r[elm.picture_id]
+    end
     r
   end
-=end
   
 end