OSDN Git Service

4e6f0f41d33ed666df3450dcac0d5f27703e5708
[pettanr/pettanr.git] / app / models / comic.rb
1 class Comic < ActiveRecord::Base
2   has_many :panels, :dependent => :destroy
3   belongs_to :author
4   
5   class NotZeroValidator < ActiveModel::EachValidator\r
6     def validate_each(record, attribute, value)\r
7       record.errors[attribute] << (options[:message] || "is zero") if value == 0\r
8     end\r
9   end\r
10
11   validates :title, :presence => true, :length => {:maximum => 100}\r
12   validates :width, :presence => true, :numericality => true, :not_zero => true
13   validates :height, :presence => true, :numericality => true, :not_zero => true
14   validates :visible, :presence => true, :numericality => true, :inclusion => {:in => 0...4}
15   validates :editable, :presence => true, :numericality => true, :inclusion => {:in => 0...4}
16   
17   before_save do |r|
18     r.supply_default
19   end
20   
21   def supply_default
22     self.visible = 0 if self.visible.blank?
23     self.editable= 0 if self.editable.blank?
24   end
25   
26   def own? author
27     return false unless author
28     self.author_id == author.id
29   end
30   
31   def disp_editable
32     editable == 1 ? 'O' : 'X'
33   end
34   
35   def disp_visible
36     visible == 1 ? 'O' : 'X'
37   end
38   
39 end