OSDN Git Service

iroiro
[pettanr/pettanr.git] / app / controllers / comics_controller.rb
1 class ComicsController < ApplicationController
2   before_filter :authenticate_author!, :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 = current_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, :lisence, :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
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     @comic = Comic.new(params[:comic])
116     treat_param @comic
117
118     respond_to do |format|
119       if @comic.save
120         format.html { redirect_to @comic, notice: 'Comic was successfully created.' }
121         format.json { render json: @comic, status: :created, location: @comic }
122       else
123         format.html { render action: "new" }
124         format.json { render json: @comic.errors, status: :unprocessable_entity }
125       end
126     end
127   end
128
129   # PUT /comics/1
130   # PUT /comics/1.json
131   def update
132     @comic = Comic.find(params[:id])
133     if @comic.own? current_author
134       respond_to do |format|
135         if @comic.update_attributes(params[:comic])
136           format.html { redirect_to @comic, notice: 'Comic was successfully updated.' }
137           format.json { head :ok }
138         else
139           format.html { render action: "edit" }
140           format.json { render json: @comic.errors, status: :unprocessable_entity }
141         end
142       end
143     else
144       format.html { render action: "edit" }
145       format.json { render json: @comic.errors, status: :unprocessable_entity }
146     end
147   end
148
149   # DELETE /comics/1
150   # DELETE /comics/1.json
151   def destroy
152     @comic = Comic.find(params[:id])
153     if @comic.own? current_author
154       @comic.destroy
155       respond_to do |format|
156         format.html { redirect_to comics_url }
157         format.json { head :ok }
158       end
159     else
160       format.html { render action: "edit" }
161       format.json { render json: @comic.errors, status: :unprocessable_entity }
162     end
163   end
164 end