OSDN Git Service

speach rename to speech
[pettanr/pettanr.git] / app / controllers / speech_balloon_templates_controller.rb
diff --git a/app/controllers/speech_balloon_templates_controller.rb b/app/controllers/speech_balloon_templates_controller.rb
new file mode 100644 (file)
index 0000000..d17ff32
--- /dev/null
@@ -0,0 +1,114 @@
+class SpeechBalloonTemplatesController < ApplicationController
+  before_filter :authenticate_user!, :only => [:index, :show]
+  before_filter :authenticate_admin!, :only => [:list, :browse, :import, :destroy]
+
+  private
+  
+  def validate_param(ft)
+    res = nil
+    bl = ft[:size_count].to_i
+    tl = ft[:tail_count].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[:size].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 /speech_balloon_templates
+  # GET /speech_balloon_templates.json
+  def index
+    @speech_balloon_templates = SpeechBalloon.all
+
+    respond_to do |format|
+      format.html # index.html.erb
+      format.json { render json: @speech_balloon_templates }
+    end
+  end
+
+  # GET /speech_balloon_templates/1
+  # GET /speech_balloon_templates/1.json
+  def show
+    @speech_balloon_template = SpeechBalloon.find(params[:id], include: {:balloon_templates => :speech_templates})
+
+    respond_to do |format|
+      format.html # show.html.erb
+      format.json {
+        render :json => @speech_balloon_template.to_json(include: {
+          :balloon_templates => {:include => :speech_templates}
+        })
+      }
+      format.jsonp {
+        render :json => "callback(" + @speech_balloon_template.to_json(include: {
+          :balloon_templates => {:include => :speech_templates}
+        }) + ")"
+      }
+    end
+  end
+
+  def list
+    @speech_balloon_templates = SpeechBalloon.all
+
+    respond_to do |format|
+      format.html { render layout: 'system' }
+      format.json { render json: @speech_balloon_templates }
+    end
+  end
+
+  def browse
+    @speech_balloon_template = SpeechBalloon.find(params[:id])
+
+    respond_to do |format|
+      format.html { render layout: 'system' }
+      format.json { render json: @speech_balloon_template }
+    end
+  end
+
+  # POST /speech_balloon_templates
+  # POST /speech_balloon_templates.json
+  def create
+    @speech_balloon_template = SpeechBalloon.new(params[:speech_balloon_template])
+    if em = validate_param(params[:speech_balloon_template])
+      respond_to do |format|
+        format.json { render json: em, status: :unprocessable_entity }
+      end
+    else
+      respond_to do |format|
+        if @speech_balloon_template.save
+          format.html { redirect_to @speech_balloon_template, notice: 'Speech balloon was successfully created.' }
+          format.json { render json: @speech_balloon_template, status: :created, location: @speech_balloon_template }
+        else
+          format.html { render action: "new" }
+          format.json { render json: @speech_balloon_template.errors, status: :unprocessable_entity }
+        end
+      end
+    end
+  end
+
+  # DELETE /speech_balloon_templates/1
+  # DELETE /speech_balloon_templates/1.json
+  def destroy
+    @speech_balloon_template = SpeechBalloon.find(params[:id])
+    @speech_balloon_template.destroy
+
+    respond_to do |format|
+      format.html { redirect_to speech_balloon_templates_url }
+      format.json { head :ok }
+    end
+  end
+end