OSDN Git Service

t#31470:create pager
[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 :code, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 0x1000000}
7   validates :z, :presence => true, :numericality => {:greater_than => 0}
8   
9   def supply_default
10   end
11   
12   def overwrite pid
13     self.panel_id = pid
14   end
15   
16   def visible? roles
17     if MagicNumber['run_mode'] == 0
18       return false unless guest_role_check(roles)
19     else
20       return false unless reader_role_check(roles)
21     end
22     return true if self.panel.own?(roles)
23     self.panel.visible? roles
24   end
25   
26   def self.default_page_size
27     25
28   end
29   
30   def self.max_page_size
31     100
32   end
33   
34   def self.page prm = nil
35     page = prm.to_i
36     page = 1 if page < 1
37     page
38   end
39   
40   def self.page_size prm = self.default_page_size
41     page_size = prm.to_i
42     page_size = self.max_page_size if page_size > self.max_page_size
43     page_size = self.default_page_size if page_size < 1
44     page_size
45   end
46   
47   def self.list_where
48     'panels.publish > 0'
49   end
50   
51   def self.mylist_where au
52     ['panels.author_id = ?', au.id]
53   end
54   
55   def self.himlist_where au
56     ['panels.author_id = ? and panels.publish > 0', au.id]
57   end
58   
59   def self.list page = 1, page_size = self.default_page_size
60     GroundColor.where(self.list_where()).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
61   end
62   
63   def self.mylist au, page = 1, page_size = Author.default_ground_color_page_size
64     GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
65   end
66   
67   def self.himlist au, page = 1, page_size = Author.default_ground_color_page_size
68     GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).order('ground_colors.updated_at desc').offset((page -1) * page_size).limit(page_size)
69   end
70   
71   def self.list_paginate page = 1, page_size = self.default_page_size
72     Kaminari.paginate_array(Array.new(GroundColor.where(self.list_where()).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
73   end
74   
75   def self.mylist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
76     Kaminari.paginate_array(Array.new(GroundColor.where(self.mylist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
77   end
78   
79   def self.himlist_paginate au, page = 1, page_size = Author.default_ground_color_page_size
80     Kaminari.paginate_array(Array.new(GroundColor.where(self.himlist_where(au)).includes(GroundColor.list_opt).count, nil)).page(page).per(page_size)
81   end
82   
83   def self.list_opt
84     {:panel => {:author => {}} }
85   end
86   
87   def self.list_json_opt
88     {:include => {:panel => {:include => {:author => {}}} }}
89   end
90   
91   def self.show cid, roles
92     opt = {}
93     opt.merge!(GroundColor.show_opt)
94     res = GroundColor.find(cid, opt)
95     raise ActiveRecord::Forbidden unless res.visible?(roles)
96     res
97   end
98   
99   def self.show_opt
100     {:include => {:panel => {:author => {}} }}
101   end
102   
103   def self.show_json_opt
104     {:include => {:panel => {:include => {:author => {}}} }}
105   end
106   
107   def store au
108     if self.new_record?
109       self.panel.ground_colors.build(self.attributes)
110     else
111       self.panel.ground_colors.each do |ground_color|
112         next unless ground_color == self
113         attr = self.attributes
114         attr.delete 'id'
115         ground_color.attributes = attr
116         break
117       end
118     end
119     self.panel.store({}, au)
120   end
121   
122   def remove au
123     PanelPicture.transaction do
124       d = false
125       panel_pictures_attributes = {}
126       
127       self.panel.panel_pictures.each do |panel_picture|
128         attr = panel_picture.attributes
129         if panel_picture == self
130           attr['_destroy'] = true
131           d = true
132         else
133           if d
134             attr['t']  -= 1 
135           end
136         end
137         panel_pictures_attributes[panel_picture.id] = attr
138       end
139       self.panel.attributes = {:panel_pictures_attributes => panel_pictures_attributes}
140       self.panel.store({}, au)
141     end
142   end
143   
144   def scenario
145     ''
146   end
147   
148 end