* Refactored PostSet, splitting it into PostSets::Post and PostSets::Favorite
* Additional functional tests
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
class AdvertisementsController < ApplicationController
|
||||
before_filter :advertiser_only
|
||||
|
||||
def new
|
||||
@advertisement = Advertisement.new(
|
||||
:ad_type => "vertical",
|
||||
@@ -12,18 +14,8 @@ class AdvertisementsController < ApplicationController
|
||||
|
||||
def index
|
||||
@advertisements = Advertisement.all
|
||||
|
||||
if params[:start_date]
|
||||
@start_date = Date.parse(params[:start_date])
|
||||
else
|
||||
@start_date = 1.month.ago.to_date
|
||||
end
|
||||
|
||||
if params[:end_date]
|
||||
@end_date = Date.parse(params[:end_date])
|
||||
else
|
||||
@end_date = Date.today
|
||||
end
|
||||
@start_date = 1.month.ago.to_date
|
||||
@end_date = Date.today
|
||||
end
|
||||
|
||||
def show
|
||||
@@ -55,4 +47,12 @@ class AdvertisementsController < ApplicationController
|
||||
@advertisement.destroy
|
||||
redirect_to advertisements_path, :notice => "Advertisement destroyed"
|
||||
end
|
||||
|
||||
private
|
||||
def advertiser_only
|
||||
if !Danbooru.config.is_user_advertiser?(CurrentUser.user)
|
||||
redirect_to "/static/access_denied"
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,8 +2,8 @@ class ArtistVersionsController < ApplicationController
|
||||
respond_to :html, :xml, :json
|
||||
|
||||
def index
|
||||
@search = Artist.search(params[:search])
|
||||
@artist_versions = @search.paginate :order => "version desc", :per_page => 25, :page => params[:page]
|
||||
@search = ArtistVersion.search(params[:search])
|
||||
@artist_versions = @search.paginate :order => "id desc", :per_page => 25, :page => params[:page]
|
||||
respond_with(@artist_versions)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
class FavoritesController < ApplicationController
|
||||
def index
|
||||
@posts = CurrentUser.favorite_posts(params)
|
||||
if params[:tags]
|
||||
redirect_to(posts_path(:tags => "fav:#{CurrentUser.name} #{params[:tags]}"))
|
||||
else
|
||||
@posts = PostSets::Favorite.new(CurrentUser.user)
|
||||
end
|
||||
end
|
||||
|
||||
def create
|
||||
|
||||
@@ -4,7 +4,7 @@ class PostsController < ApplicationController
|
||||
respond_to :html, :xml, :json
|
||||
|
||||
def index
|
||||
@post_set = PostSet.new(params[:tags], params[:page], params[:before_id])
|
||||
@post_set = PostSets::Post.new(params[:tags], :page => params[:page], :before_id => params[:before_id])
|
||||
respond_with(@post_set)
|
||||
end
|
||||
|
||||
|
||||
@@ -35,26 +35,26 @@ class Favorite
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def self.destroy_for_post_and_user(post_id, user_id)
|
||||
execute_sql("DELETE FROM #{table_name_for(user_id)} WHERE post_id = #{post_id} AND user_id = #{user_id}")
|
||||
end
|
||||
|
||||
def self.destroy_for_post(post)
|
||||
0.upto(9) do |i|
|
||||
execute_sql("DELETE FROM favorites_#{i} WHERE post_id = #{post.id}")
|
||||
private
|
||||
def self.destroy_for_post_and_user(post_id, user_id)
|
||||
execute_sql("DELETE FROM #{table_name_for(user_id)} WHERE post_id = #{post_id} AND user_id = #{user_id}")
|
||||
end
|
||||
end
|
||||
|
||||
def self.destroy_for_user(user)
|
||||
execute_sql("DELETE FROM #{table_name_for(user)} WHERE user_id = #{user.id}")
|
||||
end
|
||||
def self.destroy_for_post(post)
|
||||
0.upto(9) do |i|
|
||||
execute_sql("DELETE FROM favorites_#{i} WHERE post_id = #{post.id}")
|
||||
end
|
||||
end
|
||||
|
||||
def self.select_value_sql(sql, *params)
|
||||
ActiveRecord::Base.select_value_sql(sql, *params)
|
||||
end
|
||||
def self.destroy_for_user(user)
|
||||
execute_sql("DELETE FROM #{table_name_for(user)} WHERE user_id = #{user.id}")
|
||||
end
|
||||
|
||||
def self.execute_sql(sql, *params)
|
||||
ActiveRecord::Base.execute_sql(sql, *params)
|
||||
end
|
||||
def self.select_value_sql(sql, *params)
|
||||
ActiveRecord::Base.select_value_sql(sql, *params)
|
||||
end
|
||||
|
||||
def self.execute_sql(sql, *params)
|
||||
ActiveRecord::Base.execute_sql(sql, *params)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
class PostSet
|
||||
class Error < Exception ; end
|
||||
|
||||
attr_accessor :tags, :page, :before_id, :errors, :count
|
||||
attr_accessor :wiki_page, :artist, :posts, :suggestions
|
||||
|
||||
def initialize(tags, page, before_id = nil)
|
||||
@tags = Tag.normalize(tags)
|
||||
@page = page.to_i
|
||||
@before_id = before_id
|
||||
@errors = []
|
||||
load_associations
|
||||
load_posts
|
||||
load_suggestions
|
||||
validate
|
||||
end
|
||||
|
||||
def use_sequential_paginator?
|
||||
!use_numbered_paginator?
|
||||
end
|
||||
|
||||
def use_numbered_paginator?
|
||||
before_id.nil?
|
||||
end
|
||||
|
||||
def has_errors?
|
||||
errors.any?
|
||||
end
|
||||
|
||||
def offset
|
||||
x = (page - 1) * limit
|
||||
if x < 0
|
||||
x = 0
|
||||
end
|
||||
x
|
||||
end
|
||||
|
||||
def limit
|
||||
Danbooru.config.posts_per_page
|
||||
end
|
||||
|
||||
def is_single_tag?
|
||||
tag_array.size == 1
|
||||
end
|
||||
|
||||
def date_tag
|
||||
tag_array.grep(/date:/).first
|
||||
end
|
||||
|
||||
def load_associations
|
||||
if is_single_tag?
|
||||
@wiki_page = WikiPage.find_by_title(tags)
|
||||
@artist = Artist.find_by_name(tags)
|
||||
end
|
||||
end
|
||||
|
||||
def load_posts
|
||||
@count = Post.fast_count(tags)
|
||||
@posts = Post.find_by_tags(tags, :before_id => before_id).all(:order => "posts.id desc", :limit => limit, :offset => offset)
|
||||
end
|
||||
|
||||
def load_suggestions
|
||||
if count < limit && is_single_tag?
|
||||
@suggestions = Tag.find_suggestions(tags)
|
||||
else
|
||||
@suggestions = []
|
||||
end
|
||||
end
|
||||
|
||||
def tag_array
|
||||
@tag_array ||= Tag.scan_query(tags)
|
||||
end
|
||||
|
||||
def validate
|
||||
validate_page
|
||||
validate_query_count
|
||||
rescue Error => x
|
||||
@errors << x.to_s
|
||||
end
|
||||
|
||||
def validate_page
|
||||
if page > 1_000
|
||||
raise Error.new("You cannot explicitly specify the page after page 1000")
|
||||
end
|
||||
end
|
||||
|
||||
def validate_query_count
|
||||
if !CurrentUser.user.is_privileged? && tag_array.size > 2
|
||||
raise Error.new("You can only search up to two tags at once with a basic account")
|
||||
end
|
||||
|
||||
if tag_array.size > 6
|
||||
raise Error.new("You can only search up to six tags at once")
|
||||
end
|
||||
end
|
||||
|
||||
def to_xml
|
||||
posts.to_xml
|
||||
end
|
||||
|
||||
def to_json
|
||||
posts.to_json
|
||||
end
|
||||
|
||||
def presenter
|
||||
@presnter ||= PostSetPresenter.new(self)
|
||||
end
|
||||
end
|
||||
39
app/logical/post_sets/base.rb
Normal file
39
app/logical/post_sets/base.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
module PostSets
|
||||
class Base
|
||||
attr_accessor :page, :before_id, :count
|
||||
|
||||
def initialize(options = {})
|
||||
@page = options[:page].to_i
|
||||
@before_id = options[:before_id]
|
||||
load_posts
|
||||
end
|
||||
|
||||
def has_wiki?
|
||||
false
|
||||
end
|
||||
|
||||
def use_sequential_paginator?
|
||||
!use_numbered_paginator?
|
||||
end
|
||||
|
||||
def use_numbered_paginator?
|
||||
before_id.nil?
|
||||
end
|
||||
|
||||
def load_posts
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def to_xml
|
||||
posts.to_xml
|
||||
end
|
||||
|
||||
def to_json
|
||||
posts.to_json
|
||||
end
|
||||
|
||||
def presenter
|
||||
@presnter ||= PostSetPresenter.new(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
18
app/logical/post_sets/favorite.rb
Normal file
18
app/logical/post_sets/favorite.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
module PostSets
|
||||
class FavoriteSet < Base
|
||||
attr_accessor :user
|
||||
|
||||
def initialize(options = {})
|
||||
super(options)
|
||||
@user = user
|
||||
end
|
||||
|
||||
def tags
|
||||
"fav:#{user.name}"
|
||||
end
|
||||
|
||||
def load_posts
|
||||
user.favorite_posts(:before_id => before_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
92
app/logical/post_sets/post.rb
Normal file
92
app/logical/post_sets/post.rb
Normal file
@@ -0,0 +1,92 @@
|
||||
module PostSets
|
||||
class Post < Base
|
||||
class Error < Exception ; end
|
||||
|
||||
attr_accessor :tags, :errors, :count
|
||||
attr_accessor :wiki_page, :artist, :suggestions
|
||||
|
||||
def initialize(tags, options = {})
|
||||
super(options)
|
||||
@tags = Tag.normalize(tags)
|
||||
@errors = []
|
||||
load_associations
|
||||
load_suggestions
|
||||
validate
|
||||
end
|
||||
|
||||
def has_wiki?
|
||||
is_single_tag?
|
||||
end
|
||||
|
||||
def has_errors?
|
||||
errors.any?
|
||||
end
|
||||
|
||||
def offset
|
||||
x = (page - 1) * limit
|
||||
if x < 0
|
||||
x = 0
|
||||
end
|
||||
x
|
||||
end
|
||||
|
||||
def limit
|
||||
Danbooru.config.posts_per_page
|
||||
end
|
||||
|
||||
def is_single_tag?
|
||||
tag_array.size == 1
|
||||
end
|
||||
|
||||
def date_tag
|
||||
tag_array.grep(/date:/).first
|
||||
end
|
||||
|
||||
def load_associations
|
||||
if is_single_tag?
|
||||
@wiki_page = WikiPage.find_by_title(tags)
|
||||
@artist = Artist.find_by_name(tags)
|
||||
end
|
||||
end
|
||||
|
||||
def load_posts
|
||||
@count = Post.fast_count(tags)
|
||||
@posts = Post.find_by_tags(tags, :before_id => before_id).all(:order => "posts.id desc", :limit => limit, :offset => offset)
|
||||
end
|
||||
|
||||
def load_suggestions
|
||||
if count < limit && is_single_tag?
|
||||
@suggestions = Tag.find_suggestions(tags)
|
||||
else
|
||||
@suggestions = []
|
||||
end
|
||||
end
|
||||
|
||||
def tag_array
|
||||
@tag_array ||= Tag.scan_query(tags)
|
||||
end
|
||||
|
||||
def validate
|
||||
validate_page
|
||||
validate_query_count
|
||||
rescue Error => x
|
||||
@errors << x.to_s
|
||||
end
|
||||
|
||||
def validate_page
|
||||
if page > 1_000
|
||||
raise Error.new("You cannot explicitly specify the page after page 1000")
|
||||
end
|
||||
end
|
||||
|
||||
def validate_query_count
|
||||
if !CurrentUser.is_privileged? && tag_array.size > 2
|
||||
raise Error.new("You can only search up to two tags at once with a basic account")
|
||||
end
|
||||
|
||||
if tag_array.size > 6
|
||||
raise Error.new("You can only search up to six tags at once")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -32,6 +32,10 @@ class Advertisement < ActiveRecord::Base
|
||||
"#{Rails.root}/public/images/advertisements/#{file_name}"
|
||||
end
|
||||
|
||||
def file
|
||||
nil
|
||||
end
|
||||
|
||||
def file=(f)
|
||||
if f.size > 0
|
||||
self.file_name = unique_identifier + File.extname(f.original_filename)
|
||||
|
||||
@@ -17,7 +17,7 @@ class PostSetPresenter < Presenter
|
||||
end
|
||||
|
||||
def wiki_html(template)
|
||||
if post_set.is_single_tag?
|
||||
if post_set.has_wiki?
|
||||
wiki_page = WikiPage.find_by_title(post_set.tags)
|
||||
html = '<section>'
|
||||
if wiki_page.nil?
|
||||
|
||||
@@ -1,28 +1,7 @@
|
||||
<% form_for @advertisement, :html => {:multipart => true} do |f| %>
|
||||
<table width="100%">
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td><%= submit_tag "Submit" %></td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
<tbody>
|
||||
<tr>
|
||||
<th width="15%"><%= f.label :file %></th>
|
||||
<td width="85%"><%= f.file_field "file" %></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><%= f.label :referral_url %></th>
|
||||
<td><%= f.text_field :referral_url %></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><%= f.label :ad_type %></th>
|
||||
<td><%= f.select :ad_type, ["vertical", "horizontal"] %></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><%= f.label :status %></th>
|
||||
<td><%= f.select :status, ["active", "inactive"] %></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<%= simple_form_for @advertisement, :html => {:multipart => true} do |f| %>
|
||||
<%= f.input :file, :as => :file %>
|
||||
<%= f.input :referral_url %>
|
||||
<%= f.input :ad_type, :collection => %w(vertical horizontal) %>
|
||||
<%= f.input :status, :collection => %w(active inactive) %>
|
||||
<%= f.button :submit %>
|
||||
<% end %>
|
||||
@@ -1,28 +1,5 @@
|
||||
<h4>Advertisements</h4>
|
||||
|
||||
<div style="margin-bottom: 1em;">
|
||||
<% form_tag(advertisements_path, :method => :get) do %>
|
||||
<table width="100%">
|
||||
<tfoot>
|
||||
<tr>
|
||||
<th></th>
|
||||
<td><%= submit_tag "Search" %></td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
<tbody>
|
||||
<tr>
|
||||
<th width="15%"><label>Start Date</label></th>
|
||||
<td width="85%"><%= text_field_tag "start_date", @start_date %></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th width="15%"><label>End Date</label></th>
|
||||
<td width="85%"><%= text_field_tag "end_date", @end_date %></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<table width="100%" class="highlightable">
|
||||
<thead>
|
||||
<tr>
|
||||
|
||||
@@ -20,9 +20,9 @@
|
||||
<td><%= h artist_version.other_names %></td>
|
||||
<td><%= h artist_version.group_name %></td>
|
||||
<td><%= time_ago_in_words artist_version.created_at %> ago</td>
|
||||
<td><%= link_to artist_version.updater_name, user_path(artist_version.user_id) %></td>
|
||||
<td><%= link_to artist_version.updater_name, user_path(artist_version.updater_id) %></td>
|
||||
<td><%= artist_version.is_active? %></td>
|
||||
<td><%= artist_version.urls.join(" ") %></td>
|
||||
<td><%= artist_version.url_string %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
<div class="favorites">
|
||||
<div class="index">
|
||||
<aside id="sidebar">
|
||||
<section id="search-box">
|
||||
<h1>Search Favorites</h1>
|
||||
<%= form_tag(favorites_path, :method => "get") do %>
|
||||
<%= text_field_tag("tags", params[:tags], :size => 20) %>
|
||||
<%= submit_tag "Go" %>
|
||||
<% end %>
|
||||
</section>
|
||||
|
||||
<% if CurrentUser.is_privileged? %>
|
||||
<section id="mode-box">
|
||||
<%= render :partial => "posts/partials/index/mode_menu" %>
|
||||
</section>
|
||||
<% end %>
|
||||
|
||||
<section id="blacklist-box">
|
||||
<h1>Blacklisted</h1>
|
||||
<%= link_to "Hidden", "#" %>
|
||||
<ul>
|
||||
</ul>
|
||||
</section>
|
||||
</aside>
|
||||
|
||||
<section id="content">
|
||||
<h1>Posts</h1>
|
||||
<%= @post_set.presenter.post_previews_html %>
|
||||
|
||||
<div class="clearfix"></div>
|
||||
|
||||
<div class="paginator">
|
||||
<%= @post_set.presenter.pagination_html(self) %>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<% content_for(:page_title) do %>
|
||||
/ fav:<%= CurrentUser.name %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -198,6 +198,10 @@ module Danbooru
|
||||
!user.is_privileged?
|
||||
end
|
||||
|
||||
def is_user_advertiser?(user)
|
||||
user.is_admin?
|
||||
end
|
||||
|
||||
def can_user_see_post?(user, post)
|
||||
if is_user_restricted?(user) && is_post_restricted?(post)
|
||||
false
|
||||
|
||||
@@ -31,5 +31,9 @@ module Danbooru
|
||||
</script>
|
||||
}.html_safe
|
||||
end
|
||||
|
||||
def is_user_advertiser?(user)
|
||||
user.is_admin? || user.name == "ppayne"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
17
test/functional/advertisement_hits_controller_test.rb
Normal file
17
test/functional/advertisement_hits_controller_test.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
require 'test_helper'
|
||||
|
||||
class AdvertisementHitsControllerTest < ActionController::TestCase
|
||||
context "An advertisement hits controller" do
|
||||
setup do
|
||||
@ad = Factory.create(:advertisement)
|
||||
@advertiser = Factory.create(:admin_user)
|
||||
end
|
||||
|
||||
should "create a new hit" do
|
||||
assert_difference("AdvertisementHit.count", 1) do
|
||||
post :create, {:id => @ad.id}
|
||||
end
|
||||
assert_redirected_to(@ad.referral_url)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,58 @@
|
||||
require 'test_helper'
|
||||
|
||||
class AdvertisementsControllerTest < ActionController::TestCase
|
||||
context "An advertisement controller" do
|
||||
setup do
|
||||
@ad = Factory.create(:advertisement)
|
||||
@advertiser = Factory.create(:admin_user)
|
||||
end
|
||||
|
||||
should "render the new page" do
|
||||
get :new, {}, {:user_id => @advertiser.id}
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
should "render the edit page" do
|
||||
get :edit, {:id => @ad.id}, {:user_id => @advertiser.id}
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
should "render the index page" do
|
||||
get :index, {}, {:user_id => @advertiser.id}
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
should "render the show page" do
|
||||
get :show, {:id => @ad.id}, {:user_id => @advertiser.id}
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
should "create an ad" do
|
||||
assert_difference("Advertisement.count", 1) do
|
||||
post :create, {:advertisement => Factory.attributes_for(:advertisement)}, {:user_id => @advertiser.id}
|
||||
end
|
||||
ad = Advertisement.last
|
||||
assert_redirected_to(advertisement_path(ad))
|
||||
end
|
||||
|
||||
should "update an ad" do
|
||||
post :update, {:id => @ad.id, :advertisement => {:width => 100}}, {:user_id => @advertiser.id}
|
||||
ad = Advertisement.last
|
||||
assert_equal(100, ad.width)
|
||||
assert_redirected_to(advertisement_path(ad))
|
||||
end
|
||||
|
||||
should "delete an ad" do
|
||||
assert_difference("Advertisement.count", -1) do
|
||||
post :destroy, {:id => @ad.id}, {:user_id => @advertiser.id}
|
||||
end
|
||||
assert_redirected_to(advertisements_path)
|
||||
end
|
||||
|
||||
should "block non-advertisers" do
|
||||
regular_user = Factory.create(:user)
|
||||
get :index, {}, {:user_id => regular_user.id}
|
||||
assert_redirected_to("/static/access_denied")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,8 +1,26 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ArtistVersionsControllerTest < ActionController::TestCase
|
||||
# Replace this with your real tests.
|
||||
test "the truth" do
|
||||
assert true
|
||||
context "An artist versions controller" do
|
||||
setup do
|
||||
CurrentUser.user = Factory.create(:user)
|
||||
CurrentUser.ip_addr = "127.0.0.1"
|
||||
@artist = Factory.create(:artist)
|
||||
end
|
||||
|
||||
teardown do
|
||||
CurrentUser.user = nil
|
||||
CurrentUser.ip_addr = nil
|
||||
end
|
||||
|
||||
should "render the index page" do
|
||||
get :index
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
should "render the index page when searching for something" do
|
||||
get :index, {:search => {:name_equals => @artist.name}}
|
||||
assert_response :success
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user