--- /dev/null
+class SpeachBalloonsController < ApplicationController
+ before_filter :authenticate_author!, :except => [:index]
+
+ private
+
+ def validate_param(ft)
+ res = nil
+ bl = ft[:border_limit].to_i
+ tl = ft[:tail_limit].to_i
+ if bl * tl == ft[:balloon_templates_attributes].size
+ flag = []
+ bl.times do |i|
+ flag[i] = []
+ tl.times do |j|
+ flag[i][j] = true
+ end
+ end
+ ft[:balloon_templates_attributes].each do |k, t|
+ flag[t[:border].to_i - 1][t[:tail].to_i - 1] = nil
+ end
+ if flag.flatten.compact.size == 0
+ else
+ res = "invalid variation"
+ end
+ else
+ res = "invalid template size"
+ end
+ res
+ end
+
+ public # GET /speach_balloons
+ # GET /speach_balloons.json
+ def index
+ @speach_balloons = SpeachBalloon.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @speach_balloons }
+ end
+ end
+
+ # GET /speach_balloons/1
+ # GET /speach_balloons/1.json
+ def show
+ @speach_balloon = SpeachBalloon.find(params[:id], include: {:balloon_templates => :speach_templates})
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json {
+ render :json => @speach_balloon.to_json(include: {
+ :balloon_templates => {:include => :speach_templates}
+ })
+ }
+ format.jsonp {
+ render :json => "callback(" + @speach_balloon.to_json(include: {
+ :balloon_templates => {:include => :speach_templates}
+ }) + ")"
+ }
+ end
+ end
+
+ # GET /speach_balloons/new
+ # GET /speach_balloons/new.json
+ def new
+ @speach_balloon = SpeachBalloon.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @speach_balloon }
+ end
+ end
+
+ # GET /speach_balloons/1/edit
+ def edit
+ @speach_balloon = SpeachBalloon.find(params[:id])
+ end
+
+ # POST /speach_balloons
+ # POST /speach_balloons.json
+ def create
+ @speach_balloon = SpeachBalloon.new(params[:speach_balloon])
+
+ if em = validate_param(params[:speach_balloon])
+ respond_to do |format|
+ format.json { render json: em, status: :unprocessable_entity }
+ end
+ else
+ respond_to do |format|
+ if @speach_balloon.save
+ format.html { redirect_to @speach_balloon, notice: 'Speach balloon was successfully created.' }
+ format.json { render json: @speach_balloon, status: :created, location: @speach_balloon }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @speach_balloon.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+ end
+
+ # PUT /speach_balloons/1
+ # PUT /speach_balloons/1.json
+ def update
+ @speach_balloon = SpeachBalloon.find(params[:id])
+
+ respond_to do |format|
+ if @speach_balloon.update_attributes(params[:speach_balloon])
+ format.html { redirect_to @speach_balloon, notice: 'Speach balloon was successfully updated.' }
+ format.json { head :ok }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @speach_balloon.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /speach_balloons/1
+ # DELETE /speach_balloons/1.json
+ def destroy
+ @speach_balloon = SpeachBalloon.find(params[:id])
+ @speach_balloon.destroy
+
+ respond_to do |format|
+ format.html { redirect_to speach_balloons_url }
+ format.json { head :ok }
+ end
+ end
+end