OSDN Git Service

1072cc93a1f7bb2f6218717d790fd7cabfc7de09
[pettanr/pettanr.git] / app / controllers / speach_balloons_controller.rb
1 class SpeachBalloonsController < ApplicationController
2   before_filter :authenticate_author!, :only => [:index, :show]
3   before_filter :authenticate_admin!, :only => [:list, :browse, :create, :update, :destroy]
4
5   private
6   
7   def validate_param(ft)
8     res = nil
9     bl = ft[:size_count].to_i
10     tl = ft[:tail_count].to_i
11     if bl * tl == ft[:balloon_templates_attributes].size
12       flag = []
13       bl.times do |i|
14         flag[i] = []
15         tl.times do |j|
16           flag[i][j] = true
17         end
18       end
19       ft[:balloon_templates_attributes].each do |k, t|
20         flag[t[:size].to_i - 1][t[:tail].to_i - 1] = nil
21       end
22       if flag.flatten.compact.size == 0
23       else
24         res = "invalid variation"
25       end
26     else
27       res = "invalid template size"
28     end
29     res
30   end
31   
32   public
33   # GET /speach_balloons
34   # GET /speach_balloons.json
35   def index
36     @speach_balloons = SpeachBalloon.all
37
38     respond_to do |format|
39       format.html # index.html.erb
40       format.json { render json: @speach_balloons }
41     end
42   end
43
44   # GET /speach_balloons/1
45   # GET /speach_balloons/1.json
46   def show
47     @speach_balloon = SpeachBalloon.find(params[:id], include: {:balloon_templates => :speach_templates})
48
49     respond_to do |format|
50       format.html # show.html.erb
51       format.json {
52         render :json => @speach_balloon.to_json(include: {
53           :balloon_templates => {:include => :speach_templates}
54         })
55       }
56       format.jsonp {
57         render :json => "callback(" + @speach_balloon.to_json(include: {
58           :balloon_templates => {:include => :speach_templates}
59         }) + ")"
60       }
61     end
62   end
63
64   def list
65     @speach_balloons = SpeachBalloon.all
66
67     respond_to do |format|
68       format.html { render layout: 'system' }
69       format.json { render json: @speach_balloons }
70     end
71   end
72
73   def browse
74     @speach_balloon = SpeachBalloon.find(params[:id])
75
76     respond_to do |format|
77       format.html { render layout: 'system' }
78       format.json { render json: @speach_balloon }
79     end
80   end
81
82   # POST /speach_balloons
83   # POST /speach_balloons.json
84   def create
85     @speach_balloon = SpeachBalloon.new(params[:speach_balloon])
86     if em = validate_param(params[:speach_balloon])
87       respond_to do |format|
88         format.json { render json: em, status: :unprocessable_entity }
89       end
90     else
91       respond_to do |format|
92         if @speach_balloon.save
93           format.html { redirect_to @speach_balloon, notice: 'Speach balloon was successfully created.' }
94           format.json { render json: @speach_balloon, status: :created, location: @speach_balloon }
95         else
96           format.html { render action: "new" }
97           format.json { render json: @speach_balloon.errors, status: :unprocessable_entity }
98         end
99       end
100     end
101   end
102
103   # PUT /speach_balloons/1
104   # PUT /speach_balloons/1.json
105   def update
106     @speach_balloon = SpeachBalloon.find(params[:id])
107
108     respond_to do |format|
109       if @speach_balloon.update_attributes(params[:speach_balloon])
110         format.html { redirect_to @speach_balloon, notice: 'Speach balloon was successfully updated.' }
111         format.json { head :ok }
112       else
113         format.html { render action: "edit" }
114         format.json { render json: @speach_balloon.errors, status: :unprocessable_entity }
115       end
116     end
117   end
118
119   # DELETE /speach_balloons/1
120   # DELETE /speach_balloons/1.json
121   def destroy
122     @speach_balloon = SpeachBalloon.find(params[:id])
123     @speach_balloon.destroy
124
125     respond_to do |format|
126       format.html { redirect_to speach_balloons_url }
127       format.json { head :ok }
128     end
129   end
130 end