OSDN Git Service

add Picture
[pettanr/pettanr.git] / app / controllers / resource_pictures_controller.rb
index 4b17772..ce413b2 100644 (file)
@@ -1,26 +1,24 @@
 class ResourcePicturesController < ApplicationController
-  before_filter :authenticate_user!, :only => [:index, :show]
+  before_filter :authenticate_user!, :only => [:index, :show, :new, :create]
   before_filter :authenticate_admin!, :only => [:list, :browse]
 
   # GET /resource_pictures
   # GET /resource_pictures.json
   def index
-    @resource_pictures = ResourcePicture.find(:all, 
-      :include => {:original_picture => [:artist, :license]}, :order => 'updated_at', :limit => 20
-    )
+    @page = ResourcePicture.page params[:page]
+    @page_size = ResourcePicture.page_size params[:page_size]
+    @resource_pictures = ResourcePicture.list({}, @page, @page_size)
 
     respond_to do |format|
       format.html # index.html.erb
-      format.json { render :json => @resource_pictures.to_json(
-        :include => [:original_picture, :artist, :license]
-      ) }
+      format.json { render :json => @resource_pictures.to_json(ResourcePicture.list_json_opt) }
     end
   end
 
   # GET /resource_pictures/1
   # GET /resource_pictures/1.json
   def show
-    @resource_picture = ResourcePicture.find(params[:id])
+    @resource_picture = ResourcePicture.show(params[:id])
 
     respond_to do |format|
       opt = {:type => @resource_picture.mime_type, :disposition=>"inline"}
@@ -28,10 +26,45 @@ class ResourcePicturesController < ApplicationController
       format.gif { send_data(@resource_picture.restore(params[:subdir]), opt ) }
       format.jpeg { send_data(@resource_picture.restore(params[:subdir]), opt ) }
       format.html # show.html.erb
-      format.json { render json: @resource_picture}
+      format.json { render :json => @resource_picture.to_json(ResourcePicture.list_json_opt)}
     end
   end
+  
+  def new
+    @original_picture = OriginalPicture.show params[:original_picture_id], @artist
+    @original_picture_license_group = OriginalPictureLicenseGroup.new params[:original_picture_license_group]
+    @license_group = LicenseGroup.show @original_picture_license_group.license_group_id
+    @resource_picture = ResourcePicture.new params[:resource_picture]
+    
+    respond_to do |format|
+      format.html # new.html.erb
+      format.js
+    end
+  end
+  
+  def create
+    @original_picture = OriginalPicture.show params[:original_picture_id], @artist
+    @original_picture_license_group = OriginalPictureLicenseGroup.new params[:original_picture_license_group]
+    @license_group = LicenseGroup.show @original_picture_license_group.license_group_id
+    @resource_picture = ResourcePicture.new params[:resource_picture]
 
+    respond_to do |format|
+      if @resource_picture.store
+        format.html { render :template => "resource_pictures/new" }
+        format.js { render :template => "resource_pictures/new" }
+      else
+        format.html { render action: "new" }
+        format.js { render action: "new" }
+      end
+    end
+  end
+  def count
+    @resource_picture = {:count => ResourcePicture.visible_count}
+    respond_to do |format|
+      format.json { render json: @resource_picture.to_json }
+    end
+  end
+  
   def list
     @resource_pictures = ResourcePicture.all