diff --git a/app/controllers/advertisements_controller.rb b/app/controllers/advertisements_controller.rb index 17272cacd..bae259d8f 100644 --- a/app/controllers/advertisements_controller.rb +++ b/app/controllers/advertisements_controller.rb @@ -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 diff --git a/app/controllers/artist_versions_controller.rb b/app/controllers/artist_versions_controller.rb index d57879ca8..631410701 100644 --- a/app/controllers/artist_versions_controller.rb +++ b/app/controllers/artist_versions_controller.rb @@ -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 diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb index 9bd261e12..c878fe5c6 100644 --- a/app/controllers/favorites_controller.rb +++ b/app/controllers/favorites_controller.rb @@ -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 diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 051f10e28..8b6f3c30b 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -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 diff --git a/app/logical/favorite.rb b/app/logical/favorite.rb index 2ce80c134..3249feeb2 100644 --- a/app/logical/favorite.rb +++ b/app/logical/favorite.rb @@ -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 diff --git a/app/logical/post_set.rb b/app/logical/post_set.rb deleted file mode 100644 index d33faaaf0..000000000 --- a/app/logical/post_set.rb +++ /dev/null @@ -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 diff --git a/app/logical/post_sets/base.rb b/app/logical/post_sets/base.rb new file mode 100644 index 000000000..626edcd61 --- /dev/null +++ b/app/logical/post_sets/base.rb @@ -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 diff --git a/app/logical/post_sets/favorite.rb b/app/logical/post_sets/favorite.rb new file mode 100644 index 000000000..29df710a7 --- /dev/null +++ b/app/logical/post_sets/favorite.rb @@ -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 diff --git a/app/logical/post_sets/post.rb b/app/logical/post_sets/post.rb new file mode 100644 index 000000000..ee7f91bc1 --- /dev/null +++ b/app/logical/post_sets/post.rb @@ -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 diff --git a/app/models/advertisement.rb b/app/models/advertisement.rb index d686b7474..ed6f0cb0a 100644 --- a/app/models/advertisement.rb +++ b/app/models/advertisement.rb @@ -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) diff --git a/app/presenters/post_set_presenter.rb b/app/presenters/post_set_presenter.rb index 2ab3f9968..706e6b689 100644 --- a/app/presenters/post_set_presenter.rb +++ b/app/presenters/post_set_presenter.rb @@ -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 = '
' if wiki_page.nil? diff --git a/app/views/advertisements/_form.html.erb b/app/views/advertisements/_form.html.erb index 58a69cfc2..9745e32db 100644 --- a/app/views/advertisements/_form.html.erb +++ b/app/views/advertisements/_form.html.erb @@ -1,28 +1,7 @@ -<% form_for @advertisement, :html => {:multipart => true} do |f| %> - - - - - - - - - - - - - - - - - - - - - - - - - -
<%= submit_tag "Submit" %>
<%= f.label :file %><%= f.file_field "file" %>
<%= f.label :referral_url %><%= f.text_field :referral_url %>
<%= f.label :ad_type %><%= f.select :ad_type, ["vertical", "horizontal"] %>
<%= f.label :status %><%= f.select :status, ["active", "inactive"] %>
+<%= 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 %> \ No newline at end of file diff --git a/app/views/advertisements/index.html.erb b/app/views/advertisements/index.html.erb index 12f12f269..17a5381db 100644 --- a/app/views/advertisements/index.html.erb +++ b/app/views/advertisements/index.html.erb @@ -1,28 +1,5 @@

Advertisements

-
- <% form_tag(advertisements_path, :method => :get) do %> - - - - - - - - - - - - - - - - - -
<%= submit_tag "Search" %>
<%= text_field_tag "start_date", @start_date %>
<%= text_field_tag "end_date", @end_date %>
- <% end %> -
- diff --git a/app/views/artist_versions/index.html.erb b/app/views/artist_versions/index.html.erb index 6302d7f2a..d9f4ab802 100644 --- a/app/views/artist_versions/index.html.erb +++ b/app/views/artist_versions/index.html.erb @@ -20,9 +20,9 @@ - + - + <% end %> diff --git a/app/views/favorites/index.html.erb b/app/views/favorites/index.html.erb index e69de29bb..5e8e7cfb5 100644 --- a/app/views/favorites/index.html.erb +++ b/app/views/favorites/index.html.erb @@ -0,0 +1,42 @@ +
+
+ + +
+

Posts

+ <%= @post_set.presenter.post_previews_html %> + +
+ +
+ <%= @post_set.presenter.pagination_html(self) %> +
+
+ + <% content_for(:page_title) do %> + / fav:<%= CurrentUser.name %> + <% end %> +
+
+ diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index 1d0e5f0db..4b93052e3 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -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 diff --git a/config/danbooru_local_config.rb b/config/danbooru_local_config.rb index be65aa190..f4b04a6fb 100644 --- a/config/danbooru_local_config.rb +++ b/config/danbooru_local_config.rb @@ -31,5 +31,9 @@ module Danbooru }.html_safe end + + def is_user_advertiser?(user) + user.is_admin? || user.name == "ppayne" + end end end diff --git a/test/functional/advertisement_hits_controller_test.rb b/test/functional/advertisement_hits_controller_test.rb new file mode 100644 index 000000000..045c472b9 --- /dev/null +++ b/test/functional/advertisement_hits_controller_test.rb @@ -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 diff --git a/test/functional/advertisements_controller_test.rb b/test/functional/advertisements_controller_test.rb index 344813aa6..7abdf9ae5 100644 --- a/test/functional/advertisements_controller_test.rb +++ b/test/functional/advertisements_controller_test.rb @@ -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 diff --git a/test/functional/artist_versions_controller_test.rb b/test/functional/artist_versions_controller_test.rb index 3e0822fe5..b22b229d5 100644 --- a/test/functional/artist_versions_controller_test.rb +++ b/test/functional/artist_versions_controller_test.rb @@ -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
<%= h artist_version.other_names %> <%= h artist_version.group_name %> <%= time_ago_in_words artist_version.created_at %> ago<%= link_to artist_version.updater_name, user_path(artist_version.user_id) %><%= link_to artist_version.updater_name, user_path(artist_version.updater_id) %> <%= artist_version.is_active? %><%= artist_version.urls.join(" ") %><%= artist_version.url_string %>