OSDN Git Service

fix orm
[pettanr/pettanr.git] / app / controllers / ground_pictures_controller.rb
index 755c419..a4616e6 100644 (file)
 class GroundPicturesController < ApplicationController
-  layout 'test' if Pettanr::Application.test_layout
-  before_filter :authenticate_user!, :only => [:index]
+  if Manifest.manifest.magic_numbers['run_mode'] == 0
+    before_filter :authenticate_user, :only => [:new, :edit, :create, :update, :destroy]
+    before_filter :authenticate_author, :only => [:new, :edit, :create, :update, :destroy]
+  else
+    before_filter :authenticate_reader, :only => [
+      :index, :show, :by_panel, :by_author, :count, :count_by_panel, :count_by_author
+    ]
+    before_filter :authenticate_user, :only => [:new, :edit, :create, :update, :destroy]
+    before_filter :authenticate_author, :only => [:new, :edit, :create, :update, :destroy]
+  end
 
-  # GET /ground_pictures
-  # GET /ground_pictures.json
+  def self.model
+    GroundPicture
+  end
+  
   def index
-    @page = GroundPicture.page params[:page]
-    @page_size = GroundPicture.page_size params[:page_size]
-    @ground_pictures = GroundPicture.list(@page, @page_size)
+    filer_list
+  end
+  
+  def by_panel
+    filer_list
+  end
+  
+  def by_author
+    filer_list
+  end
+  
+  def show_html_format format
+    format.html {
+      @ground_picture = @item
+    }
+  end
+  
+  def show
+    set_show
+    respond_to do |format|
+      show_html_format format
+      show_prof_format format
+      show_json_format format
+    end
+  end
+  
+  def count
+    list_count
+  end
+  
+  def count_by_panel
+    list_count
+  end
+  
+  def count_by_author
+    list_count
+  end
+  
+  def new
+    raise Pettanr::NotWork unless @operators.author.working_panel
+    @picture = Picture.show params[:picture_id], @operators
+    raise ActiveRecord::Forbidden unless @picture.enable?
+    @panel = Panel.edit(@operators.author.working_panel, @operators)
+    
+    @ground_picture = GroundPicture.new :panel_id => @panel.id, :picture_id => @picture.id
+    @ground_picture.supply_default
 
     respond_to do |format|
-      format.html # index.html.erb
-      format.json { render :json => @ground_pictures.to_json(GroundPicture.list_json_opt) }
+      format.html
+      format.json { render :json => @ground_picture.to_json(GroundPicture.show_json_opt) }
     end
   end
 
+  def edit
+    @ground_picture = GroundPicture.show(params[:id], @operators)
+    @panel = Panel.edit(@ground_picture.panel.id, @operators)
+    
+    respond_to do |format|
+      format.html
+    end
+  end
+
+  def create
+    raise Pettanr::NotWork unless @operators.author.working_panel
+    @panel = Panel.edit(@operators.author.working_panel, @operators)
+    
+    @ground_picture = GroundPicture.new
+    @ground_picture.attributes = params[:ground_picture]
+    @ground_picture.overwrite @panel.id
+    
+    @picture = Picture.show @ground_picture.picture_id, @operators
+    raise ActiveRecord::Forbidden unless @picture.enable?
+    
+    respond_to do |format|
+      if @ground_picture.valid?
+        if @ground_picture.store @operators
+          flash[:notice] = I18n.t('flash.notice.created', :model => Panel.model_name.human)
+          format.html { redirect_to @panel }
+          format.json { render json: @panel.panel_elements_as_json, status: :created, location: @panel }
+        else
+          flash[:notice] = I18n.t('flash.notice.not_created', :model => GroundPicture.model_name.human)
+          format.html { render action: "new" }
+          format.json { render json: @panel.errors, status: :unprocessable_entity }
+        end
+      else
+        flash[:notice] = I18n.t('flash.notice.not_created', :model => GroundPicture.model_name.human)
+        format.html { render action: "new" }
+        format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  def update
+    @ground_picture = GroundPicture.show(params[:id], @operators)
+    @ground_picture.attributes = params[:ground_picture]
+    @panel = Panel.edit(@ground_picture.panel.id, @operators)
+    @ground_picture.overwrite @panel.id
+    
+    @picture = Picture.show @ground_picture.picture_id, @operators
+    raise ActiveRecord::Forbidden unless @picture.enable?
+    
+    respond_to do |format|
+      if @ground_picture.store @operators
+        flash[:notice] = I18n.t('flash.notice.updated', :model => GroundPicture.model_name.human)
+        format.html { redirect_to @ground_picture }
+        format.json { head :ok }
+      else
+        flash[:notice] = I18n.t('flash.notice.not_updated', :model => GroundPicture.model_name.human)
+        format.html { render action: "edit" }
+        format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  def destroy
+    @ground_picture = GroundPicture.show(params[:id], @operators)
+    @panel = Panel.edit(@ground_picture.panel.id, @operators)
+    
+    respond_to do |format|
+      if @ground_picture.remove @operators
+        flash[:notice] = I18n.t('flash.notice.destroyed', :model => GroundPicture.model_name.human)
+        format.html { redirect_to @panel }
+        format.json { head :ok }
+      else
+        flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => GroundPicture.model_name.human)
+        format.html { redirect_to @ground_picture }
+        format.json { render json: @ground_picture.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+  
 end