OSDN Git Service

t#29312:any update
[pettanr/pettanr.git] / app / models / ground_color.rb
1 class GroundColor < ActiveRecord::Base
2   belongs_to :panel
3   belongs_to :color
4   
5   validates :panel_id, :numericality => {:allow_blank => true}
6   validates :color_id, :numericality => true, :existence => true
7   validates :z, :presence => true, :numericality => {:greater_than => 0}
8   
9   def self.default_page_size
10     25
11   end
12   
13   def self.max_page_size
14     100
15   end
16   
17   def self.page prm = nil
18     page = prm.to_i
19     page = 1 if page < 1
20     page
21   end
22   
23   def self.page_size prm = self.default_page_size
24     page_size = prm.to_i
25     page_size = self.max_page_size if page_size > self.max_page_size
26     page_size = self.default_page_size if page_size < 1
27     page_size
28   end
29   
30   def self.offset cnt, prm = nil
31     offset = prm.to_i
32     offset = cnt - 1 if offset >= cnt
33     offset = cnt - offset.abs if offset < 0
34     offset = 0 if offset < 0
35     offset
36   end
37   
38   def self.list opt = {}, page = 1, page_size = self.default_page_size
39     opt.merge!(self.list_opt) unless opt[:include]
40     opt.merge!({:conditions => 'panels.publish > 0', :order => 'ground_colors.updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
41     GroundColor.find(:all, opt)
42   end
43   
44   def self.list_opt
45     {:include => {:panel => {:author => {}}, :color => {} }}
46   end
47   
48   def self.list_json_opt
49     {:include => {:panel => {:author => {}}, :color => {} }}
50   end
51   
52   def self.mylist au, opt = {}, page = 1, ground_color_page_size = Author.default_ground_color_page_size
53     opt.merge!(self.list_opt) unless opt[:include]
54     opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'ground_colors.updated_at desc', :limit => page_size, :offset => (page -1) * ground_color_page_size})
55     GroundColor.find(:all, opt)
56   end
57   
58 end