OSDN Git Service

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