OSDN Git Service

list browser created
[pettanr/pettanr.git] / app / controllers / panels_controller.rb
1 class PanelsController < ApplicationController
2   before_filter :authenticate_author!, :only => [:index, :show, :create, :update, :destroy]
3   before_filter :authenticate_admin!, :only => [:list, :browse]
4
5   private
6   
7   def treat_param panel
8     panel.author_id = current_author.id
9   end
10   
11   public
12   
13   # GET /panels
14   # GET /panels.json
15   def index
16     @panels = Panel.find(:all, :include => [:comic, :author], :order => 'updated_at', :limit => 20)
17
18     respond_to do |format|
19       format.html # index.html.erb
20       format.json { render :json => @panels.to_json(
21         :include => [:comic, :author]
22       ) }
23     end
24   end
25
26   # GET /panels/1
27   # GET /panels/1.json
28   def show
29     @panel = Panel.find(params[:id], include: [:comic, :panel_pictures => :resource_picture, :balloons => :speaches])# only: [:width, :height])
30
31     respond_to do |format|
32       format.html # show.html.erb
33        format.json {
34         render :json => @panel.to_json(include: {
35           :comic => {}, :panel_pictures => {:include => :image}, :fukidashis => {:include => :serifus}
36         })
37       }
38       format.jsonp {
39         render :json => "callback(" + @panel.to_json(include: {
40           :comic => {}, :panel_pictures => {:include => :image}, :fukidashis => {:include => :serifus}
41         }) + ");"
42       }
43 #      format.json { render :json => @frame.to_json(include: {
44 #        :comic => {:only => :title}, :panel_pictures => {:include => {:image => {:only => [:width]}},:only => [:width, :height, :z, :image_id]}
45 #      }, only: [:border]) }
46     end
47   end
48
49   def list
50     @panels = Panel.all :order => 'updated_at'
51
52     respond_to do |format|
53       format.html { render layout: 'system' }
54       format.json { render json: @panels }
55     end
56   end
57
58   def browse
59     @panel = Panel.find(params[:id])
60
61     respond_to do |format|
62       format.html { render layout: 'system' }
63     end
64   end
65
66   # POST /panels
67   # POST /panels.json
68   def create
69     @panel = Panel.new(params[:panel])
70     treat_param @panel
71     @comic = Comic.find @panel.comic_id
72
73     respond_to do |format|
74       Panel.transaction do
75         if @panel.vdt_save
76           format.html { redirect_to @panel, notice: 'Panel was successfully created.' }
77           format.json { render json: @panel, status: :created, location: @panel }
78         else
79           format.html { render action: "new" }
80           format.json { render json: @panel.errors, status: :unprocessable_entity }
81         end
82       end
83     end
84   end
85
86   # PUT /panels/1
87   # PUT /panels/1.json
88   def update
89     @panel = Panel.find(params[:id])
90     if @panel.own? current_author
91       respond_to do |format|
92         Panel.transaction do
93           if params[:panel][:t] and params[:panel][:t].to_i != @panel.t
94             @panel.move_to params[:panel][:t].to_i
95           end
96           if @panel.update_attributes(params[:panel])
97             format.html { redirect_to @panel, notice: 'Panel was successfully updated.' }
98             format.json { head :ok }
99           else
100             format.html { render action: "edit" }
101             format.json { render json: @panel.errors, status: :unprocessable_entity }
102           end
103         end
104       end
105     else
106       format.html { render action: "edit" }
107       format.json { render json: @panel.errors, status: :unprocessable_entity }
108     end
109   end
110
111   # DELETE /panels/1
112   # DELETE /panels/1.json
113   def destroy
114     @panel = Panel.find(params[:id])
115     if @panel.own? current_author
116       respond_to do |format|
117         Panel.transaction do
118           @panel.destroy_and_shorten
119           format.html { redirect_to panels_url }
120           format.json { head :ok }
121         end
122       end
123     else
124       format.html { render action: "edit" }
125       format.json { render json: @panel.errors, status: :unprocessable_entity }
126     end
127   end
128   
129   def move
130     @panel = Panel.find(params[:id])
131     @new_seq = params[:panel][:t].to_i
132     respond_to do |format|
133       Panel.transaction do
134         if @panel.move_to(@new_t)
135           format.html { redirect_to @panel, notice: 'Panel was successfully moved.' }
136           format.json { head :ok }
137         else
138           format.html { render action: "show" }
139           format.json { render json: @panel.errors, status: :unprocessable_entity }
140         end
141       end
142     end
143   end
144   
145 end