improved pool interface

This commit is contained in:
albert
2011-03-04 18:50:38 -05:00
parent e431452a11
commit a281bf03ed
11 changed files with 105 additions and 18 deletions

View File

@@ -16,6 +16,7 @@ class PoolsController < ApplicationController
def index def index
@search = Pool.search(params[:search]) @search = Pool.search(params[:search])
@pools = @search.paginate(:page => params[:page]) @pools = @search.paginate(:page => params[:page])
respond_with(@pools)
end end
def show def show

View File

@@ -1,18 +1,27 @@
class PoolsPostsController < ApplicationController class PoolsPostsController < ApplicationController
respond_to :html, :xml, :json respond_to :html, :xml, :json, :js
before_filter :member_only before_filter :member_only
def create def create
@pool = Pool.find(params[:pool_id]) @pool = Pool.find_by_name(params[:pool_name]) || Pool.find(params[:pool_id])
@post = Post.find(params[:id]) @post = Post.find(params[:post_id])
@pool.add_post!(@post) @pool.add_post!(@post)
append_pool_to_session(@pool)
respond_with(@pool, :location => pool_path(@pool)) respond_with(@pool, :location => pool_path(@pool))
end end
def destroy def destroy
@pool = Pool.find(params[:pool_id]) @pool = Pool.find(params[:pool_id])
@post = Post.find(params[:id]) @post = Post.find(params[:post_id])
@pool.remove_post!(@post) @pool.remove_post!(@post)
respond_with(@pool, :location => pool_path(@pool)) respond_with(@pool, :location => pool_path(@pool))
end end
private
def append_pool_to_session(pool)
recent_pool_ids = session[:recent_pool_ids].to_s.scan(/\d+/)
recent_pool_ids << pool.id.to_s
recent_pool_ids = recent_pool_ids.slice(1, 5) if recent_pool_ids.size > 5
session[:recent_pool_ids] = recent_pool_ids.uniq.join(",")
end
end end

View File

@@ -1,6 +1,6 @@
module PoolsHelper module PoolsHelper
def recent_pool_list def recent_updated_pools(&block)
pool_ids = session[:recent_pool_ids].to_s.split(/,/) pool_ids = session[:recent_pool_ids].to_s.scan(/\d+/)
pool_ids.map {|x| content_tag("option", x, :value => x)}.join("\n").html_safe Pool.where(["id IN (?)", pool_ids]).each(&block) if pool_ids.any?
end end
end end

View File

@@ -1,6 +1,5 @@
<%= simple_form_for(pool) do |f| %> <%= simple_form_for(pool) do |f| %>
<%= f.input :name %> <%= f.input :name %>
<%= f.input :description %> <%= f.input :description %>
<%= f.input :is_active %>
<%= f.button :submit %> <%= f.button :submit %>
<% end %> <% end %>

View File

@@ -1,4 +1,18 @@
<%= form_for(post_pools_path(@post)) do %> <div id="c-pools-posts">
<p>Select a pool:</p> <div id="a-new">
<%= recent_pool_list %> <%= form_tag(pool_post_path) do %>
<% end %> <%= hidden_field_tag "post_id", @post.id %>
<%= text_field_tag "pool_name", "", :size => 20 %>
<%= submit_tag "Select" %>
<% end %>
<div>
<h1>Recent Pools</h1>
<ul id="recent-pools">
<% recent_updated_pools.each do |pool| %>
<li><%= pool.name %></li>
<% end %>
</ul>
</div>
</div>
</div>

View File

@@ -35,14 +35,12 @@ Danbooru::Application.routes.draw do
end end
resources :note_versions, :only => [:index] resources :note_versions, :only => [:index]
resources :pools do resources :pools do
resources :posts, :controller => "pools_posts", :only => [:create, :destroy, :show]
member do member do
put :revert put :revert
end end
end end
resources :pool_versions, :only => [:index] resources :pool_versions, :only => [:index]
resources :posts do resources :posts do
resources :pools, :controller => "pools_posts", :only => [:create]
resources :votes, :controller => "post_votes", :only => [:create, :destroy] resources :votes, :controller => "post_votes", :only => [:create, :destroy]
member do member do
put :revert put :revert
@@ -70,6 +68,8 @@ Danbooru::Application.routes.draw do
end end
resources :wiki_page_versions, :only => [:index] resources :wiki_page_versions, :only => [:index]
match '/pool_post' => 'pools_posts#create', :via => :post, :as => 'pool_post'
match '/pool_post' => 'pools_posts#destroy', :via => :delete, :as => 'pool_post'
match '/post_moderation/moderate' => 'post_moderation#moderate' match '/post_moderation/moderate' => 'post_moderation#moderate'
match '/post_moderation/disapprove' => 'post_moderation#disapprove', :via => :put match '/post_moderation/disapprove' => 'post_moderation#disapprove', :via => :put
match '/post_moderation/approve' => 'post_moderation#approve', :via => :put match '/post_moderation/approve' => 'post_moderation#approve', :via => :put

View File

@@ -1423,10 +1423,26 @@ $(document).ready(function() {
Danbooru.Pool.initialize_add_to_pool_link = function() { Danbooru.Pool.initialize_add_to_pool_link = function() {
$("#add-to-pool-dialog").dialog({autoOpen: false}); $("#add-to-pool-dialog").dialog({autoOpen: false});
$("#c-pools-posts #a-new input[type=text]").autocomplete({
source: function(req, resp) {
$.getJSON(
"/pools.json?search[name_contains]=" + req.term,
function(data) {
resp(data.map(function(x) {return x.pool.name;}));
}
);
},
minLength: 4,
});
$("a#pool").click(function() { $("a#pool").click(function() {
$("#add-to-pool-dialog").dialog("open"); $("#add-to-pool-dialog").dialog("open");
return false; return false;
}); });
$("ul#recent-pools li").click(function() {
$("#pool_name").val($(this).html());
});
} }
Danbooru.Pool.initialize_simple_edit = function() { Danbooru.Pool.initialize_simple_edit = function() {

View File

@@ -9,10 +9,26 @@
Danbooru.Pool.initialize_add_to_pool_link = function() { Danbooru.Pool.initialize_add_to_pool_link = function() {
$("#add-to-pool-dialog").dialog({autoOpen: false}); $("#add-to-pool-dialog").dialog({autoOpen: false});
$("#c-pools-posts #a-new input[type=text]").autocomplete({
source: function(req, resp) {
$.getJSON(
"/pools.json?search[name_contains]=" + req.term,
function(data) {
resp(data.map(function(x) {return x.pool.name;}));
}
);
},
minLength: 4,
});
$("a#pool").click(function() { $("a#pool").click(function() {
$("#add-to-pool-dialog").dialog("open"); $("#add-to-pool-dialog").dialog("open");
return false; return false;
}); });
$("ul#recent-pools li").click(function() {
$("#pool_name").val($(this).html());
});
} }
Danbooru.Pool.initialize_simple_edit = function() { Danbooru.Pool.initialize_simple_edit = function() {

View File

@@ -217,6 +217,16 @@ form.simple_form div.input {
div.dtext p { div.dtext p {
margin-bottom: 1em; } margin-bottom: 1em; }
/*** Pools Posts ***/
div#c-pools-posts div#a-new form {
margin-bottom: 1em; }
div#c-pools-posts div#a-new h1 {
font-size: 1.2em;
font-weight: bold; }
div#c-pools-posts div#a-new li {
margin-left: 1em;
cursor: pointer; }
/*** Pools ***/ /*** Pools ***/
div#c-pools div#a-edit p { div#c-pools div#a-edit p {
margin-bottom: 1em; margin-bottom: 1em;

View File

@@ -309,6 +309,28 @@ div.dtext {
} }
/*** Pools Posts ***/
div#c-pools-posts {
div#a-new {
form {
margin-bottom: 1em;
}
h1 {
font-size: 1.2em;
font-weight: bold;
}
li {
margin-left: 1em;
cursor: pointer;
}
}
}
/*** Pools ***/ /*** Pools ***/
div#c-pools { div#c-pools {

View File

@@ -17,14 +17,14 @@ class PoolsPostsControllerTest < ActionController::TestCase
context "create action" do context "create action" do
should "add a post to a pool" do should "add a post to a pool" do
post :create, {:pool_id => @pool.id, :id => @post.id, :format => "json"}, {:user_id => @user.id} post :create, {:pool_id => @pool.id, :post_id => @post.id, :format => "json"}, {:user_id => @user.id}
@pool.reload @pool.reload
assert_equal([@post.id], @pool.post_id_array) assert_equal([@post.id], @pool.post_id_array)
end end
should "add a post to a pool once and only once" do should "add a post to a pool once and only once" do
@pool.add_post!(@post) @pool.add_post!(@post)
post :create, {:pool_id => @pool.id, :id => @post.id, :format => "json"}, {:user_id => @user.id} post :create, {:pool_id => @pool.id, :post_id => @post.id, :format => "json"}, {:user_id => @user.id}
@pool.reload @pool.reload
assert_equal([@post.id], @pool.post_id_array) assert_equal([@post.id], @pool.post_id_array)
end end
@@ -36,14 +36,14 @@ class PoolsPostsControllerTest < ActionController::TestCase
end end
should "remove a post from a pool" do should "remove a post from a pool" do
post :destroy, {:pool_id => @pool.id, :id => @post.id, :format => "json"}, {:user_id => @user.id} post :destroy, {:pool_id => @pool.id, :post_id => @post.id, :format => "json"}, {:user_id => @user.id}
@pool.reload @pool.reload
assert_equal([], @pool.post_id_array) assert_equal([], @pool.post_id_array)
end end
should "do nothing if the post is not a member of the pool" do should "do nothing if the post is not a member of the pool" do
@pool.remove_post!(@post) @pool.remove_post!(@post)
post :destroy, {:pool_id => @pool.id, :id => @post.id, :format => "json"}, {:user_id => @user.id} post :destroy, {:pool_id => @pool.id, :post_id => @post.id, :format => "json"}, {:user_id => @user.id}
@pool.reload @pool.reload
assert_equal([], @pool.post_id_array) assert_equal([], @pool.post_id_array)
end end