OSDN Git Service

t#31470:create pager
[pettanr/pettanr.git] / app / models / ground_picture.rb
1 class GroundPicture < ActiveRecord::Base
2   belongs_to :panel
3   belongs_to :picture
4   
5   validates :panel_id, :numericality => {:allow_blank => true}
6   validates :repeat, :numericality => true, :inclusion => { :in => 0..3 }
7   validates :x, :numericality => true
8   validates :y, :numericality => true
9   validates :picture_id, :numericality => true, :existence => {:both => false}
10   validates :z, :presence => true, :numericality => {:greater_than => 0}
11   
12   def supply_default
13     self.repeat = 0
14     self.x = 0
15     self.y = 0
16   end
17   
18   def overwrite
19   end
20   
21   def visible? roles
22     if MagicNumber['run_mode'] == 0
23       return false unless guest_role_check(roles)
24     else
25       return false unless reader_role_check(roles)
26     end
27     return true if self.panel.own?(roles)
28     self.panel.visible? roles
29   end
30   
31   def self.default_page_size
32     25
33   end
34   
35   def self.max_page_size
36     100
37   end
38   
39   def self.page prm = nil
40     page = prm.to_i
41     page = 1 if page < 1
42     page
43   end
44   
45   def self.page_size prm = self.default_page_size
46     page_size = prm.to_i
47     page_size = self.max_page_size if page_size > self.max_page_size
48     page_size = self.default_page_size if page_size < 1
49     page_size
50   end
51   
52   def self.list_where
53     'panels.publish > 0'
54   end
55   
56   def self.mylist_where au
57     ['panels.author_id = ?', au.id]
58   end
59   
60   def self.himlist_where au
61     ['panels.author_id = ? and panels.publish > 0', au.id]
62   end
63   
64   def self.list page = 1, page_size = self.default_page_size
65     GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
66   end
67   
68   def self.mylist au, page = 1, page_size = Author.default_ground_picture_page_size
69     GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
70   end
71   
72   def self.himlist au, page = 1, page_size = Author.default_ground_picture_page_size
73     GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).order('ground_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
74   end
75   
76   def self.list_paginate page = 1, page_size = self.default_page_size
77     Kaminari.paginate_array(Array.new(GroundPicture.where(self.list_where()).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
78   end
79   
80   def self.mylist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
81     Kaminari.paginate_array(Array.new(GroundPicture.where(self.mylist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
82   end
83   
84   def self.himlist_paginate au, page = 1, page_size = Author.default_ground_picture_page_size
85     Kaminari.paginate_array(Array.new(GroundPicture.where(self.himlist_where(au)).includes(GroundPicture.list_opt).count, nil)).page(page).per(page_size)
86   end
87   
88   def self.list_opt
89     {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}} }
90   end
91   
92   def self.list_json_opt
93     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
94   end
95   
96   def self.show cid, au
97     opt = {}
98     opt.merge!(GroundPicture.show_opt)
99     res = GroundPicture.find(cid, opt)
100     raise ActiveRecord::Forbidden unless res.visible?(au)
101     res
102   end
103   
104   def self.show_opt
105     {:include => {:panel => {:author => {}}, :picture => {:artist => {}, :license => {}}}}
106   end
107   
108   def self.show_json_opt
109     {:include => {:panel => {:include => {:author => {}}}, :picture => {:include => {:artist => {}, :license => {}}} }}
110   end
111   
112   def scenario
113     ''
114   end
115   
116 end