OSDN Git Service

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