OSDN Git Service

t#32326:fix work
[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     raise Pettanr::NotWork unless @author.working_panel
35     @picture = Picture.show params[:picture_id], @author
36     raise ActiveRecord::Forbidden unless @picture.enable?
37     @panel = Panel.edit(@author.working_panel, @author)
38     
39     @ground_picture = GroundPicture.new :panel_id => @panel.id, :picture_id => @picture.id
40     @ground_picture.supply_default
41
42     respond_to do |format|
43       format.html
44       format.json { render :json => @ground_picture.to_json(GroundPicture.show_json_opt) }
45     end
46   end
47
48   def edit
49     @ground_picture = GroundPicture.show(params[:id], @author)
50     @panel = Panel.edit(@ground_picture.panel.id, @author)
51     
52     respond_to do |format|
53       format.html
54     end
55   end
56
57   def create
58     raise Pettanr::NotWork unless @author.working_panel
59     @panel = Panel.edit(@author.working_panel, @author)
60     
61     @ground_picture = GroundPicture.new
62     @ground_picture.attributes = params[:ground_picture]
63     @ground_picture.overwrite @panel.id
64     
65     @picture = Picture.show @ground_picture.picture_id, @author
66     raise ActiveRecord::Forbidden unless @picture.enable?
67     
68     respond_to do |format|
69       if @ground_picture.valid?
70         if @ground_picture.store @author
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 => GroundPicture.model_name.human)
76           format.html { render action: "new" }
77           format.json { render json: @panel.errors, status: :unprocessable_entity }
78         end
79       else
80         flash[:notice] = I18n.t('flash.notice.not_created', :model => GroundPicture.model_name.human)
81         format.html { render action: "new" }
82         format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
83       end
84     end
85   end
86
87   def update
88     @ground_picture = GroundPicture.show(params[:id], @author)
89     @ground_picture.attributes = params[:ground_picture]
90     @panel = Panel.edit(@ground_picture.panel.id, @author)
91     @ground_picture.overwrite @panel.id
92     
93     @picture = Picture.show @ground_picture.picture_id, @author
94     raise ActiveRecord::Forbidden unless @picture.enable?
95     
96     respond_to do |format|
97       if @ground_picture.store @author
98         flash[:notice] = I18n.t('flash.notice.updated', :model => GroundPicture.model_name.human)
99         format.html { redirect_to @ground_picture }
100         format.json { head :ok }
101       else
102         flash[:notice] = I18n.t('flash.notice.not_updated', :model => GroundPicture.model_name.human)
103         format.html { render action: "edit" }
104         format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
105       end
106     end
107   end
108
109   def destroy
110     @ground_picture = GroundPicture.show(params[:id], @author)
111     @panel = Panel.edit(@ground_picture.panel.id, @author)
112     
113     respond_to do |format|
114       if @ground_picture.remove @author
115         flash[:notice] = I18n.t('flash.notice.destroyed', :model => GroundPicture.model_name.human)
116         format.html { redirect_to @panel }
117         format.json { head :ok }
118       else
119         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => GroundPicture.model_name.human)
120         format.html { redirect_to @ground_picture }
121         format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
122       end
123     end
124   end
125   
126 end