OSDN Git Service

speach balloon permission change to admin
[pettanr/pettanr.git] / app / controllers / speach_balloons_controller.rb
1 class SpeachBalloonsController < ApplicationController
2   before_filter :authenticate_author!, :only => [:index]
3   before_filter :authenticate_admin!, :except => [:index]
4
5   private
6   
7   def validate_param(ft)
8     res = nil
9     bl = ft[:border_limit].to_i
10     tl = ft[:tail_limit].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[:border].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  # GET /speach_balloons
33   # GET /speach_balloons.json
34   def index
35     @speach_balloons = SpeachBalloon.all
36
37     respond_to do |format|
38       format.html # index.html.erb
39       format.json { render json: @speach_balloons }
40     end
41   end
42
43   # GET /speach_balloons/1
44   # GET /speach_balloons/1.json
45   def show
46     @speach_balloon = SpeachBalloon.find(params[:id], include: {:balloon_templates => :speach_templates})
47
48     respond_to do |format|
49       format.html # show.html.erb
50       format.json {
51         render :json => @speach_balloon.to_json(include: {
52           :balloon_templates => {:include => :speach_templates}
53         })
54       }
55       format.jsonp {
56         render :json => "callback(" + @speach_balloon.to_json(include: {
57           :balloon_templates => {:include => :speach_templates}
58         }) + ")"
59       }
60     end
61   end
62
63   # GET /speach_balloons/new
64   # GET /speach_balloons/new.json
65   def new
66     @speach_balloon = SpeachBalloon.new
67
68     respond_to do |format|
69       format.html # new.html.erb
70       format.json { render json: @speach_balloon }
71     end
72   end
73
74   # GET /speach_balloons/1/edit
75   def edit
76     @speach_balloon = SpeachBalloon.find(params[:id])
77   end
78
79   # POST /speach_balloons
80   # POST /speach_balloons.json
81   def create
82     @speach_balloon = SpeachBalloon.new(params[:speach_balloon])
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