OSDN Git Service

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