OSDN Git Service

fix extend
[pettanr/pettanr.git] / lib / peta / leaf.rb
1 module Peta
2   class Leaf < Content
3     self.abstract_class = true
4     
5     # Dynamic ClassMethods
6     
7     def self.load_manifest
8       super
9       # Class Methods
10       pm = Manifest.manifest.models[self.my_peta.parent_model_name].classify
11       define_singleton_method("parent_model") do 
12         pm
13       end
14       # Instance Methods
15     end
16     
17     def self.element?
18       true
19     end
20     
21     def self.root_model
22       self.parent_model
23     end
24     
25     def visible? operators
26       return false unless super
27       self.class.root_model.visible? operators
28     end
29     
30     def self.play_list_where cid
31       ['scroll_panels.scroll_id = ?', cid]
32     end
33     
34     def self.play_list scroll, author, offset = 0, limit = ScrollPanel.default_panel_size
35       ScrollPanel.where(self.play_list_where(scroll.id)).includes(ScrollPanel.list_opt).order('scroll_panels.t').offset(offset).limit(limit)
36     end
37     
38     def self.play_sheet_where sid
39       ['story_sheets.story_id = ?', sid]
40     end
41     
42     def self.play_sheet story, operators, page = 1
43       ss = StorySheet.where(self.play_sheet_where(story.id)).includes(StorySheet.list_opt).order('story_sheets.t').offset(page -1).limit(1).first
44       if ss 
45         if ss.sheet
46           Sheet.show(ss.sheet.id, operators)
47         else
48           nil
49         end
50       else
51         nil
52       end
53     end
54     
55     def self.play_paginate story, page
56       Kaminari.paginate_array(Array.new(StorySheet.where(self.play_sheet_where(story.id)).includes(StorySheet.list_opt).count, nil)).page(page).per(1)
57     end
58     
59     def self.new_t binder_id
60       r = ScrollPanel.max_t(binder_id)
61       r.blank? ? 0 : r.to_i + 1
62     end
63     
64     def self.max_t binder_id
65       self.class..maximum(:t, :conditions => ['scroll_id = ?', binder_id])
66     end
67     
68     def self.find_t binder_id, t
69       self.class.find(:first, :conditions => ['scroll_id = ? and t = ?', binder_id, t])
70     end
71     
72     def self.collect_t scroll_panel
73       r = self.class.find(:all, :conditions => ['scroll_id = ?', scroll_panel.scroll_id], :order => 't')
74       r.map {|sp| sp.t}
75     end
76     
77     def self.top_sheet story, author
78       StorySheet.play_list(story, author).first
79     end
80     
81     def self.collect_t story_sheet
82       r = StorySheet.find(:all, :conditions => ['story_id = ?', story_sheet.story_id], :order => 't')
83       r.map {|sp| sp.t}
84     end
85     
86     def self.serial? ary
87       i = 0
88       ary.compact.sort.each do |t|
89         break false unless t == i
90         i += 1
91       end
92       ary.compact.size == i
93     end
94     
95     def self.validate_t scroll_panel
96       ScrollPanel.serial?(ScrollPanel.collect_t(scroll_panel))
97     end
98     
99     def insert_shift
100       ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and t >= ?', self.scroll_id, self.t])
101     end
102     
103     def lesser_shift old_t
104       self.t = 0 if self.t < 0
105       ScrollPanel.update_all('t = t + 1', ['scroll_id = ? and (t >= ? and t < ?)', self.scroll_id, self.t, old_t])
106     end
107     
108     def higher_shift old_t
109       nf = ScrollPanel.find_t(self.scroll_id, self.t)
110       max_t = ScrollPanel.max_t(self.scroll_id).to_i
111       self.t = max_t if self.t > max_t
112       ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ? and t <= ?)', self.scroll_id, old_t, self.t])
113     end
114     
115     def update_shift old_t
116       if self.t > old_t
117         higher_shift old_t
118       else
119         lesser_shift old_t
120       end
121     end
122     
123     def rotate old_t = nil
124       if self.new_record?
125         if self.t.blank?
126           self.t = ScrollPanel.new_t self.scroll_id
127         else
128           self.insert_shift
129         end
130       else
131         if self.t.blank?
132         else
133           self.update_shift old_t
134         end
135       end
136     end
137     
138     def allow? operators
139       return nil if self.scroll_id == nil or self.panel_id == nil
140       self.scroll.own?(operators) and self.panel.usable?(operators)
141     end
142     
143     def destroy_and_shorten
144       res = false
145       ScrollPanel.transaction do
146         ScrollPanel.update_all('t = t - 1', ['scroll_id = ? and (t > ?)', self.scroll_id, self.t])
147         raise ActiveRecord::Rollback unless self.destroy
148         res = true
149       end
150       res
151     end
152     
153   end
154 end
155