OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / app / controllers / comics_controller.rb
1 class ComicsController < 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]
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     Comic
15   end
16   def self.controller
17     Pettanr::Application::manifest.controller_managers[self.model.item_name]
18   end
19   def self.profiler_manager
20     Pettanr::Application::manifest.profiler_managers[self.model.item_name]
21   end
22   
23   def index
24     action_name = params[:action]
25     @action = self.class.controller.open(action_name, params, @operators)
26     @items = @action.items 
27     respond_to do |format|
28       format.html {
29         @filer = @action.filer
30         render :template => 'system/filer', :locals => {
31           :filer => @filer
32         }
33       }
34       format.json { render json: @items.to_json(self.class.model.list_json_opt) }
35       format.atom 
36       format.rss
37     end
38   end
39
40   def show
41     @item = Comic.show(params[:id], @operators)
42     respond_to do |format|
43       format.html {
44         @comic = @item
45       }
46       format.prof { 
47         @profiler = @@profiler_manager.open(@item, @operators)
48         render :template => 'system/prof', :locals => {
49           :profiler => @profiler
50         }
51       }
52       format.json { render json: @item.to_json(Comic.show_json_opt) }
53       format.atom 
54       format.rss 
55     end
56   end
57
58   def count
59     @comic = {:count => Comic.visible_count}
60     respond_to do |format|
61       format.json { render json: @comic.to_json }
62     end
63   end
64   
65   def list
66     @comics = Comic.all
67
68     respond_to do |format|
69       format.html { render layout: 'system' }# index.html.erb
70       format.json { render json: @comics }
71     end
72   end
73
74   def browse
75     @comic = Comic.find(params[:id])
76
77     respond_to do |format|
78       format.html { render layout: 'system' } # show.html.erb
79       format.json { render json: @comic }
80     end
81   end
82   
83   def new
84     @comic = Comic.new
85     @comic.supply_default
86     respond_to do |format|
87       format.html # new.html.erb
88       format.js
89       format.json { render json: @comic.to_json(Comic.show_json_opt) }
90     end
91   end
92
93   def edit
94     @comic = Comic.edit(params[:id], @author)
95     respond_to do |format|
96       format.html 
97       format.js
98     end
99   end
100
101   def create
102     @comic = Comic.new
103     @comic.supply_default 
104     @comic.attributes = params[:comic]
105     @comic.overwrite @author
106
107     respond_to do |format|
108       if @comic.save
109         flash[:notice] = I18n.t('flash.notice.created', :model => Comic.model_name.human)
110         format.html { redirect_to @comic }
111         format.json { render json: @comic.to_json(Comic.show_json_opt), status: :created, location: @comic }
112       else
113         flash[:notice] = I18n.t('flash.notice.not_created', :model => Comic.model_name.human)
114         format.html { render action: "new" }
115         format.json { render json: @comic.errors, status: :unprocessable_entity }
116       end
117     end
118   end
119
120   def update
121     @comic = Comic.edit(params[:id], @author)
122     @comic.attributes = params[:comic]
123     @comic.overwrite @author
124     respond_to do |format|
125       if @comic.save
126         flash[:notice] = I18n.t('flash.notice.updated', :model => Comic.model_name.human)
127         format.html { redirect_to @comic }
128         format.json { head :ok }
129       else
130         flash[:notice] = I18n.t('flash.notice.not_updated', :model => Comic.model_name.human)
131         format.html { render action: "edit" }
132         format.json { render json: @comic.errors, status: :unprocessable_entity }
133       end
134     end
135   end
136
137   def destroy
138     @comic = Comic.edit(params[:id], @author)
139     respond_to do |format|
140       if @comic.destroy_with_story
141         flash[:notice] = I18n.t('flash.notice.destroyed', :model => Comic.model_name.human)
142         format.html { redirect_to '/home/comics' }
143         format.json { head :ok }
144       else
145         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Comic.model_name.human)
146         format.html { redirect_to @comic }
147         format.json { render json: @comic.errors, status: :unprocessable_entity }
148       end
149     end
150   end
151 end