OSDN Git Service

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