OSDN Git Service

t#29400:itr3?
[pettanr/pettanr.git] / app / controllers / system_pictures_controller.rb
index 884d33c..e43e672 100644 (file)
@@ -1,60 +1,33 @@
 class SystemPicturesController < ApplicationController
+  layout 'test' if Pettanr::TestLayout
   before_filter :authenticate_user!, :only => [:index, :show]
-  before_filter :authenticate_admin!, :only => [:list, :browse, :create, :update, :destroy]
+  before_filter :authenticate_admin!, :only => [:list, :browse, :new, :create]
 
-  private
-  
-  def set_image(prm)
-    img = nil
-    if (f = prm[:system_picture][:file]).respond_to?(:read)
-      if f.size > 1000000
-        @system_picture.width = 0
-        @system_picture.height = 0
-        @system_picture.ext = 'none'
-        @system_picture.filesize = 1.megabytes
-      else
-        img = Magick::Image.from_blob(f.read).shift
-        @system_picture.width = img.columns
-        @system_picture.height = img.rows
-        @system_picture.ext = img.format.downcase
-        @system_picture.filesize = f.size
-      end
-    else
-      dat = Base64.decode64(prm[:system_picture][:file].to_s.gsub(' ', '+')) #rubyのバグ?+でデコードされるべきキャラがスペースになる
-      img = Magick::Image.from_blob(dat).shift
-      @system_picture.width = img.columns
-      @system_picture.height = img.rows
-      @system_picture.ext = img.format.downcase
-      @system_picture.filesize = 1000
-    end
-    img
-  end
-  
-  public
-  
   # GET /system_pictures
   # GET /system_pictures.json
   def index
-    @system_pictures = SystemPicture.all
+    @page = SystemPicture.page params[:page]
+    @page_size = SystemPicture.page_size params[:page_size]
+    @system_pictures = SystemPicture.list(@page, @page_size)
 
     respond_to do |format|
       format.html # index.html.erb
-      format.json { render json: @system_pictures }
+      format.json { render json: @system_pictures.to_json(SystemPicture.list_json_opt) }
     end
   end
 
   # GET /system_pictures/1
   # GET /system_pictures/1.json
   def show
-    @system_picture = SystemPicture.find(params[:id])
-
+    @system_picture = SystemPicture.show(params[:id], @author)
+    
     respond_to do |format|
       opt = {:type => @system_picture.mime_type, :disposition=>"inline"}
       format.png { send_data(@system_picture.restore, opt ) }
       format.gif { send_data(@system_picture.restore, opt ) }
       format.jpeg { send_data(@system_picture.restore, opt ) }
       format.html # show.html.erb
-      format.json { render json: @system_picture}
+      format.json { render json: @system_picture.to_json(SystemPicture.show_json_opt)}
     end
   end
 
@@ -76,67 +49,27 @@ class SystemPicturesController < ApplicationController
     end
   end
 
-
-  # POST /system_pictures
-  # POST /system_pictures.json
-  def create
-    @system_picture = SystemPicture.new
-    img = set_image params
-
+  def new
     respond_to do |format|
-      SystemPicture.transaction do
-        if @system_picture.save
-          if @system_picture.store(img)
-            format.html { redirect_to @system_picture, notice: 'system picture was successfully created.' }
-            format.json { render json: @system_picture, status: :created, location: @system_picture }
-          else
-            format.html { redirect_to @system_picture, notice: 'Failed! system picture was NOT created.' }
-            format.json { render json: @system_picture.errors, status: :unprocessable_entity }
-          end
-        else
-          format.html { render action: "new" }
-          format.json { render json: @system_picture.errors, status: :unprocessable_entity }
-        end
-      end
+      format.html # new.html.erb
     end
   end
 
-  # PUT /system_pictures/1
-  # PUT /system_pictures/1.json
-  def update
-    @system_picture = SystemPicture.find(params[:id])
-    img = set_image params
+  # POST /system_pictures
+  # POST /system_pictures.json
+  def create
+    @imager = PettanImager.load set_image params[:system_picture][:file]
 
     respond_to do |format|
-      SystemPicture.transaction do
-        if @system_picture.save
-          if @system_picture.store(img)
-            format.html { redirect_to @system_picture, notice: 'System picture was successfully updated.' }
-            format.json { head :ok }
-          else
-            format.html { redirect_to @system_picture, notice: 'Failed! System picture was NOT created.' }
-            format.json { render json: @system_picture.errors, status: :unprocessable_entity }
-          end
-        else
-          format.html { render action: "edit" }
-          format.json { render json: @system_picture.errors, status: :unprocessable_entity }
-        end
+      @system_picture = SystemPicture.store @imager
+      if @system_picture
+        format.html { redirect_to @system_picture, notice: 'system picture was successfully created.' }
+        format.json { render json: @system_picture.to_json(SystemPicture.show_json_opt), status: :created, location: @system_picture }
+      else
+        format.html { render action: "new" }
+        format.json { render json: {}, status: :unprocessable_entity }
       end
     end
   end
 
-  # DELETE /system_pictures/1
-  # DELETE /system_pictures/1.json
-  def destroy
-    @system_picture = SystemPicture.find(params[:id])
-    SystemPicture.transaction do
-      @system_picture.destroy
-      
-    end
-    
-    respond_to do |format|
-      format.html { redirect_to system_pictures_url }
-      format.json { head :ok }
-    end
-  end
 end