OSDN Git Service

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