OSDN Git Service

f5c834d76ec4b66772d5fcef2bd9bab3609164af
[pettanr/pettanr.git] / app / controllers / speech_balloons_controller.rb
1 class SpeechBalloonsController < ApplicationController
2   layout 'test' if MagicNumber['test_layout']
3   if MagicNumber['run_mode'] == 0
4     before_filter :authenticate_user, :only => [:new, :edit, :create, :update, :destroy]
5     before_filter :authenticate_author, :only => [:new, :edit, :create, :update, :destroy]
6   else
7     before_filter :authenticate_reader, :only => [:index, :show]
8     before_filter :authenticate_user, :only => [:new, :edit, :create, :update, :destroy]
9     before_filter :authenticate_author, :only => [:new, :edit, :create, :update, :destroy]
10   end
11   before_filter :authenticate_admin!, :only => [:list, :browse]
12
13   def index
14     @page = SpeechBalloon.page params[:page]
15     @page_size = SpeechBalloon.page_size params[:page_size]
16     @speech_balloons = SpeechBalloon.list(@page, @page_size)
17
18     respond_to do |format|
19       format.html {
20         @paginate = SpeechBalloon.list_paginate(@page, @page_size)
21       }
22       format.json { render json: @speech_balloons.to_json(SpeechBalloon.list_json_opt) }
23     end
24   end
25   
26   def show
27     @speech_balloon = SpeechBalloon.show(params[:id], [@user, @admin])
28     respond_to do |format|
29       format.html # show.html.erb
30       format.json { render json: @speech_balloon.to_json(SpeechBalloon.show_json_opt) }
31     end
32   end
33   
34   def list
35     @speech_balloons = SpeechBalloon.all
36
37     respond_to do |format|
38       format.html { render layout: 'system' }
39       format.json { render json: @speech_balloons }
40     end
41   end
42
43   def browse
44     @speech_balloon = SpeechBalloon.find(params[:id])
45
46     respond_to do |format|
47       format.html { render layout: 'system' }
48       format.json { render json: @speech_balloon }
49     end
50   end
51
52   def new
53     @speech_balloon_template = SpeechBalloonTemplate.show params[:speech_balloon_template_id], @author
54     
55     @panel = Panel.edit(@author.working_panel, @author)
56     @speech_balloon = SpeechBalloon.new :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
57     @speech_balloon.supply_default
58     @speech_balloon.build_balloon 
59     @speech_balloon.build_speech 
60     @speech_balloon.balloon.supply_default 
61     @speech_balloon.speech.supply_default 
62
63     respond_to do |format|
64       format.html
65       format.json { render :json => @speech_balloon.to_json(SpeechBalloon.show_json_opt) }
66     end
67   end
68
69   def edit
70     @speech_balloon = SpeechBalloon.show(params[:id], @author)
71     @panel = Panel.edit(@speech_balloon.panel.id, @author)
72     
73     respond_to do |format|
74       format.html
75     end
76   end
77
78   def create
79     @panel = Panel.edit(@author.working_panel, @author)
80     
81     @speech_balloon = SpeechBalloon.new 
82     @speech_balloon.attributes = params[:speech_balloon]
83     @speech_balloon.overwrite @panel.id
84     
85     respond_to do |format|
86       if @speech_balloon.valid?
87         if @speech_balloon.store @author, params[:speech_balloon]
88           flash[:notice] = I18n.t('flash.notice.created', :model => Panel.model_name.human)
89           format.html { redirect_to @panel }
90           format.json { render json: @panel.panel_elements_as_json, status: :created, location: @panel }
91         else
92           flash[:notice] = I18n.t('flash.notice.not_created', :model => SpeechBalloon.model_name.human)
93           format.html { render action: "new" }
94           format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
95         end
96       else
97         flash[:notice] = I18n.t('flash.notice.not_created', :model => SpeechBalloon.model_name.human)
98         format.html { render action: "new" }
99         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
100       end
101     end
102   end
103
104   def update
105     @speech_balloon = SpeechBalloon.show(params[:id], @author)
106     @speech_balloon.attributes = params[:speech_balloon]
107     @panel = Panel.edit(@speech_balloon.panel.id, @author)
108     @speech_balloon.overwrite @panel.id
109     
110     respond_to do |format|
111       if @speech_balloon.store @author, params[:speech_balloon]
112         flash[:notice] = I18n.t('flash.notice.updated', :model => SpeechBalloon.model_name.human)
113         format.html { redirect_to @speech_balloon }
114         format.json { head :ok }
115       else
116         flash[:notice] = I18n.t('flash.notice.not_updated', :model => SpeechBalloon.model_name.human)
117         format.html { render action: "edit" }
118         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
119       end
120     end
121   end
122
123   def destroy
124     @speech_balloon = SpeechBalloon.show(params[:id], @author)
125     @panel = Panel.edit(@speech_balloon.panel.id, @author)
126     
127     respond_to do |format|
128       if @speech_balloon.remove @author
129         flash[:notice] = I18n.t('flash.notice.destroyed', :model => SpeechBalloon.model_name.human)
130         format.html { redirect_to @panel }
131         format.json { head :ok }
132       else
133         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => SpeechBalloon.model_name.human)
134         format.html { redirect_to @speech_balloon }
135         format.json { render json: @speech_balloon.errors, status: :unprocessable_entity }
136       end
137     end
138   end
139   
140 end