OSDN Git Service

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