OSDN Git Service

speech balloon template changed
[pettanr/pettanr.git] / app / controllers / speech_balloon_templates_controller.rb
1 class SpeechBalloonTemplatesController < 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 /speech_balloon_templates
34   # GET /speech_balloon_templates.json
35   def index
36     @speech_balloon_templates = SpeechBalloonTemplate.all
37
38     respond_to do |format|
39       format.html # index.html.erb
40       format.json { render json: @speech_balloon_templates }
41     end
42   end
43
44   # GET /speech_balloon_templates/1
45   # GET /speech_balloon_templates/1.json
46   def show
47     @speech_balloon_template = SpeechBalloonTemplate.find(params[:id])
48
49     respond_to do |format|
50       format.html # show.html.erb
51       format.json {
52         render :json => @speech_balloon_template.to_json()
53       }
54       format.jsonp {
55         render :json => "callback(" + @speech_balloon_template.to_json() + ")"
56       }
57     end
58   end
59
60   def list
61     @speech_balloon_templates = SpeechBalloonTemplate.all
62
63     respond_to do |format|
64       format.html { render layout: 'system' }
65       format.json { render json: @speech_balloon_templates }
66     end
67   end
68
69   def browse
70     @speech_balloon_template = SpeechBalloonTemplate.find(params[:id])
71
72     respond_to do |format|
73       format.html { render layout: 'system' }
74       format.json { render json: @speech_balloon_template }
75     end
76   end
77
78   # POST /speech_balloon_templates
79   # POST /speech_balloon_templates.json
80   def create
81     @speech_balloon_template = SpeechBalloonTemplate.new(params[:speech_balloon_template])
82     if em = validate_param(params[:speech_balloon_template])
83       respond_to do |format|
84         format.json { render json: em, status: :unprocessable_entity }
85       end
86     else
87       respond_to do |format|
88         if @speech_balloon_template.save
89           format.html { redirect_to @speech_balloon_template, notice: 'Speech balloon was successfully created.' }
90           format.json { render json: @speech_balloon_template, status: :created, location: @speech_balloon_template }
91         else
92           format.html { render action: "new" }
93           format.json { render json: @speech_balloon_template.errors, status: :unprocessable_entity }
94         end
95       end
96     end
97   end
98
99   # DELETE /speech_balloon_templates/1
100   # DELETE /speech_balloon_templates/1.json
101   def destroy
102     @speech_balloon_template = SpeechBalloonTemplate.find(params[:id])
103     @speech_balloon_template.destroy
104
105     respond_to do |format|
106       format.html { redirect_to :action => :list }
107       format.json { head :ok }
108     end
109   end
110 end