OSDN Git Service

Merge branch 'v04' of git.sourceforge.jp:/gitroot/pettanr/pettanr
[pettanr/pettanr.git] / app / controllers / comics_controller.rb
1 class ComicsController < ApplicationController
2   layout 'test' if Pettanr::TestLayout
3   if Const.run_mode == 0
4     before_filter :authenticate_user!, :only => [:new, :create, :edit, :update, :destroy]
5   else
6     before_filter :authenticate_user!, :only => [:top, :index, :show, :play, :new, :create, :edit, :update, :destroy]
7   end
8   before_filter :authenticate_admin!, :only => [:list, :browse]
9
10   private
11   
12   def treat_param comic
13     comic.author_id = @author.id
14   end
15   
16   public
17   
18   def top
19     @new_comics = Comic.find(:all, 
20       :include => :author, :conditions => ['visible > 0'], :order => 'updated_at desc', :limit => 5
21     )
22     @new_pictures = OriginalPicture.find(:all, 
23       :include => [:artist, :license, :resource_picture], :order => 'updated_at', :limit => 5
24     )
25
26     respond_to do |format|
27       format.html # index.html.erb
28     end
29   end
30   
31   # GET /comics
32   # GET /comics.json
33   def index
34     @page = Comic.page params[:page]
35     @page_size = Comic.page_size params[:page_size]
36     @comics = Comic.list({}, @page, @page_size)
37     respond_to do |format|
38       format.html # index.html.erb
39       format.json { render json: @comics.to_json(Comic.list_json_opt) }
40     end
41   end
42
43   # GET /comics/1
44   # GET /comics/1.json
45   def show
46     @comic = Comic.show(params[:id], @author)
47
48     respond_to do |format|
49       format.html # show.html.erb
50       format.json { render json: @comic.to_json(Comic.show_json_include_opt) }
51     end
52   end
53
54   def count
55     @comic = {:count => Comic.visible_count}
56     respond_to do |format|
57       format.json { render json: @comic.to_json }
58     end
59   end
60   
61   def list
62     @comics = Comic.all
63
64     respond_to do |format|
65       format.html { render layout: 'system' }# index.html.erb
66       format.json { render json: @comics }
67     end
68   end
69
70   def browse
71     @comic = Comic.find(params[:id])
72
73     respond_to do |format|
74       format.html { render layout: 'system' } # show.html.erb
75       format.json { render json: @comic }
76     end
77   end
78   
79   # GET /comics/new
80   # GET /comics/new.js
81   def new
82     @comic = Comic.new
83     @comic.supply_default
84     respond_to do |format|
85       format.html # new.html.erb
86       format.js
87     end
88   end
89
90   # GET /comics/1/edit
91   # GET /comics/1.js/edit
92   def edit
93     @comic = Comic.show(params[:id], @author)
94     @comic.supply_default
95     respond_to do |format|
96       format.html 
97       format.js
98     end
99   end
100
101   # POST /comics
102   # POST /comics.json
103   def create
104     params[:comic].merge! author_id: @author.id
105     @comic = Comic.new params[:comic]
106     @comic.supply_default 
107
108     respond_to do |format|
109       if @comic.save
110         format.html { redirect_to @comic, notice: 'Comic was successfully created.' }
111         format.json { render json: Comic.show(@comic.id, @author).to_json(Comic.show_json_include_opt), status: :created, location: @comic }
112       else
113         format.html { render action: "new" }
114         format.json { render json: @comic.errors, status: :unprocessable_entity }
115       end
116     end
117   end
118
119   # PUT /comics/1
120   # PUT /comics/1.json
121   def update
122     params[:comic].merge! author_id: @author.id
123     @comic = Comic.show(params[:id], @author)
124     respond_to do |format|
125       if @comic.update_attributes(params[:comic])
126         format.html { redirect_to @comic, notice: 'Comic was successfully updated.' }
127         format.json { head :ok }
128       else
129         format.html { render action: "edit" }
130         format.json { render json: @comic.errors, status: :unprocessable_entity }
131       end
132     end
133   end
134
135   # DELETE /comics/1
136   # DELETE /comics/1.json
137   def destroy
138     @comic = Comic.find(params[:id])
139     if @comic.own? @author
140       @comic.destroy
141       respond_to do |format|
142         format.html { redirect_to comics_url }
143         format.json { head :ok }
144       end
145     else
146       format.html { render action: "edit" }
147       format.json { render json: @comic.errors, status: :unprocessable_entity }
148     end
149   end
150 end