OSDN Git Service

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