fixed tag subscription tests

This commit is contained in:
albert 2011-07-20 17:23:38 -04:00
parent c453e7db0f
commit 6791d4d113
13 changed files with 99 additions and 44 deletions

View File

@ -15,8 +15,9 @@ class TagSubscriptionsController < ApplicationController
end
def index
@search = TagSubscription.visible.search(params[:search])
@tag_subscriptions = @search.paginate(:page => params[:page])
@user = CurrentUser.user
@search = TagSubscription.visible_to(@user).search(params[:search])
@tag_subscriptions = @search.paginate(params[:page])
respond_with(@tag_subscriptions)
end
@ -41,6 +42,6 @@ class TagSubscriptionsController < ApplicationController
private
def check_privilege(tag_subscription)
raise User::PrivilegeError unless (tag_subscription.owner_id == CurrentUser.id || CurrentUser.is_moderator?)
raise User::PrivilegeError unless tag_subscription.editable_by?(CurrentUser.user)
end
end

View File

@ -31,7 +31,11 @@ class CurrentUser
end
def self.id
user.id
if user.nil?
nil
else
user.id
end
end
def self.name

View File

@ -1,18 +1,18 @@
class TagSubscription < ActiveRecord::Base
belongs_to :owner, :class_name => "User"
before_validation :initialize_owner, :on => :create
belongs_to :creator, :class_name => "User"
before_validation :initialize_creator, :on => :create
before_validation :initialize_post_ids, :on => :create
before_save :normalize_name
before_save :limit_tag_count
scope :visible, lambda {where("is_public = TRUE OR owner_id = ? OR ?", CurrentUser.id, CurrentUser.is_moderator?)}
attr_accessible :name, :tag_query, :post_ids, :is_visible_on_profile
validates_presence_of :name, :tag_query, :is_public, :creator_id
def normalize_name
self.name = name.gsub(/\W/, "_")
end
def initialize_owner
self.owner_id = CurrentUser.id
def initialize_creator
self.creator_id = CurrentUser.id
end
def initialize_post_ids
@ -33,6 +33,14 @@ class TagSubscription < ActiveRecord::Base
end
self.post_ids = post_ids.sort.reverse.slice(0, Danbooru.config.tag_subscription_post_limit).join(",")
end
def editable_by?(user)
user.is_moderator? || creator_id == user.id
end
def self.visible_to(user)
where("(is_public = TRUE OR creator_id = ? OR ?)", user.id, user.is_moderator?)
end
def self.find_tags(subscription_name)
if subscription_name =~ /^(.+?):(.+)$/
@ -46,7 +54,7 @@ class TagSubscription < ActiveRecord::Base
user = User.find_by_name(user_name)
if user
relation = where(["owner_id = ?", user.id])
relation = where(["creator_id = ?", user.id])
if sub_group
relation = relation.where(["name ILIKE ? ESCAPE E'\\\\'", sub_group.to_escaped_for_sql_like])
@ -59,7 +67,7 @@ class TagSubscription < ActiveRecord::Base
end
def self.find_post_ids(user_id, name = nil, limit = Danbooru.config.tag_subscription_post_limit)
relation = where(["owner_id = ?", user_id])
relation = where(["creator_id = ?", user_id])
if name
relation = relation.where(["name ILIKE ? ESCAPE E'\\\\'", name.to_escaped_for_sql_like])
@ -74,7 +82,7 @@ class TagSubscription < ActiveRecord::Base
def self.process_all
find_each do |tag_subscription|
if $job_task_daemon_active != false && tag_subscription.owner.is_privileged?
if $job_task_daemon_active != false && tag_subscription.creator.is_privileged?
begin
tag_subscription.process
tag_subscription.save

View File

@ -0,0 +1,6 @@
<%= simple_form_for(@tag_subscription) do |f| %>
<%= f.input :name %>
<%= f.input :tag_query %>
<%= f.input :is_public %>
<%= f.button :submit %>
<% end %>

View File

@ -0,0 +1,14 @@
<% content_for(:secondary_links) do %>
<menu>
<li><%= link_to "Listing", tag_subscriptions_path %></li>
<li><%= link_to "New", new_tag_subscription_path %></li>
<li><%= link_to "Help", wiki_pages_path(:title => "help:tag_subscriptions") %></li>
<% if @tag_subscription && !@tag_subscription.new_record? && @tag_subscription.editable_by?(CurrentUser.user) %>
<li>|</li>
<li><%= link_to "Show", tag_subscription_path(@tag_subscription) %></li>
<li><%= link_to "Edit", edit_tag_subscription_path(@tag_subscription) %></li>
<li><%= link_to "Delete", tag_subscription_path(@tag_subscription, :method => :delete, :confirm => "Are you sure you want to delete this tag subscription?") %></li>
<% end %>
</menu>
<% end %>

View File

@ -0,0 +1,6 @@
<div id="c-tag-subscriptions">
<div id="a-edit">
<h1>Edit Tag Subscription</h1>
<%= render "form" %>
</div>
</div>

View File

@ -1,14 +1,25 @@
<% form_tag(:action => "update") do %>
<h4>Edit Tag Subscriptions</h4>
<div style="margin-bottom: 1em;">
You can only create up to <%= CONFIG["max_tag_subscriptions"] %> groups and each group can have up to 20 tags.
<div id="c-tag-subscriptions">
<div id="a-index">
<h1>Tag Subscriptions</h1>
<table>
<thead>
<tr>
<th>Creator</th>
<th>Name</th>
<th>Tag Query</th>
</tr>
</thead>
<tbody>
<% @tag_subscriptions.each do |tag_subscription| %>
<tr>
<td><%= tag_subscription.creator.name %></td>
<td><%= link_to tag_subscription.name, tag_subscription_path(tag_subscription.id) %></td>
<td><%= link_to tag_subscription.tag_query, posts_path(:tags => "sub:#{@user.name}:#{tag_subscription.name}") %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<table width="100%" class="highlightable">
<%= render :partial => "listing", :locals => {:tag_subscriptions => @tag_subscriptions} %>
</table>
<% end %>
</div>
<% content_for("subnavbar") do %>
<li><%= link_to "Help", :controller => "help", :action => "tag_subscriptions" %></li>
<% end %>
<%= render "secondary_links" %>

View File

@ -0,0 +1,6 @@
<div id="c-tag-subscriptions">
<div id="a-new">
<h1>New Tag Subscription</h1>
<%= render "form" %>
</div>
</div>

View File

@ -2284,7 +2284,7 @@ ALTER SEQUENCE tag_implications_id_seq OWNED BY tag_implications.id;
CREATE TABLE tag_subscriptions (
id integer NOT NULL,
owner_id integer NOT NULL,
creator_id integer NOT NULL,
name character varying(255) NOT NULL,
tag_query character varying(255) NOT NULL,
post_ids text NOT NULL,
@ -4932,6 +4932,13 @@ CREATE INDEX index_tag_implications_on_antecedent_name ON tag_implications USING
CREATE INDEX index_tag_implications_on_consequent_name ON tag_implications USING btree (consequent_name);
--
-- Name: index_tag_subscriptions_on_creator_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_tag_subscriptions_on_creator_id ON tag_subscriptions USING btree (creator_id);
--
-- Name: index_tag_subscriptions_on_name; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -4939,13 +4946,6 @@ CREATE INDEX index_tag_implications_on_consequent_name ON tag_implications USING
CREATE INDEX index_tag_subscriptions_on_name ON tag_subscriptions USING btree (name);
--
-- Name: index_tag_subscriptions_on_owner_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_tag_subscriptions_on_owner_id ON tag_subscriptions USING btree (owner_id);
--
-- Name: index_tags_on_name; Type: INDEX; Schema: public; Owner: -; Tablespace:
--

View File

@ -1,7 +1,7 @@
class CreateTagSubscriptions < ActiveRecord::Migration
def self.up
create_table :tag_subscriptions do |t|
t.column :owner_id, :integer, :null => false
t.column :creator_id, :integer, :null => false
t.column :name, :string, :null => false
t.column :tag_query, :string, :null => false
t.column :post_ids, :text, :null => false
@ -9,7 +9,7 @@ class CreateTagSubscriptions < ActiveRecord::Migration
t.timestamps
end
add_index :tag_subscriptions, :owner_id
add_index :tag_subscriptions, :creator_id
add_index :tag_subscriptions, :name
end

6
script/testing/reset_db.sh Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env bash
rake db:drop db:create
createlang plpgsql danbooru2
rake db:migrate

View File

@ -1,4 +1,5 @@
Factory.define(:tag_subscription) do |f|
f.name {Faker::Lorem.words.join(" ")}
f.is_public true
f.tag_query "aaa"
end

View File

@ -15,26 +15,18 @@ class TagSubscriptionsControllerTest < ActionController::TestCase
context "index action" do
setup do
@tag_subscription = Factory.create(:tag_subscription, :name => "aaa", :owner => @user)
@tag_subscription = Factory.create(:tag_subscription, :name => "aaa")
end
should "list all visible tag subscriptions" do
get :index
assert_response :success
end
context "with search conditions" do
should "list all matching forum posts" do
get :index, {:search => {:name_equals => "aaa"}}
assert_response :success
assert_equal(1, assigns(:tag_subscriptions).size)
end
end
end
context "edit action" do
setup do
@tag_subscription = Factory.create(:tag_subscription, :owner => @user)
@tag_subscription = Factory.create(:tag_subscription)
end
should "render" do