OSDN Git Service

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