OSDN Git Service

t#31538:fix default value
[pettanr/pettanr.git] / app / models / panel.rb
index 2d7de5b..2167d93 100644 (file)
@@ -1,78 +1,67 @@
+#コマ
 class Panel < ActiveRecord::Base
-  belongs_to :comic
   belongs_to :author
   belongs_to :resource_picture
-#  belongs_to :background_picture, :class_name => 'ResourcePicture'
+  has_many :stories
   has_many :panel_pictures, :dependent => :destroy
-  has_many :balloons, :dependent => :destroy
+  has_many :speech_balloons, :dependent => :destroy
+  has_many :ground_pictures, :dependent => :destroy
+  has_many :ground_colors, :dependent => :destroy
   accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
-  accepts_nested_attributes_for :balloons, :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
 
-  validates :comic_id, :presence => true, :numericality => true, :existence => true, :uniqueness => {:scope => :t} 
-  validates :resource_picture_id, :numericality => {:allow_blank => 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 :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
-  validates :author_id, :presence => true, :numericality => true, :existence => true
-  
-  def supply_default au
-    c = self.comic_id ? Comic.show(self.comic_id, au) : nil
-    if c
-      self.width = c.width if self.width.blank?
-      self.height = c.height if self.height.blank?
-      self.t = Panel.new_t(c.id) if self.t.blank? and self.new_record?
+  validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
+  validates :publish, :presence => true, :numericality => true
+  
+  before_validation :valid_encode
+  
+  def valid_encode
+    ['caption'].each do |a|
+      next if attributes[a] == nil
+      raise Pettanr::BadRequest unless attributes[a].valid_encoding?
     end
-    self.border = 0 if self.border.blank?
-    self.author_id = au.id
   end
   
-  def self.new_t comic_id
-    r = Panel.max_t(comic_id)
-    r.blank? ? 0 : r.to_i + 1
+  def supply_default
+    self.border = 2
+    self.publish = 0
   end
   
-  def self.max_t comic_id
-    Panel.maximum(:t, :conditions => ['comic_id = ?', comic_id])
+  def overwrite au
+    self.author_id = au.id
   end
   
-  def self.find_t comic_id, t
-    Panel.find(:first, :conditions => ['comic_id = ? and t = ?', comic_id, t])
+  def own? roles
+    roles = [roles] unless roles.respond_to?(:each)
+    au = Panel.get_author_from_roles roles
+    return false unless au
+    self.author_id == au.id
   end
-
-  #更新する時にPanelIDをチェックしとかないと勝手に所属先を変えられるゾ!?
-
-  def vdt_save
-    f = nil
-    f = Panel.find_t(self.comic_id, self.t) if self.t
-    if f
-      Panel.update_all('t = t + 1', ['comic_id = ? and t >= ?', self.comic_id, self.t])
+  
+  def visible? roles
+    if MagicNumber['run_mode'] == 0
+      return false unless guest_role_check(roles)
     else
-      self.t = Panel.new_t self.comic_id
+      return false unless reader_role_check(roles)
     end
-    self.save
+    return true if self.own?(roles)
+    self.publish?
   end
   
-  def move_to new_t
-    return true if self.t == new_t
-    if self.t > new_t
-      Panel.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, new_t, self.t])
-    else
-      nf = Panel.find_t(self.comic_id, new_t)
-      max_t = Panel.max_t.to_i self.comic_id
-      new_t = max_t if new_t > max_t
-      Panel.update_all('t = t - 1', ['comic_id = ? and (t > ? and t <= ?)', self.comic_id, self.t, new_t])
-    end
-    self.t = new_t
-    self.save
+  def usable? au
+    visible? au
   end
   
-  def destroy_and_shorten
-    Panel.update_all('t = t - 1', ['comic_id = ? and (t > ?)', self.comic_id, self.t])
-    self.destroy
+  def publish?
+    self.publish > 0
   end
   
   def self.default_page_size
@@ -96,114 +85,268 @@ class Panel < ActiveRecord::Base
     page_size
   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
+  def self.list_where
+    'panels.publish > 0'
   end
   
-  def self.list opt = {}, page = 1, page_size = self.default_page_size
-    opt.merge!(self.list_opt) unless opt[:include]
-    opt.merge!({:order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
-    Panel.find(:all, opt)
+  def self.mylist_where au
+    ['panels.author_id = ?', au.id]
   end
   
-  def self.list_opt
-    {:include => {
-      :comic => :author, 
-      :panel_pictures => {
-        :resource_picture => {:artist => {}, :license => {}}
-      }, 
-      :balloons => {:speaches => {}},
-      :author => {}
-    }}
+  def self.himlist_where au
+    ['panels.author_id = ? and panels.publish > 0', au.id]
   end
   
-  def self.list_json_opt
-    {:include => {
-      :comic => :author, 
-      :panel_pictures => {
-        :resource_picture => {:artist => {}, :license => {}}
-      }, 
-      :balloons => {:speaches => {}},
-      :author => {}
-    }}
+  def self.list page = 1, page_size = self.default_page_size
+    Panel.where(self.list_where()).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
   end
   
-  def self.show rid, au, opt = {}
-    r = Panel.find(rid, :include => self.show_include_opt(opt))
-    raise ActiveRecord::Forbidden unless r.visible?(au)
-    r
+  def self.mylist au, page = 1, page_size = Author.default_panel_page_size
+    Panel.where(self.mylist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.himlist au, page = 1, page_size = Author.default_panel_page_size
+    Panel.where(self.himlist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
   end
   
-  def self.show_include_opt opt = {}
-    res = {
-      :comic => :author, 
+  def self.list_paginate page = 1, page_size = self.default_page_size
+    Kaminari.paginate_array(Array.new(Panel.where(self.list_where()).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.mylist_paginate au, page = 1, page_size = Author.default_panel_page_size
+    Kaminari.paginate_array(Array.new(Panel.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.himlist_paginate au, page = 1, page_size = Author.default_panel_page_size
+    Kaminari.paginate_array(Array.new(Panel.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.list_opt
+    {
       :panel_pictures => {
-        :resource_picture => {:artist => {}, :license => {}}
+        :picture => {:artist => {}, :license => {}}
+      }, 
+      :speech_balloons => {:balloon => {}, :speech => {}},
+      :ground_pictures => {
+        :picture => {:artist => {}, :license => {}}
+      }, 
+      :ground_colors => {
       }, 
-      :balloons => {:speaches => {}},
       :author => {}
     }
-    res.merge!(opt[:include]) if opt[:include]
+  end
+  
+  def self.show rid, roles
+    opt = {}
+    opt.merge!(Panel.show_opt)
+    res = Panel.find(rid, opt)
+    raise ActiveRecord::Forbidden unless res.visible?(roles)
+    res
+  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
   end
   
-  def self.show_json_include_opt
+  def self.show_opt
     {:include => {
-      :comic => :author, 
       :panel_pictures => {
-        :resource_picture => {:artist => {}, :license => {}}
+        :picture => {:artist => {}, :license => {}}
+      }, 
+      :speech_balloons => {:balloon => {}, :speech => {}},
+      :ground_pictures => {
+        :picture => {:artist => {}, :license => {}}
+      }, 
+      :ground_colors => {
       }, 
-      :balloons => {:speaches => {}},
       :author => {}
     }}
   end
   
-  def visible? au
-    return false unless au
-    return false unless self.comic
-    self.comic.visible?(au) or self.author_id == au.id
+  def parts_element
+    ((self.panel_pictures || []) + (self.speech_balloons || []) + (self.ground_colors || []) + (self.ground_pictures || [])).compact
+  end
+  
+  def parts
+    res = []
+    self.parts_element.each do |e|
+      res[e.t] = e
+    end
+    res
   end
   
-  def own? author
-    return false unless author
-    self.author_id == author.id
+  def panel_elements
+    parts
   end
   
-  def sort_by_time
-    pe = []
-    self.panel_pictures.each do |picture|
-      pe[picture.t] = picture
-    end
-    self.balloons.each do |balloon|
-      pe[balloon.t] = balloon
+  @@elm_json_opt = {
+    'PanelPicture' => {
+      :picture => {:artist => {}, :license => {}}
+    }, 
+    'SpeechBalloon' => {:balloon => {}, :speech => {}},
+    'GroundPicture' => {
+      :picture => {:artist => {}, :license => {}}
+    }, 
+    'GroundColor' => {
+    }, 
+  }
+  
+  def self.elm_json_opt e
+    @@elm_json_opt[e.class.to_s]
+  end
+  
+  def elements
+    self.panel_elements.map {|e|
+      #(-_-;)<... kore wa hidoi
+      JSON.parse e.to_json({:include => Panel.elm_json_opt(e)})
+    }
+  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 new_t
+    self.parts.size
+  end
+  
+  def new_z
+    self.parts.size + 1
+  end
+  
+  def scenario
+    panel_elements.map { |e|
+      e.scenario
+    }.join
+  end
+  
+  def plain_scenario
+    panel_elements.map { |e|
+      e.plain_scenario
+    }.join
+  end
+  
+  def licensed_pictures
+    r = {}
+    ((self.panel_pictures || []) + (self.ground_pictures || [])).compact.each do |elm|
+      r[elm.picture_id] = elm.picture unless r[elm.picture_id]
     end
-    pe
+    r
+  end
+  
+  def self.visible_count
+    Panel.count
   end
   
-  def each_element
-    self.sort_by_time.each do |e|
-      yield e
+  def self.collect_element_value elements, name
+    elements.map {|e|
+      e.map {|o|
+        if o['_destroy'] or o[:_destroy]
+          nil
+        else
+          o[name]
+        end
+      }.compact
+    }.flatten
+  end
+  
+  def self.validate_serial ary, offset = 0
+    i = offset
+    ary.compact.sort.each do |n|
+      break false unless n == i
+      i += 1
     end
+    ary.compact.size == i - offset
   end
   
-  def panel_elements
-    res = []
-    self.each_element do |elm|
-      if elm.kind_of?(PanelPicture)
-        res[elm.t] = elm.to_json({:include => :resource_picture})
+  def self.validate_element_serial elements, name, offset = 0
+    Panel.validate_serial(Panel.collect_element_value(elements, name), offset)
+  end
+  
+  def self.validate_elements_serial c
+    c.map {|conf|
+      Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false
+    }.compact.empty?
+  end
+  
+  def validate_serial_list
+    [
+      {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :t, :offset => 0}, 
+      {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :z, :offset => 1}
+    ]
+  end
+  def validate_child
+#    r1 = Panel.validate_elements_id validate_id_list
+    Panel.validate_elements_serial validate_serial_list
+  end
+  
+  def store attr, au
+    if attr == false
+      self.errors.add :base, I18n.t('errors.invalid_json')
+      return false
+    end
+    self.attributes = attr
+    self.overwrite au
+    res = false
+    Panel.transaction do
+      res = self.save
+      unless validate_child
+        res = false
+        self.errors.add :base, I18n.t('errors.invalid_t')
+        raise ActiveRecord::Rollback
       end
-      if elm.kind_of?(Balloon)
-        res[elm.t] = elm.to_json({:include => :speaches})
+    end
+    res
+  end
+  
+  def destroy_with_elements
+    res = false
+    Panel.transaction do
+      self.parts_element.each do |element|
+        raise ActiveRecord::Rollback unless element.destroy
       end
+      raise ActiveRecord::Rollback unless self.destroy
+      res = true
     end
     res
   end
   
-  def to_json_play
-    self.to_json :methods => :panel_elements
+=begin
+  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?
   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})
+    r
+  end
+=end
+  
 end