OSDN Git Service

t#31297:fix author's list
[pettanr/pettanr.git] / app / controllers / authors_controller.rb
1 class AuthorsController < ApplicationController
2   layout 'test' if MagicNumber['test_layout']
3   if MagicNumber['run_mode'] == 0
4     before_filter :authenticate_user, :only => [:new, :create, :edit, :update]
5     before_filter :authenticate_author, :only => [:edit, :update]
6   else
7     before_filter :authenticate_reader, :only => [:index, :show, :comics, :stories, :panels]
8     before_filter :authenticate_user, :only => [:new, :create, :edit, :update]
9     before_filter :authenticate_author, :only => [:edit, :update]
10   end
11   before_filter :authenticate_admin!, :only => [:list, :browse]
12
13   def index
14     @page = Author.page params[:page]
15     @page_size = Author.page_size params[:page_size]
16     @authors = Author.list(@page, @page_size)
17
18     respond_to do |format|
19       format.html # index.html.erb
20       format.json { render :json => @authors.to_json(Author.list_json_opt) }
21     end
22   end
23
24   def show
25     @au = Author.show(params[:id], [@user, @admin])
26
27     respond_to do |format|
28       format.html
29       format.json { render :json => @au.to_json(Author.show_json_opt) }
30     end
31   end
32
33   def comics
34     @au = Author.show(params[:id], [@user, @admin])
35
36     @page = Author.page params[:page]
37     @page_size = Author.comic_page_size params[:page_size]
38     @comics = Comic.himlist(@au, @page, @page_size)
39
40     respond_to do |format|
41       format.html # index.html.erb
42       format.json { render json: @comics.to_json(Comic.list_json_opt) }
43     end
44   end
45   
46   def stories
47     @au = Author.show(params[:id], [@user, @admin])
48
49     @page = Author.page params[:page]
50     @page_size = Author.comic_page_size params[:page_size]
51     @stories = Story.himlist(@au, @page, @page_size)
52
53     respond_to do |format|
54       format.html # index.html.erb
55       format.json { render json: @stories.to_json(Story.list_json_opt) }
56     end
57   end
58   
59   def panels
60     @au = Author.show(params[:id], [@user, @admin])
61
62     @page = Author.page params[:page]
63     @page_size = Author.panel_page_size params[:page_size]
64     @panels = Panel.himlist(@au, @page, @page_size)
65
66     respond_to do |format|
67       format.html # index.html.erb
68       format.json { render text: Panel.list_as_json_text(@panels) }
69     end
70   end
71   
72   def count
73     @au = {:count => Author.visible_count}
74     respond_to do |format|
75       format.json { render json: @au.to_json }
76     end
77   end
78   
79   def list
80     @authors = Author.all
81
82     respond_to do |format|
83       format.html { render layout: 'system' }
84     end
85   end
86
87   def browse
88     @au = Author.find(params[:id])
89
90     respond_to do |format|
91       format.html { render layout: 'system' }
92     end
93   end
94   
95   def new
96     @au = Author.new
97     @au.supply_default 
98
99     respond_to do |format|
100       format.html # new.html.erb
101       format.js
102       format.json { render json: @au.to_json(Author.show_json_opt) }
103     end
104   end
105
106   def edit
107     @au = Author.edit(params[:id], @author)
108   end
109
110   def create
111     @au = Author.new()
112     @au.supply_default 
113     @au.attributes = params[:author]
114     @au.overwrite @user.id
115     respond_to do |format|
116       if @au.save
117         flash[:notice] = I18n.t('flash.notice.created', :model => Author.model_name.human)
118         format.html { redirect_to root_path }
119         format.json { render json: @au.to_json(Author.show_json_opt), status: :created, location: @artist }
120       else
121         format.html { render action: "new" }
122         format.json { render json: @au.errors, status: :unprocessable_entity }
123       end
124     end
125   end
126
127   def update
128     @au = Author.edit(params[:id], @author)
129     @au.attributes = params[:author]
130     @au.overwrite @user.id
131
132     respond_to do |format|
133       if @au.save
134         flash[:notice] = I18n.t('flash.notice.updated', :model => Author.model_name.human)
135         format.html { redirect_to '/home/configure' }
136         format.json { head :ok }
137       else
138         format.html { render action: "edit" }
139         format.json { render json: @au.errors, status: :unprocessable_entity }
140       end
141     end
142   end
143 end