OSDN Git Service

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