OSDN Git Service

6bffe7d93a32accfc0cb97c940ff903c94755238
[pettanr/pettanr.git] / app / controllers / scrolls_controller.rb
1 class ScrollsController < ApplicationController
2   if Manifest.manifest.magic_numbers['run_mode'] == 0
3     before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
4     before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
5   else
6     before_filter :authenticate_reader, :only => [:top, :index, :show, :play, :by_author, :by_panel]
7     before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
8     before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
9   end
10   
11   def self.model
12     Scroll
13   end
14   
15   def index
16     public_list
17   end
18   
19   def by_author
20     filter_list
21   end
22   
23   def by_panel
24     filter_list
25   end
26   
27   def show
28     @item = Scroll.show(params[:id], @operators)
29
30     respond_to do |format|
31       format.html {
32         if @operators.author
33           @new_panel_items = assist_items('panel', 'private_list')
34           @new_panel_filer = assist_filer 'panel', @new_panel_items
35         end
36       }
37       format.json { render json: @item.to_json(Scroll.show_json_opt) }
38       format_prof format
39       format.atom 
40       format.rss 
41     end
42   end
43
44   def play
45     @item = self.class.model.show(params[:id], @operators)
46     action_name = params[:action]
47     @action = self.class.controller.open(action_name, params, @operators)
48     @items = @action.list.items @item
49     respond_to do |format|
50       format.html {
51         @count = @action.list.count @item
52         @prev_offset = @action.list.prev_offset @item
53         @next_offset = @action.list.next_offset @item
54         if @operators.author
55           @new_panel_items = assist_items('panel', 'private_list')
56           @new_panel_filer = assist_filer 'panel', @new_panel_items
57         end
58       }
59       format.json { render json: @items.to_json(self.class.model.list_json_opt) }
60     end
61   end
62   
63   def count
64     @scroll = {:count => Scroll.visible_count}
65     respond_to do |format|
66       format.json { render json: @scroll.to_json }
67     end
68   end
69   
70   def scroll_panels_count
71     has_many_list
72   end
73   
74   def panels_count
75     has_many_list
76   end
77   
78   def count_by_author
79     filter_list
80   end
81   
82   def count_by_panel
83     filter_list
84   end
85   
86   def new
87     @scroll = Scroll.new
88     @scroll.supply_default
89     respond_to do |format|
90       format.html
91       format.js
92       format.json { render json: @scroll.to_json(Scroll.show_json_opt) }
93     end
94   end
95
96   def edit
97     @scroll = Scroll.edit(params[:id], @operators)
98     respond_to do |format|
99       format.html 
100       format.js
101     end
102   end
103
104   def create
105     @scroll = Scroll.new
106     @scroll.supply_default 
107     @scroll.attributes = params[:scroll]
108     @scroll.overwrite @operators
109
110     respond_to do |format|
111       if @scroll.save
112         flash[:notice] = I18n.t('flash.notice.created', :model => Scroll.model_name.human)
113         format.html { redirect_to @scroll }
114         format.json { render json: @scroll.to_json(Scroll.show_json_opt), status: :created, location: @scroll }
115       else
116         flash[:notice] = I18n.t('flash.notice.not_created', :model => Scroll.model_name.human)
117         format.html { render action: "new" }
118         format.json { render json: @scroll.errors, status: :unprocessable_entity }
119       end
120     end
121   end
122
123   def update
124     @scroll = Scroll.edit(params[:id], @operators)
125     @scroll.attributes = params[:scroll]
126     @scroll.overwrite @operators
127     respond_to do |format|
128       if @scroll.save
129         flash[:notice] = I18n.t('flash.notice.updated', :model => Scroll.model_name.human)
130         format.html { redirect_to @scroll }
131         format.json { head :ok }
132       else
133         flash[:notice] = I18n.t('flash.notice.not_updated', :model => Scroll.model_name.human)
134         format.html { render action: "edit" }
135         format.json { render json: @scroll.errors, status: :unprocessable_entity }
136       end
137     end
138   end
139
140   def destroy
141     @scroll = Scroll.edit(params[:id], @operators)
142     respond_to do |format|
143       if @scroll.destroy_with_scroll_panel
144         flash[:notice] = I18n.t('flash.notice.destroyed', :model => Scroll.model_name.human)
145         format.html { redirect_to '/home/scrolls' }
146         format.json { head :ok }
147       else
148         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Scroll.model_name.human)
149         format.html { redirect_to @scroll }
150         format.json { render json: @scroll.errors, status: :unprocessable_entity }
151       end
152     end
153   end
154 end