OSDN Git Service

temp
[pettanr/pettanr.git] / app / controllers / scrolls_controller.rb
1 class ScrollsController < ApplicationController
2   layout 'test' if MagicNumber['test_layout']
3   if MagicNumber['run_mode'] == 0
4     before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
5     before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
6   else
7     before_filter :authenticate_reader, :only => [:top, :index, :show, :play]
8     before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
9     before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
10   end
11   before_filter :authenticate_admin!, :only => [:list, :browse]
12   
13   def self.model
14     Scroll
15   end
16   
17   def index
18     public_list
19   end
20
21   def show
22     @item = Scroll.show(params[:id], @operators)
23
24     respond_to do |format|
25       format.html {
26         @scroll = @item
27         if @operators.author
28           @new_panels = Panel.mylist(@operators.author, 1, 5)
29         end
30       }
31       format.json { render json: @item.to_json(Scroll.show_json_opt) }
32       format_prof format
33       format.atom 
34       format.rss 
35     end
36   end
37
38   def play
39     @scroll = Scroll.show(params[:id], [@user, @admin])
40     cnt = ScrollPanel.count(:conditions => ['scroll_id = ?', @scroll.id]).to_i
41     @offset = ScrollPanel.offset cnt, params[:offset]
42     @panel_count = ScrollPanel.panel_count cnt, params[:count]
43     @scroll_panels = ScrollPanel.play_list(@scroll, @author, @offset, @panel_count)
44     respond_to do |format|
45       format.html {
46         @prev_offset = if @offset > 0
47           if @offset - @panel_count < 0
48             0
49           else
50             @offset - @panel_count
51           end
52         else
53           nil
54         end
55         @next_offset = if @offset + @panel_count > cnt
56           nil
57         else
58           @offset + @panel_count
59         end
60         if @author
61           @new_panels = Panel.mylist(@author, 1, 5)
62         end
63       }
64       format.json {render text: ScrollPanel.list_as_json_text(@scroll_panels, @author)}
65       format.jsonp {
66         render :json => "callback(" + @scroll_panels.to_json_list + ");"
67       }
68     end
69   end
70   
71   def count
72     @scroll = {:count => Scroll.visible_count}
73     respond_to do |format|
74       format.json { render json: @scroll.to_json }
75     end
76   end
77   
78   def list
79     @scrolls = Scroll.all
80
81     respond_to do |format|
82       format.html { render layout: 'system' }# index.html.erb
83       format.json { render json: @scrolls }
84     end
85   end
86
87   def browse
88     @scroll = Scroll.find(params[:id])
89
90     respond_to do |format|
91       format.html { render layout: 'system' } # show.html.erb
92       format.json { render json: @scroll }
93     end
94   end
95   
96   def new
97     @scroll = Scroll.new
98     @scroll.supply_default
99     respond_to do |format|
100       format.html # new.html.erb
101       format.js
102       format.json { render json: @scroll.to_json(Scroll.show_json_opt) }
103     end
104   end
105
106   def edit
107     @scroll = Scroll.edit(params[:id], @author)
108     respond_to do |format|
109       format.html 
110       format.js
111     end
112   end
113
114   def create
115     @scroll = Scroll.new
116     @scroll.supply_default 
117     @scroll.attributes = params[:scroll]
118     @scroll.overwrite @operators
119
120     respond_to do |format|
121       if @scroll.save
122         flash[:notice] = I18n.t('flash.notice.created', :model => Scroll.model_name.human)
123         format.html { redirect_to @scroll }
124         format.json { render json: @scroll.to_json(Scroll.show_json_opt), status: :created, location: @scroll }
125       else
126         flash[:notice] = I18n.t('flash.notice.not_created', :model => Scroll.model_name.human)
127         format.html { render action: "new" }
128         format.json { render json: @scroll.errors, status: :unprocessable_entity }
129       end
130     end
131   end
132
133   def update
134     @scroll = Scroll.edit(params[:id], @author)
135     @scroll.attributes = params[:scroll]
136     @scroll.overwrite @author
137     respond_to do |format|
138       if @scroll.save
139         flash[:notice] = I18n.t('flash.notice.updated', :model => Scroll.model_name.human)
140         format.html { redirect_to @scroll }
141         format.json { head :ok }
142       else
143         flash[:notice] = I18n.t('flash.notice.not_updated', :model => Scroll.model_name.human)
144         format.html { render action: "edit" }
145         format.json { render json: @scroll.errors, status: :unprocessable_entity }
146       end
147     end
148   end
149
150   def destroy
151     @scroll = Scroll.edit(params[:id], @author)
152     respond_to do |format|
153       if @scroll.destroy_with_scroll_panel
154         flash[:notice] = I18n.t('flash.notice.destroyed', :model => Scroll.model_name.human)
155         format.html { redirect_to '/home/scrolls' }
156         format.json { head :ok }
157       else
158         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Scroll.model_name.human)
159         format.html { redirect_to @scroll }
160         format.json { render json: @scroll.errors, status: :unprocessable_entity }
161       end
162     end
163   end
164 end