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