OSDN Git Service

cb05991762b73abc315526cc78adac20f979a81d
[pettanr/pettanr.git] / app / controllers / speach_balloon_templates_controller.rb
1 class SpeachBalloonTemplatesController < ApplicationController
2   before_filter :authenticate_user!, :only => [:index, :show]
3   before_filter :authenticate_admin!, :only => [:list, :browse, :import, :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_balloon_templates
34   # GET /speach_balloon_templates.json
35   def index
36     @speach_balloon_templates = SpeachBalloon.all
37
38     respond_to do |format|
39       format.html # index.html.erb
40       format.json { render json: @speach_balloon_templates }
41     end
42   end
43
44   # GET /speach_balloon_templates/1
45   # GET /speach_balloon_templates/1.json
46   def show
47     @speach_balloon_template = 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_template.to_json(include: {
53           :balloon_templates => {:include => :speach_templates}
54         })
55       }
56       format.jsonp {
57         render :json => "callback(" + @speach_balloon_template.to_json(include: {
58           :balloon_templates => {:include => :speach_templates}
59         }) + ")"
60       }
61     end
62   end
63
64   def list
65     @speach_balloon_templates = SpeachBalloon.all
66
67     respond_to do |format|
68       format.html { render layout: 'system' }
69       format.json { render json: @speach_balloon_templates }
70     end
71   end
72
73   def browse
74     @speach_balloon_template = SpeachBalloon.find(params[:id])
75
76     respond_to do |format|
77       format.html { render layout: 'system' }
78       format.json { render json: @speach_balloon_template }
79     end
80   end
81
82   # POST /speach_balloon_templates
83   # POST /speach_balloon_templates.json
84   def create
85     @speach_balloon_template = SpeachBalloon.new(params[:speach_balloon_template])
86     if em = validate_param(params[:speach_balloon_template])
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_template.save
93           format.html { redirect_to @speach_balloon_template, notice: 'Speach balloon was successfully created.' }
94           format.json { render json: @speach_balloon_template, status: :created, location: @speach_balloon_template }
95         else
96           format.html { render action: "new" }
97           format.json { render json: @speach_balloon_template.errors, status: :unprocessable_entity }
98         end
99       end
100     end
101   end
102
103   # DELETE /speach_balloon_templates/1
104   # DELETE /speach_balloon_templates/1.json
105   def destroy
106     @speach_balloon_template = SpeachBalloon.find(params[:id])
107     @speach_balloon_template.destroy
108
109     respond_to do |format|
110       format.html { redirect_to speach_balloon_templates_url }
111       format.json { head :ok }
112     end
113   end
114 end