From 6791d4d1131b9ec99eca0cff4cc2881b5b0199c1 Mon Sep 17 00:00:00 2001 From: albert Date: Wed, 20 Jul 2011 17:23:38 -0400 Subject: [PATCH] fixed tag subscription tests --- .../tag_subscriptions_controller.rb | 7 ++-- app/logical/current_user.rb | 6 +++- app/models/tag_subscription.rb | 24 ++++++++----- app/views/tag_subscriptions/_form.html.erb | 6 ++++ .../_secondary_links.html.erb | 14 ++++++++ app/views/tag_subscriptions/edit.html.erb | 6 ++++ app/views/tag_subscriptions/index.html.erb | 35 ++++++++++++------- app/views/tag_subscriptions/new.html.erb | 6 ++++ db/development_structure.sql | 16 ++++----- ...20100307073438_create_tag_subscriptions.rb | 4 +-- script/testing/reset_db.sh | 6 ++++ test/factories/tag_subscription.rb | 1 + .../tag_subscriptions_controller_test.rb | 12 ++----- 13 files changed, 99 insertions(+), 44 deletions(-) create mode 100644 app/views/tag_subscriptions/_form.html.erb create mode 100644 app/views/tag_subscriptions/_secondary_links.html.erb create mode 100755 script/testing/reset_db.sh diff --git a/app/controllers/tag_subscriptions_controller.rb b/app/controllers/tag_subscriptions_controller.rb index 97d3e21c5..89e6cefec 100644 --- a/app/controllers/tag_subscriptions_controller.rb +++ b/app/controllers/tag_subscriptions_controller.rb @@ -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 diff --git a/app/logical/current_user.rb b/app/logical/current_user.rb index 5e0c9d2eb..34be49c7a 100644 --- a/app/logical/current_user.rb +++ b/app/logical/current_user.rb @@ -31,7 +31,11 @@ class CurrentUser end def self.id - user.id + if user.nil? + nil + else + user.id + end end def self.name diff --git a/app/models/tag_subscription.rb b/app/models/tag_subscription.rb index 2eb2fc4fa..fa5f693ea 100644 --- a/app/models/tag_subscription.rb +++ b/app/models/tag_subscription.rb @@ -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 diff --git a/app/views/tag_subscriptions/_form.html.erb b/app/views/tag_subscriptions/_form.html.erb new file mode 100644 index 000000000..550e0e62d --- /dev/null +++ b/app/views/tag_subscriptions/_form.html.erb @@ -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 %> diff --git a/app/views/tag_subscriptions/_secondary_links.html.erb b/app/views/tag_subscriptions/_secondary_links.html.erb new file mode 100644 index 000000000..b223d9048 --- /dev/null +++ b/app/views/tag_subscriptions/_secondary_links.html.erb @@ -0,0 +1,14 @@ +<% content_for(:secondary_links) do %> + +
  • <%= link_to "Listing", tag_subscriptions_path %>
  • +
  • <%= link_to "New", new_tag_subscription_path %>
  • +
  • <%= link_to "Help", wiki_pages_path(:title => "help:tag_subscriptions") %>
  • + + <% if @tag_subscription && !@tag_subscription.new_record? && @tag_subscription.editable_by?(CurrentUser.user) %> +
  • |
  • +
  • <%= link_to "Show", tag_subscription_path(@tag_subscription) %>
  • +
  • <%= link_to "Edit", edit_tag_subscription_path(@tag_subscription) %>
  • +
  • <%= link_to "Delete", tag_subscription_path(@tag_subscription, :method => :delete, :confirm => "Are you sure you want to delete this tag subscription?") %>
  • + <% end %> +
    +<% end %> diff --git a/app/views/tag_subscriptions/edit.html.erb b/app/views/tag_subscriptions/edit.html.erb index e69de29bb..4556e2b08 100644 --- a/app/views/tag_subscriptions/edit.html.erb +++ b/app/views/tag_subscriptions/edit.html.erb @@ -0,0 +1,6 @@ +
    +
    +

    Edit Tag Subscription

    + <%= render "form" %> +
    +
    \ No newline at end of file diff --git a/app/views/tag_subscriptions/index.html.erb b/app/views/tag_subscriptions/index.html.erb index 2d21bd9aa..f8f39acfa 100644 --- a/app/views/tag_subscriptions/index.html.erb +++ b/app/views/tag_subscriptions/index.html.erb @@ -1,14 +1,25 @@ -<% form_tag(:action => "update") do %> -

    Edit Tag Subscriptions

    -
    - You can only create up to <%= CONFIG["max_tag_subscriptions"] %> groups and each group can have up to 20 tags. +
    +
    +

    Tag Subscriptions

    + + + + + + + + + + <% @tag_subscriptions.each do |tag_subscription| %> + + + + + + <% end %> + +
    CreatorNameTag Query
    <%= tag_subscription.creator.name %><%= link_to tag_subscription.name, tag_subscription_path(tag_subscription.id) %><%= link_to tag_subscription.tag_query, posts_path(:tags => "sub:#{@user.name}:#{tag_subscription.name}") %>
    - - - <%= render :partial => "listing", :locals => {:tag_subscriptions => @tag_subscriptions} %> -
    -<% end %> +
    -<% content_for("subnavbar") do %> -
  • <%= link_to "Help", :controller => "help", :action => "tag_subscriptions" %>
  • -<% end %> +<%= render "secondary_links" %> diff --git a/app/views/tag_subscriptions/new.html.erb b/app/views/tag_subscriptions/new.html.erb index e69de29bb..6e5a16eae 100644 --- a/app/views/tag_subscriptions/new.html.erb +++ b/app/views/tag_subscriptions/new.html.erb @@ -0,0 +1,6 @@ +
    +
    +

    New Tag Subscription

    + <%= render "form" %> +
    +
    \ No newline at end of file diff --git a/db/development_structure.sql b/db/development_structure.sql index 713d10cf3..075f9dbbe 100644 --- a/db/development_structure.sql +++ b/db/development_structure.sql @@ -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: -- diff --git a/db/migrate/20100307073438_create_tag_subscriptions.rb b/db/migrate/20100307073438_create_tag_subscriptions.rb index e419b2c77..60c23abba 100644 --- a/db/migrate/20100307073438_create_tag_subscriptions.rb +++ b/db/migrate/20100307073438_create_tag_subscriptions.rb @@ -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 diff --git a/script/testing/reset_db.sh b/script/testing/reset_db.sh new file mode 100755 index 000000000..11b69ce9d --- /dev/null +++ b/script/testing/reset_db.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +rake db:drop db:create +createlang plpgsql danbooru2 +rake db:migrate + diff --git a/test/factories/tag_subscription.rb b/test/factories/tag_subscription.rb index 3c1dec0ca..a2802dde1 100644 --- a/test/factories/tag_subscription.rb +++ b/test/factories/tag_subscription.rb @@ -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 diff --git a/test/functional/tag_subscriptions_controller_test.rb b/test/functional/tag_subscriptions_controller_test.rb index 120806a03..1fe3a4dd6 100644 --- a/test/functional/tag_subscriptions_controller_test.rb +++ b/test/functional/tag_subscriptions_controller_test.rb @@ -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