OSDN Git Service

t#31537:fix flash
[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 index
14     @page = Comic.page params[:page]
15     @page_size = Comic.page_size params[:page_size]
16     @comics = Comic.list(@page, @page_size)
17     respond_to do |format|
18       format.html {
19         @paginate = Comic.list_paginate(@page, @page_size)
20       }
21       format.json { render json: @comics.to_json(Comic.list_json_opt) }
22       format.atom 
23       format.rss
24     end
25   end
26
27   def show
28     @comic = Comic.show(params[:id], [@user, @admin])
29
30     respond_to do |format|
31       format.html # show.html.erb
32       format.json { render json: @comic.to_json(Comic.show_json_opt) }
33       format.atom 
34       format.rss 
35     end
36   end
37
38   def count
39     @comic = {:count => Comic.visible_count}
40     respond_to do |format|
41       format.json { render json: @comic.to_json }
42     end
43   end
44   
45   def list
46     @comics = Comic.all
47
48     respond_to do |format|
49       format.html { render layout: 'system' }# index.html.erb
50       format.json { render json: @comics }
51     end
52   end
53
54   def browse
55     @comic = Comic.find(params[:id])
56
57     respond_to do |format|
58       format.html { render layout: 'system' } # show.html.erb
59       format.json { render json: @comic }
60     end
61   end
62   
63   def new
64     @comic = Comic.new
65     @comic.supply_default
66     respond_to do |format|
67       format.html # new.html.erb
68       format.js
69       format.json { render json: @comic.to_json(Comic.show_json_opt) }
70     end
71   end
72
73   def edit
74     @comic = Comic.edit(params[:id], @author)
75     respond_to do |format|
76       format.html 
77       format.js
78     end
79   end
80
81   def create
82     @comic = Comic.new
83     @comic.supply_default 
84     @comic.attributes = params[:comic]
85     @comic.overwrite @author
86
87     respond_to do |format|
88       if @comic.save
89         flash[:notice] = I18n.t('flash.notice.created', :model => Comic.model_name.human)
90         format.html { redirect_to @comic }
91         format.json { render json: @comic.to_json(Comic.show_json_opt), status: :created, location: @comic }
92       else
93         flash[:notice] = I18n.t('flash.notice.not_created', :model => Comic.model_name.human)
94         format.html { render action: "new" }
95         format.json { render json: @comic.errors, status: :unprocessable_entity }
96       end
97     end
98   end
99
100   def update
101     @comic = Comic.edit(params[:id], @author)
102     @comic.attributes = params[:comic]
103     @comic.overwrite @author
104     respond_to do |format|
105       if @comic.save
106         flash[:notice] = I18n.t('flash.notice.updated', :model => Comic.model_name.human)
107         format.html { redirect_to @comic }
108         format.json { head :ok }
109       else
110         flash[:notice] = I18n.t('flash.notice.not_updated', :model => Comic.model_name.human)
111         format.html { render action: "edit" }
112         format.json { render json: @comic.errors, status: :unprocessable_entity }
113       end
114     end
115   end
116
117   def destroy
118     @comic = Comic.edit(params[:id], @author)
119     respond_to do |format|
120       if @comic.destroy_with_story
121         flash[:notice] = I18n.t('flash.notice.destroyed', :model => Comic.model_name.human)
122         format.html { redirect_to '/home/comics' }
123         format.json { head :ok }
124       else
125         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Comic.model_name.human)
126         format.html { redirect_to @comic }
127         format.json { render json: @comic.errors, status: :unprocessable_entity }
128       end
129     end
130   end
131 end