OSDN Git Service

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