OSDN Git Service

t#:
[pettanr/pettanr.git] / app / models / speech_balloon.rb
index 8e349e1..6ca04cd 100644 (file)
@@ -1,11 +1,11 @@
 class SpeechBalloon < ActiveRecord::Base
-  has_many :balloons, :dependent => :destroy
-  has_many :speeches, :dependent => :destroy
+  has_one :balloon, :dependent => :destroy
+  has_one :speech, :dependent => :destroy
   belongs_to :speech_balloon_template
   belongs_to :panel
   
-  accepts_nested_attributes_for :balloons
-  accepts_nested_attributes_for :speeches
+  accepts_nested_attributes_for :balloon
+  accepts_nested_attributes_for :speech
   
   validates :panel_id, :numericality => {:allow_blank => true}
   validates :speech_balloon_template_id, :presence => true, :numericality => true, :existence => {:both => false}
@@ -14,16 +14,34 @@ class SpeechBalloon < ActiveRecord::Base
   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
 #  validates :settings, :presence => true
   
-  def visible? au
-    if au == nil
-      return false if MagicNumber['run_mode'] == 1
-    elsif au.is_a?(Author)
-    elsif au.is_a?(Admin)
-      return true
+  before_validation :valid_encode
+  
+  def valid_encode
+    ['caption', 'classname', 'settings'].each do |a|
+      next if attributes[a] == nil
+      raise Pettanr::BadRequest unless attributes[a].valid_encoding?
+    end
+  end
+  
+  def supply_default
+    if self.panel
+      self.t = self.panel.new_t 
+      self.z = self.panel.new_z 
+    end
+  end
+  
+  def overwrite pid
+    self.panel_id = pid
+  end
+  
+  def visible? roles
+    if MagicNumber['run_mode'] == 0
+      return false unless guest_role_check(roles)
     else
-      return false
+      return false unless reader_role_check(roles)
     end
-    self.panel.publish?
+    return true if self.panel.own?(roles)
+    self.panel.visible? roles
   end
   
   def self.default_page_size
@@ -47,20 +65,48 @@ class SpeechBalloon < ActiveRecord::Base
     page_size
   end
   
+  def self.list_where
+    'panels.publish > 0'
+  end
+  
+  def self.mylist_where au
+    ['panels.author_id = ?', au.id]
+  end
+  
+  def self.himlist_where au
+    ['panels.author_id = ? and panels.publish > 0', au.id]
+  end
+  
   def self.list page = 1, page_size = self.default_page_size
-    opt = {}
-    opt.merge!(SpeechBalloon.list_opt)
-    opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
-    opt.merge!({:conditions => 'panels.publish > 0', :order => 'speech_balloons.updated_at desc'})
-    SpeechBalloon.find(:all, opt)
+    SpeechBalloon.where(self.list_where()).includes(SpeechBalloon.list_opt).order('speech_balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.mylist au, page = 1, page_size = Author.default_speech_balloon_page_size
+    SpeechBalloon.where(self.mylist_where(au)).includes(SpeechBalloon.list_opt).order('speech_balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
+  end
+  
+  def self.himlist au, page = 1, page_size = Author.default_speech_balloon_page_size
+    SpeechBalloon.where(self.himlist_where(au)).includes(SpeechBalloon.list_opt).order('speech_balloons.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(SpeechBalloon.where(self.list_where()).includes(SpeechBalloon.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.mylist_paginate au, page = 1, page_size = Author.default_speech_balloon_page_size
+    Kaminari.paginate_array(Array.new(SpeechBalloon.where(self.mylist_where(au)).includes(SpeechBalloon.list_opt).count, nil)).page(page).per(page_size)
+  end
+  
+  def self.himlist_paginate au, page = 1, page_size = Author.default_speech_balloon_page_size
+    Kaminari.paginate_array(Array.new(SpeechBalloon.where(self.himlist_where(au)).includes(SpeechBalloon.list_opt).count, nil)).page(page).per(page_size)
   end
   
   def self.list_opt
-    {:include => {:panel => {:author => {}}, :balloons => {}, :speeches => {}, :speech_balloon_template => {} }}
+    {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }
   end
   
   def self.list_json_opt
-    {:include => {:panel => {:include => {:author => {} }}, :balloons => {}, :speeches => {}, :speech_balloon_template => {} }}
+    {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
   end
   
   def self.show cid, au
@@ -72,11 +118,55 @@ class SpeechBalloon < ActiveRecord::Base
   end
   
   def self.show_opt
-    {:include => {:panel => {:author => {}}, :balloons => {}, :speeches => {}, :speech_balloon_template => {} }}
+    {:include => {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
   end
   
   def self.show_json_opt
-    {:include => {:panel => {:include => {:author => {} }}, :balloons => {}, :speeches => {}, :speech_balloon_template => {} }}
+    {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
+  end
+  
+  def store au, attr
+    if self.new_record?
+      sb = self.panel.speech_balloons.build
+      sb.attributes = attr
+    else
+      self.panel.speech_balloons.each do |speech_balloon|
+        next unless speech_balloon == self
+        attr.delete 'id'
+        speech_balloon.attributes = attr
+        break
+      end
+    end
+    self.panel.store({}, au)
+  end
+  
+  def remove au
+    d = false
+    speech_balloons_attributes = {}
+    self.panel.speech_balloons.each do |speech_balloon|
+      attr = speech_balloon.attributes
+      if speech_balloon == self
+        attr['_destroy'] = true
+        d = true
+      else
+        if d
+          attr['t']  -= 1 
+        end
+      end
+      speech_balloons_attributes[speech_balloon.id] = attr
+    end
+    self.panel.attributes = {:speech_balloons_attributes => speech_balloons_attributes}
+    self.panel.store({}, au)
+  end
+  
+  def scenario
+    ERB::Util.html_escape(self.caption.to_s) + self.balloon.scenario.to_s + 
+    self.speech.scenario.to_s
+  end
+  
+  def plain_scenario
+    self.caption.to_s + self.balloon.plain_scenario.to_s + 
+    self.speech.plain_scenario.to_s
   end
   
 end