OSDN Git Service

t#32025:comic rename
[pettanr/pettanr.git] / app / models / speech.rb
1 class Speech < ActiveRecord::Base
2   include ElementPart
3   belongs_to :speech_balloon
4   belongs_to :writing_format
5   
6   validates :speech_balloon_id, :numericality => {:allow_blank => true}
7   validates :writing_format_id, :presence => true, :numericality => true, :existence => {:both => false}
8   validates :font_size, :presence => true, :numericality => {:only_integer => false}
9   validates :text_align, :presence => true, :numericality => true, :inclusion => { :in => 0..3 }
10   validates :fore_color, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
11   validates :x, :presence => true, :numericality => true
12   validates :y, :presence => true, :numericality => true
13   validates :width, :presence => true, :numericality => true, :natural_number => true
14   validates :height, :presence => true, :numericality => true, :natural_number => true
15   validates :quotes, :length => {:maximum => 15}, :quotes_even => true
16   validates :settings, :extend_speech => true
17   
18   @@text_align_texts = ['left', 'left', 'right', 'center']
19   
20   before_validation :valid_encode
21   
22   def valid_encode
23     ['content', 'quotes', 'settings'].each do |a|
24       next if attributes[a] == nil
25       raise Pettanr::BadRequest unless attributes[a].valid_encoding?
26     end
27   end
28   
29   def self.colum_structures
30     @@colum_structures ||= {
31       :fore_color => {
32         :helper => 'panels/color_helper'
33       }
34     }
35   end
36   
37   def supply_default
38     self.x = 0
39     self.y = 0
40     self.width = 100
41     self.height = 100
42   end
43   
44   def visible? roles
45     if MagicNumber['run_mode'] == 0
46       return false unless guest_role_check(roles)
47     else
48       return false unless reader_role_check(roles)
49     end
50     return true if self.speech_balloon.panel.own?(roles)
51     self.speech_balloon.panel.visible? roles
52   end
53   
54   def get_parent
55     self.speech_balloon || @new_parent
56   end
57   
58   def tag_element_part_type
59     'speech'
60   end
61   
62   def text_align_text
63     @@text_align_texts[self.text_align]
64   end
65   
66   def self.default_page_size
67     25
68   end
69   
70   def self.max_page_size
71     100
72   end
73   
74   def self.page prm = nil
75     page = prm.to_i
76     page = 1 if page < 1
77     page
78   end
79   
80   def self.page_size prm = self.default_page_size
81     page_size = prm.to_i
82     page_size = self.max_page_size if page_size > self.max_page_size
83     page_size = self.default_page_size if page_size < 1
84     page_size
85   end
86   
87   def self.list page = 1, page_size = self.default_page_size
88     opt = {}
89     opt.merge!(Speech.list_opt)
90     opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
91     opt.merge!({:conditions => 'panels.publish > 0', :order => 'speeches.updated_at desc'})
92     Speech.find(:all, opt)
93   end
94   
95   def self.list_opt
96     {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {}} }}
97   end
98   
99   def self.list_json_opt
100     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
101   end
102   
103   def self.show cid, au
104     opt = {}
105     opt.merge!(Speech.show_opt)
106     res = Speech.find(cid, opt)
107     raise ActiveRecord::Forbidden unless res.visible?(au)
108     res
109   end
110   
111   def self.show_opt
112     {:include => {:speech_balloon => {:panel => {:author => {}}, :balloon => {}, :speech_balloon_template => {} }}}
113   end
114   
115   def self.show_json_opt
116     {:include => {:speech_balloon => {:include => {:panel => {:include => {:author => {} }}, :balloon => {}, :speech_balloon_template => {} }}}}
117   end
118   
119   def copy_attributes
120     r = self.attributes
121     r.delete 'id'
122     r.delete 'speech_balloon_id'
123     r.delete 'created_at'
124     r.delete 'updated_at'
125     r
126   end
127   
128   def self.panelize speech_attributes
129     {'speech_attributes' => speech_attributes}
130   end
131   
132   def writing_format_engine_model
133     self.writing_format.engine_model
134   end
135   
136   def scenario
137     self.writing_format_engine_model.render self.content
138   end
139   
140   def plain_scenario
141     self.content + "\n"
142   end
143   
144   def feed
145     ERB::Util.html_escape(self.content)
146   end
147   
148 end