OSDN Git Service

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