OSDN Git Service

e00e3b1a3d3642db7bd1b99fcf6ba3eccc14881d
[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, :by_panel, :by_author]
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 by_panel
21     filter_list
22   end
23   
24   def by_author
25     filter_list
26   end
27   
28   def show
29     @item = GroundColor.show(params[:id], @operators)
30     respond_to do |format|
31       format.html {
32         @ground_color = @item
33       }
34       format_prof format
35       format.json { render json: @item.to_json(GroundColor.show_json_opt) }
36     end
37   end
38   
39   def new
40     raise Pettanr::NotWork unless @operators.author.working_panel
41     @panel = Panel.edit(@operators.author.working_panel, @operators)
42     @ground_color = GroundColor.new :panel_id => @panel.id, :code => params[:code].to_i
43     @ground_color.supply_default
44
45     respond_to do |format|
46       format.html
47       format.json { render :json => @ground_color.to_json(GroundColor.show_json_opt) }
48     end
49   end
50
51   def edit
52     @ground_color = GroundColor.show(params[:id], @operators)
53     @panel = Panel.edit(@ground_color.panel.id, @operators)
54     
55     respond_to do |format|
56       format.html
57     end
58   end
59
60   def create
61     raise Pettanr::NotWork unless @operators.author.working_panel
62     @panel = Panel.edit(@operators.author.working_panel, @operators)
63     
64     @ground_color = GroundColor.new 
65     @ground_color.attributes = params[:ground_color]
66     @ground_color.overwrite @panel.id
67     
68     respond_to do |format|
69       if @ground_color.valid?
70         if @ground_color.store @operators
71           flash[:notice] = I18n.t('flash.notice.created', :model => Panel.model_name.human)
72           format.html { redirect_to @panel }
73           format.json { render json: @panel.panel_elements_as_json, status: :created, location: @panel }
74         else
75           flash[:notice] = I18n.t('flash.notice.not_created', :model => GroundColor.model_name.human)
76           format.html { render action: "new" }
77           format.json { render json: @ground_color.errors, status: :unprocessable_entity }
78         end
79       else
80         flash[:notice] = I18n.t('flash.notice.not_created', :model => GroundColor.model_name.human)
81         format.html { render action: "new" }
82         format.json { render json: @ground_color.errors, status: :unprocessable_entity }
83       end
84     end
85   end
86
87   def update
88     @ground_color = GroundColor.show(params[:id], @operators)
89     @ground_color.attributes = params[:ground_color]
90     @panel = Panel.edit(@ground_color.panel.id, @operators)
91     @ground_color.overwrite @panel.id
92     
93     respond_to do |format|
94       if @ground_color.store @operators
95         flash[:notice] = I18n.t('flash.notice.updated', :model => GroundColor.model_name.human)
96         format.html { redirect_to @ground_color }
97         format.json { head :ok }
98       else
99         flash[:notice] = I18n.t('flash.notice.not_updated', :model => GroundColor.model_name.human)
100         format.html { render action: "edit" }
101         format.json { render json: @ground_color.errors, status: :unprocessable_entity }
102       end
103     end
104   end
105
106   def destroy
107     @ground_color = GroundColor.show(params[:id], @operators)
108     @panel = Panel.edit(@ground_color.panel.id, @operators)
109     
110     respond_to do |format|
111       if @ground_color.remove @operators
112         flash[:notice] = I18n.t('flash.notice.destroyed', :model => GroundColor.model_name.human)
113         format.html { redirect_to @panel }
114         format.json { head :ok }
115       else
116         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => GroundColor.model_name.human)
117         format.html { redirect_to @ground_color }
118         format.json { render json: @ground_color.errors, status: :unprocessable_entity }
119       end
120     end
121   end
122   
123 end