OSDN Git Service

add editor
[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, :scroll_panels, :panels, :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 scroll_panels
20     has_many_list
21   end
22   
23   def panels
24     has_many_list
25   end
26   
27   def by_author
28     filter_list
29   end
30   
31   def by_panel
32     filter_list
33   end
34   
35   def show
36     @item = Scroll.show(params[:id], @operators)
37
38     respond_to do |format|
39       format.html {
40         if @operators.author
41           @new_panel_items = assist_items('panel', 'private_list')
42           @new_panel_filer = assist_filer 'panel', @new_panel_items
43         end
44       }
45       format.json { render json: @item.to_json(Scroll.show_json_opt) }
46       format_prof format
47       format.atom 
48       format.rss 
49     end
50   end
51
52   def play
53     @item = self.class.model.show(params[:id], @operators)
54     action_name = params[:action]
55     @action = self.class.controller.open(action_name, params, @operators)
56     @items = @action.list.items @item
57     respond_to do |format|
58       format.html {
59         @count = @action.list.count @item
60         @prev_offset = @action.list.prev_offset @item
61         @next_offset = @action.list.next_offset @item
62         if @operators.author
63           @new_panel_items = assist_items('panel', 'private_list')
64           @new_panel_filer = assist_filer 'panel', @new_panel_items
65         end
66       }
67       format.json { render json: @items.to_json(self.class.model.list_json_opt) }
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 new
79     @scroll = Scroll.new
80     @scroll.supply_default
81     respond_to do |format|
82       format.html
83       format.js
84       format.json { render json: @scroll.to_json(Scroll.show_json_opt) }
85     end
86   end
87
88   def edit
89     @scroll = Scroll.edit(params[:id], @operators)
90     respond_to do |format|
91       format.html 
92       format.js
93     end
94   end
95
96   def create
97     @scroll = Scroll.new
98     @scroll.supply_default 
99     @scroll.attributes = params[:scroll]
100     @scroll.overwrite @operators
101
102     respond_to do |format|
103       if @scroll.save
104         flash[:notice] = I18n.t('flash.notice.created', :model => Scroll.model_name.human)
105         format.html { redirect_to @scroll }
106         format.json { render json: @scroll.to_json(Scroll.show_json_opt), status: :created, location: @scroll }
107       else
108         flash[:notice] = I18n.t('flash.notice.not_created', :model => Scroll.model_name.human)
109         format.html { render action: "new" }
110         format.json { render json: @scroll.errors, status: :unprocessable_entity }
111       end
112     end
113   end
114
115   def update
116     @scroll = Scroll.edit(params[:id], @operators)
117     @scroll.attributes = params[:scroll]
118     @scroll.overwrite @operators
119     respond_to do |format|
120       if @scroll.save
121         flash[:notice] = I18n.t('flash.notice.updated', :model => Scroll.model_name.human)
122         format.html { redirect_to @scroll }
123         format.json { head :ok }
124       else
125         flash[:notice] = I18n.t('flash.notice.not_updated', :model => Scroll.model_name.human)
126         format.html { render action: "edit" }
127         format.json { render json: @scroll.errors, status: :unprocessable_entity }
128       end
129     end
130   end
131
132   def destroy
133     @scroll = Scroll.edit(params[:id], @operators)
134     respond_to do |format|
135       if @scroll.destroy_with_scroll_panel
136         flash[:notice] = I18n.t('flash.notice.destroyed', :model => Scroll.model_name.human)
137         format.html { redirect_to '/home/scrolls' }
138         format.json { head :ok }
139       else
140         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Scroll.model_name.human)
141         format.html { redirect_to @scroll }
142         format.json { render json: @scroll.errors, status: :unprocessable_entity }
143       end
144     end
145   end
146 end