diff --git a/.gitignore b/.gitignore index b4dc936f2..67a758126 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ vendor/cache/*.gem .sass-cache .DS_Store coverage +*~ diff --git a/app/controllers/pools_controller.rb b/app/controllers/pools_controller.rb index ecda00d8f..7a83d091b 100644 --- a/app/controllers/pools_controller.rb +++ b/app/controllers/pools_controller.rb @@ -1,25 +1,49 @@ class PoolsController < ApplicationController + respond_to :html, :xml, :json + before_filter :member_only, :except => [:index, :show] + before_filter :moderator_only, :only => [:destroy] + def new + @pool = Pool.new + respond_with(@pool) end def edit + @pool = Pool.find(params[:id]) + respond_with(@pool) end def index + @search = Pool.search(params[:search]) + @pools = @search.paginate(:page => params[:page]) end def show + @pool = Pool.find(params[:id]) + respond_with(@pool) end def create + @pool = Pool.create(params[:pool]) + respond_with(@pool) end def update + @pool = Pool.find(params[:id]) + @pool.update_attributes(params[:pool]) + respond_with(@pool) end def destroy + @pool = Pool.find(params[:id]) + @pool.destroy + respond_with(@pool) end def revert + @pool = Pool.find(params[:id]) + @version = PoolVersion.find(params[:version_id]) + @pool.revert_to!(@version) + respond_with(@pool) end end diff --git a/app/controllers/pools_posts_controller.rb b/app/controllers/pools_posts_controller.rb new file mode 100644 index 000000000..ca0544384 --- /dev/null +++ b/app/controllers/pools_posts_controller.rb @@ -0,0 +1,18 @@ +class PoolsPostsController < ApplicationController + respond_to :html, :xml, :json + before_filter :member_only + + def create + @pool = Pool.find(params[:pool_id]) + @post = Post.find(params[:post_id]) + @pool.add_post!(@post) + respond_with(@pool) + end + + def destroy + @pool = Pool.find(params[:pool_id]) + @post = Post.find(params[:post_id]) + @pool.remove_post!(@post) + respond_with(@pool) + end +end diff --git a/app/models/pool.rb b/app/models/pool.rb index 74c97b4b3..48369d378 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -8,7 +8,7 @@ class Pool < ActiveRecord::Base has_many :versions, :class_name => "PoolVersion", :dependent => :destroy before_save :normalize_name after_save :create_version - attr_accessible :name, :description, :post_ids, :is_public, :is_active + attr_accessible :name, :description, :post_ids, :is_active def self.name_to_id(name) select_value_sql("SELECT id FROM pools WHERE name = ?", name.downcase) diff --git a/app/views/pools/edit.html.erb b/app/views/pools/edit.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/pools/index.html.erb b/app/views/pools/index.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/pools/new.html.erb b/app/views/pools/new.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/pools/show.html.erb b/app/views/pools/show.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/config/routes.rb b/config/routes.rb index 8aa3c52cd..1c1bd3e1a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,6 +33,7 @@ Danbooru::Application.routes.draw do end resources :note_versions resources :pools do + resources :posts, :controller => "pools_posts", :only => [:create, :destroy] member do put :revert end diff --git a/db/migrate/20100211025616_create_pools.rb b/db/migrate/20100211025616_create_pools.rb index 66203407c..c77ef6d9f 100644 --- a/db/migrate/20100211025616_create_pools.rb +++ b/db/migrate/20100211025616_create_pools.rb @@ -4,7 +4,6 @@ class CreatePools < ActiveRecord::Migration t.column :name, :string t.column :creator_id, :integer, :null => false t.column :description, :text - t.column :is_public, :boolean, :null => false, :default => true t.column :is_active, :boolean, :null => false, :default => true t.column :post_ids, :text, :null => false, :default => "" t.timestamps diff --git a/test/functional/notes_controller_test.rb b/test/functional/notes_controller_test.rb index 2b1e45ae3..6690481dd 100644 --- a/test/functional/notes_controller_test.rb +++ b/test/functional/notes_controller_test.rb @@ -67,7 +67,7 @@ class NotesControllerTest < ActionController::TestCase should "destroy a note" do assert_difference("Note.count", -1) do - post :destroy, {:id => @note.id} + post :destroy, {:id => @note.id}, {:user_id => @user.id} end end end diff --git a/test/functional/pools_controller_test.rb b/test/functional/pools_controller_test.rb index f1f0e2a0d..a9762f701 100644 --- a/test/functional/pools_controller_test.rb +++ b/test/functional/pools_controller_test.rb @@ -1,8 +1,90 @@ require 'test_helper' class PoolsControllerTest < ActionController::TestCase - # Replace this with your real tests. - test "the truth" do - assert true + context "The pools controller" do + setup do + @user = Factory.create(:user) + @mod = Factory.create(:moderator_user) + CurrentUser.user = @user + CurrentUser.ip_addr = "127.0.0.1" + @post = Factory.create(:post) + end + + teardown do + CurrentUser.user = nil + end + + context "index action" do + setup do + Factory.create(:pool, :name => "abc") + end + + should "list all pools" do + get :index + assert_response :success + end + + should "list all pools (with search)" do + get :index, {:search => {:name_matches => "abc"}} + assert_response :success + end + end + + context "show action" do + setup do + @pool = Factory.create(:pool) + end + + should "render" do + get :show, {:id => @pool.id} + assert_response :success + end + end + + context "create action" do + should "create a pool" do + assert_difference("Pool.count", 1) do + post :create, {:pool => {:name => "xxx", :description => "abc"}}, {:user_id => @user.id} + end + end + end + + context "update action" do + setup do + @pool = Factory.create(:pool) + end + + should "update a pool" do + post :update, {:id => @pool.id, :pool => {:name => "xyz"}}, {:user_id => @user.id} + @pool.reload + assert_equal("xyz", @pool.name) + end + end + + context "destroy action" do + setup do + @pool = Factory.create(:pool) + end + + should "destroy a pool" do + assert_difference("Pool.count", -1) do + post :destroy, {:id => @pool.id}, {:user_id => @mod.id} + end + end + end + + context "revert action" do + setup do + @pool = Factory.create(:pool, :name => "000") + @pool.update_attributes(:name => "111") + @pool.update_attributes(:name => "222") + end + + should "revert to a previous version" do + post :revert, {:id => @pool.id, :version_id => @pool.versions(true).first.id} + @pool.reload + assert_equal("000", @pool.name) + end + end end end