OSDN Git Service

t#:
[pettanr/pettanr.git] / app / controllers / ground_colors_controller.rb
1 class GroundColorsController < ApplicationController
2   layout 'test' if MagicNumber['test_layout']
3   if MagicNumber['run_mode'] == 0
4     before_filter :authenticate_user, :only => [:new, :edit, :create, :update, :destroy]
5     before_filter :authenticate_author, :only => [:new, :edit, :create, :update, :destroy]
6   else
7     before_filter :authenticate_reader, :only => [:index, :show]
8     before_filter :authenticate_user, :only => [:new, :edit, :create, :update, :destroy]
9     before_filter :authenticate_author, :only => [:new, :edit, :create, :update, :destroy]
10   end
11
12   def self.model
13     GroundColor
14   end
15   
16   def index
17     public_list
18   end
19   
20   def show
21     @item = GroundColor.show(params[:id], @operators)
22     respond_to do |format|
23       format.html {
24         @ground_color = @item
25       }
26       format_prof format
27       format.json { render json: @item.to_json(GroundColor.show_json_opt) }
28     end
29   end
30   
31   def new
32     raise Pettanr::NotWork unless @operators.author.working_panel
33     @panel = Panel.edit(@operators.author.working_panel, @operators)
34     @ground_color = GroundColor.new :panel_id => @panel.id, :code => params[:code].to_i
35     @ground_color.supply_default
36
37     respond_to do |format|
38       format.html
39       format.json { render :json => @ground_color.to_json(GroundColor.show_json_opt) }
40     end
41   end
42
43   def edit
44     @ground_color = GroundColor.show(params[:id], @operators)
45     @panel = Panel.edit(@ground_color.panel.id, @operators)
46     
47     respond_to do |format|
48       format.html
49     end
50   end
51
52   def create
53     raise Pettanr::NotWork unless @operators.author.working_panel
54     @panel = Panel.edit(@operators.author.working_panel, @operators)
55     
56     @ground_color = GroundColor.new 
57     @ground_color.attributes = params[:ground_color]
58     @ground_color.overwrite @panel.id
59     
60     respond_to do |format|
61       if @ground_color.valid?
62         if @ground_color.store @operators
63           flash[:notice] = I18n.t('flash.notice.created', :model => Panel.model_name.human)
64           format.html { redirect_to @panel }
65           format.json { render json: @panel.panel_elements_as_json, status: :created, location: @panel }
66         else
67           flash[:notice] = I18n.t('flash.notice.not_created', :model => GroundColor.model_name.human)
68           format.html { render action: "new" }
69           format.json { render json: @ground_color.errors, status: :unprocessable_entity }
70         end
71       else
72         flash[:notice] = I18n.t('flash.notice.not_created', :model => GroundColor.model_name.human)
73         format.html { render action: "new" }
74         format.json { render json: @ground_color.errors, status: :unprocessable_entity }
75       end
76     end
77   end
78
79   def update
80     @ground_color = GroundColor.show(params[:id], @operators)
81     @ground_color.attributes = params[:ground_color]
82     @panel = Panel.edit(@ground_color.panel.id, @operators)
83     @ground_color.overwrite @panel.id
84     
85     respond_to do |format|
86       if @ground_color.store @operators
87         flash[:notice] = I18n.t('flash.notice.updated', :model => GroundColor.model_name.human)
88         format.html { redirect_to @ground_color }
89         format.json { head :ok }
90       else
91         flash[:notice] = I18n.t('flash.notice.not_updated', :model => GroundColor.model_name.human)
92         format.html { render action: "edit" }
93         format.json { render json: @ground_color.errors, status: :unprocessable_entity }
94       end
95     end
96   end
97
98   def destroy
99     @ground_color = GroundColor.show(params[:id], @operators)
100     @panel = Panel.edit(@ground_color.panel.id, @operators)
101     
102     respond_to do |format|
103       if @ground_color.remove @operators
104         flash[:notice] = I18n.t('flash.notice.destroyed', :model => GroundColor.model_name.human)
105         format.html { redirect_to @panel }
106         format.json { head :ok }
107       else
108         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => GroundColor.model_name.human)
109         format.html { redirect_to @ground_color }
110         format.json { render json: @ground_color.errors, status: :unprocessable_entity }
111       end
112     end
113   end
114   
115 end