OSDN Git Service

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