OSDN Git Service

fic picture publishing
[pettanr/pettanr.git] / app / models / speech_balloon.rb
index 8aabc7a..d5a4148 100644 (file)
@@ -1,6 +1,5 @@
-class SpeechBalloon < ActiveRecord::Base
-  include Element
-  include ElementInspire
+class SpeechBalloon < Peta::Element
+  load_manifest
   has_one :balloon, :dependent => :destroy
   has_one :speech, :dependent => :destroy
   belongs_to :speech_balloon_template
@@ -11,18 +10,17 @@ class SpeechBalloon < ActiveRecord::Base
   
   validates :panel_id, :numericality => {:allow_blank => true}
   validates :speech_balloon_template_id, :presence => true, :numericality => true, :existence => {:both => false}
-  validates :classname, :presence => true, :length => {:maximum => 50}
+  validates :speech_balloon_template_classname, :presence => true, :length => {:maximum => 50}
   validates :z, :presence => true, :numericality => {:greater_than => 0}
   validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
-  validates :settings, :extend_speech_balloon => true
+  validates :speech_balloon_template_settings, :boost => {:resource_name => :speech_balloon_template}
   
-  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
+  def self.by_author_list_includes
+    {
+      :panel => {
+        :author => {}
+      }
+    }
   end
   
   def self.list_opt_for_panel
@@ -47,10 +45,6 @@ class SpeechBalloon < ActiveRecord::Base
     false
   end
   
-  def extend_column
-    'classname'
-  end
-  
   def supply_default
     if self.panel
       self.t = self.panel.new_t 
@@ -62,160 +56,26 @@ class SpeechBalloon < ActiveRecord::Base
     self.panel_id = pid
   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.panel.own?(roles)
-    self.panel.visible? roles
-  end
-  
-  def new_balloon
-    @new_balloon
-  end
-  
-  def new_balloon= v
-    @new_balloon = v
-  end
-  
-  def get_balloon
-    self.balloon || @new_balloon
-  end
-  
-  def new_speech
-    @new_speech
-  end
-  
-  def new_speech= v
-    @new_speech = v
-  end
-  
-  def get_speech
-    self.speech || @new_speech
-  end
-  
-  def boost
-    self.extend self.speech_balloon_template.engine_speech_balloon_module
-    if self.balloon
-    else
-      self.new_balloon = self.build_balloon 
-      self.new_speech = self.build_speech 
-    end
-    self.balloon.extend self.speech_balloon_template.engine_balloon_module
-    self.speech.extend self.speech_balloon_template.engine_speech_module
-    self.balloon.new_parent = self
-    self.speech.new_parent = self
-    self.extend_speech_balloon = if self.settings.blank?
-      {}
-    else
-      JSON.parse(self.settings)
-    end
-    self.balloon.extend_balloon = if self.balloon.settings.blank?
-      {}
-    else
-      JSON.parse(self.get_balloon.settings)
-    end
-    self.speech.extend_speech = if self.speech.settings.blank?
-      {}
-    else
-      JSON.parse(self.get_speech.settings)
-    end
-  end
-  
-  def self.fold_extend_settings params
-    speech_balloon_settings = params[:speech_balloon][:settings]
-    if speech_balloon_settings.is_a?(Hash)
-      params[:speech_balloon][:settings] = speech_balloon_settings.to_json
-    end
-    balloon_settings = params[:speech_balloon][:balloon_attributes][:settings]
-    if balloon_settings.is_a?(Hash)
-      params[:speech_balloon][:balloon_attributes][:settings] = balloon_settings.to_json
-    end
-    speech_settings = params[:speech_balloon][:speech_attributes][:settings]
-    if speech_settings.is_a?(Hash)
-      params[:speech_balloon][:speech_attributes][:settings] = speech_settings.to_json
-    end
+  def symbol_option
+    self.speech_balloon_template.symbol_option
   end
   
   def tag_element_type
     'speech_balloon'
   end
   
-  def self.default_page_size
-    25
-  end
-  
-  def self.max_page_size
-    100
-  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
+  def self.public_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
-    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
-    {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }
+  def self.list_order
+    'speech_balloons.updated_at desc'
   end
   
   def self.list_json_opt
     {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
   end
   
-  def self.show cid, au
-    opt = {}
-    opt.merge!(SpeechBalloon.show_opt)
-    res = SpeechBalloon.find(cid, opt)
-    raise ActiveRecord::Forbidden unless res.visible?(au)
-    res
-  end
-  
   def self.show_opt
     {:include => {:panel => {:author => {}}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
   end
@@ -224,7 +84,7 @@ class SpeechBalloon < ActiveRecord::Base
     {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech => {}, :speech_balloon_template => {} }}
   end
   
-  def store au, attr
+  def store operators, attr
     if self.new_record?
       sb = self.panel.speech_balloons.build
       sb.attributes = attr
@@ -236,7 +96,7 @@ class SpeechBalloon < ActiveRecord::Base
         break
       end
     end
-    self.panel.store({}, au)
+    self.panel.store({}, operators)
   end
   
   def remove au