From fc0a076aca1e2d8129a576ebbe20dedb98562d62 Mon Sep 17 00:00:00 2001 From: albert Date: Wed, 27 Oct 2010 20:16:43 -0400 Subject: [PATCH] * Missing files * Work on post exploration code by traversing dates --- app/controllers/comment_votes_controller.rb | 4 + app/controllers/comments_controller.rb | 2 - app/controllers/post_votes_controller.rb | 4 + app/helpers/application_helper.rb | 10 + app/logical/date_tag.rb | 72 +++++++ app/logical/post_set.rb | 4 + app/models/comment.rb | 21 +- app/models/comment_vote.rb | 7 +- app/models/post.rb | 10 +- app/models/post_vote.rb | 16 +- app/presenters/paginators/base.rb | 2 +- app/views/comments/index.html.haml | 4 +- .../comments/partials/index/_header.html.haml | 44 ++--- .../comments/partials/index/_list.html.haml | 2 + .../comments/partials/show/_comment.html.haml | 4 +- app/views/post_votes/create.js.rjs | 6 +- app/views/posts/index.html.erb | 27 +-- .../posts/partials/index/_explore.html.erb | 10 + .../posts/partials/index/_mode_menu.html.erb | 22 +++ app/views/posts/show.html.erb | 2 +- app/views/users/new.html.erb | 180 +++++++++--------- config/danbooru_default_config.rb | 2 +- db/development_structure.sql | 1 + .../20100215223541_create_post_votes.rb | 1 + public/javascripts/compiled/default.js | 13 ++ public/javascripts/src/app/users.js | 2 +- public/stylesheets/compiled/default.css | 29 ++- public/stylesheets/src/default.scss | 32 +++- script/custom/compile_javascripts | 1 + test/factories/comment.rb | 2 - test/unit/ip_ban_test.rb | 12 +- test/unit/post_test.rb | 12 +- 32 files changed, 371 insertions(+), 189 deletions(-) create mode 100644 app/logical/date_tag.rb create mode 100644 app/views/posts/partials/index/_explore.html.erb create mode 100644 app/views/posts/partials/index/_mode_menu.html.erb diff --git a/app/controllers/comment_votes_controller.rb b/app/controllers/comment_votes_controller.rb index 0863994fd..f3f89ec76 100644 --- a/app/controllers/comment_votes_controller.rb +++ b/app/controllers/comment_votes_controller.rb @@ -1,5 +1,9 @@ class CommentVotesController < ApplicationController def create + @comment = Comment.find(params[:comment_id]) + @comment.vote!(params[:score]) + rescue CommentVote::Error => x + @error = x end def destroy diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 76dbd0533..828199938 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -15,8 +15,6 @@ class CommentsController < ApplicationController def create @comment = Comment.new(params[:comment]) @comment.post_id = params[:comment][:post_id] - @comment.creator_id = CurrentUser.user.id - @comment.ip_addr = request.remote_ip @comment.score = 0 @comment.save respond_with(@comment) do |format| diff --git a/app/controllers/post_votes_controller.rb b/app/controllers/post_votes_controller.rb index d94fa457d..f64ac0eef 100644 --- a/app/controllers/post_votes_controller.rb +++ b/app/controllers/post_votes_controller.rb @@ -1,5 +1,9 @@ class PostVotesController < ApplicationController def create + @post = Post.find(params[:post_id]) + @post.vote!(params[:score]) + rescue PostVote::Error => x + @error = x end def destroy diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 6b53b9c83..a8fe26f8e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -23,6 +23,16 @@ module ApplicationHelper end end + def compact_time(time) + if time > Time.now.beginning_of_day + time.strftime("%H:%M") + elsif time > Time.now.beginning_of_year + time.strftime("%b %e") + else + time.strftime("%b %e, %Y") + end + end + protected def nav_link_match(controller, url) url =~ case controller diff --git a/app/logical/date_tag.rb b/app/logical/date_tag.rb new file mode 100644 index 000000000..a87ba00cf --- /dev/null +++ b/app/logical/date_tag.rb @@ -0,0 +1,72 @@ +class DateTag + attr_accessor :tag, :start_date, :end_date + + def self.new_from_range(start, stop) + new("#{start.to_formatted_s(:db)}..#{stop.to_formatted_s(:db)}") + end + + def initialize(tag) + @tag = tag + end + + def is_single_day? + tag =~ /^\d+-\d+-\d+$/ + end + + def is_range? + !is_single_day + end + + def start_date + return date if is_single_day? + extract_ranges + start_date + end + + def end_date + return date if is_single_day? + extract_ranges + end_date + end + + def previous_week + DateTag.new_from_range(1.week.ago(start_date), 1.week.ago(end_date)) + end + + def next_week + DateTag.new_from_range(1.week.since(start_date)), 1.week.since(end_date) + end + + def previous_month + DateTag.new_from_range(1.month.ago(start_date), 1.month.ago(end_date)) + end + + def next_month + DateTag.new_from_range(1.month.since(start_date)), 1.month.since(end_date) + end + + def date + Date.parse(tag) + end + + private + def extract_ranges + case tag + when /\A(.+?)\.\.(.+)/ + self.start_date = Date.parse($1) + self.end_date = Date.parse($2) + + when /\A<(.+)/, /\A<=(.+)/, /\A\.\.(.+)/ + self.start_date = 20.years.ago + self.end_date = Date.parse($1) + + when /\A>(.+)/, /\A>=(.+)/, /\A(.+)\.\.\Z/ + self.start_date = Date.parse($1) + self.end_date = Date.today + + else + self.start_date = Date.today + self.end_date = Date.today + end + end +end diff --git a/app/logical/post_set.rb b/app/logical/post_set.rb index 5a26ac15b..d33faaaf0 100644 --- a/app/logical/post_set.rb +++ b/app/logical/post_set.rb @@ -43,6 +43,10 @@ class PostSet 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) diff --git a/app/models/comment.rb b/app/models/comment.rb index ca9c2faf8..e6b853364 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -4,6 +4,7 @@ class Comment < ActiveRecord::Base belongs_to :post belongs_to :creator, :class_name => "User" has_many :votes, :class_name => "CommentVote", :dependent => :destroy + before_validation :initialize_creator, :on => :create after_save :update_last_commented_at attr_accessible :body attr_accessor :do_not_bump_post @@ -12,6 +13,11 @@ class Comment < ActiveRecord::Base scope :search_body, lambda {|query| where("body_index @@ plainto_tsquery(?)", query).order("comments.id DESC")} scope :hidden, lambda {|user| where("score < ?", user.comment_threshold)} + def initialize_creator + self.creator_id = CurrentUser.user.id + self.ip_addr = CurrentUser.ip_addr + end + def creator_name User.id_to_name(creator_id) end @@ -26,20 +32,21 @@ class Comment < ActiveRecord::Base end end - def vote!(is_positive) + def vote!(score) if !CurrentUser.user.can_comment_vote? raise CommentVote::Error.new("You can only vote ten times an hour on comments") - elsif !is_positive && creator.is_janitor_or_higher? + elsif score == "down" && creator.is_janitor? raise CommentVote::Error.new("You cannot downvote janitor/moderator/admin comments") elsif votes.find_by_user_id(CurrentUser.user.id).nil? - if is_positive - update_attribute(:score, score + 1) - else - update_attribute(:score, score - 1) + if score == "up" + increment!(:score) + elsif score == "down" + decrement!(:score) end - votes.create(:user_id => CurrentUser.user.id) + + votes.create else raise CommentVote::Error.new("You have already voted for this comment") diff --git a/app/models/comment_vote.rb b/app/models/comment_vote.rb index 6356f91d7..738779005 100644 --- a/app/models/comment_vote.rb +++ b/app/models/comment_vote.rb @@ -1,11 +1,16 @@ class CommentVote < ActiveRecord::Base class Error < Exception ; end - attr_accessor :is_positive belongs_to :comment belongs_to :user + before_validation :initialize_user, :on => :create + validates_presence_of :user_id, :comment_id def self.prune! destroy_all(["created_at < ?", 14.days.ago]) end + + def initialize_user + self.user_id = CurrentUser.user.id + end end diff --git a/app/models/post.rb b/app/models/post.rb index 494d4c8e1..fb662e038 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -644,15 +644,15 @@ class Post < ActiveRecord::Base !votes.exists?(["user_id = ?", user.id]) end - def vote!(user, is_positive) - if can_be_voted_by?(user) - if is_positive + def vote!(score) + if can_be_voted_by?(CurrentUser.user) + if score == "up" increment!(:score) - else + elsif score == "down" decrement!(:score) end - votes.create(:user_id => user.id) + votes.create(:score => score) else raise PostVote::Error.new("You have already voted for this comment") end diff --git a/app/models/post_vote.rb b/app/models/post_vote.rb index 8ed8d0632..db51f07ef 100644 --- a/app/models/post_vote.rb +++ b/app/models/post_vote.rb @@ -1,6 +1,20 @@ class PostVote < ActiveRecord::Base class Error < Exception ; end - attr_accessor :is_positive belongs_to :post + before_validation :initialize_user, :on => :create + validates_presence_of :post_id, :user_id, :score + validates_inclusion_of :score, :in => [1, -1] + + def score=(x) + if x == "up" + write_attribute(:score, 1) + elsif x == "down" + write_attribute(:score, -1) + end + end + + def initialize_user + self.user_id = CurrentUser.user.id + end end diff --git a/app/presenters/paginators/base.rb b/app/presenters/paginators/base.rb index 832ce9cd5..3751a5150 100644 --- a/app/presenters/paginators/base.rb +++ b/app/presenters/paginators/base.rb @@ -53,7 +53,7 @@ module Paginators elsif page == current_page html << page.to_s else - html << link(template, page) + html << paginated_link(template, page) end html << "" html.html_safe diff --git a/app/views/comments/index.html.haml b/app/views/comments/index.html.haml index 487e49d10..dc8dbc9f6 100644 --- a/app/views/comments/index.html.haml +++ b/app/views/comments/index.html.haml @@ -3,6 +3,6 @@ - @posts.each do |post| %div{:class => "post"} %div{:class => "preview"} - = image_tag(post.preview_file_url) - = render :partial => "comments/partials/index/list", :locals => {:post => post, :comments => post.comments.recent.reverse} + = link_to image_tag(post.preview_file_url), post_path(post) + = render :partial => "comments/partials/index/list", :locals => {:post => post, :comments => post.comments.recent.reverse, :show_header => true} %div{:class => "clearfix"} \ No newline at end of file diff --git a/app/views/comments/partials/index/_header.html.haml b/app/views/comments/partials/index/_header.html.haml index f0bda3074..e66ab8548 100644 --- a/app/views/comments/partials/index/_header.html.haml +++ b/app/views/comments/partials/index/_header.html.haml @@ -12,32 +12,18 @@ %span{:class => "info"} %strong Score %span{:id => "score-for-post-#{post.id}"} - = post.score - - -
-
- Date <%= compact_time(post.created_at) %> - User <%= fast_link_to h(post.author), :controller => "user", :action => "show", :id => post.user_id %> - Rating <%= post.pretty_rating %> - - Score <%= post.score %> - <% if @current_user.is_privileged_or_higher? %> - (vote <%= link_to_function "up", "Post.vote(1, #{post.id})" %>/<%= link_to_function "down", "Post.vote(-1, #{post.id})" %>) - <% end %> - -
-
- Tags - <% post.cached_tags.split(/ /).each do |name| %> - - <%= fast_link_to h(name.tr("_", " ")), :controller => "post", :action => "index", :tags => name %> - - <% end %> -
-
- <% if post.comments.count > 6 %> - <%= link_to_remote "#{pluralize post.comments.size - 6, 'comment'} hidden", :url => {:controller => "comment", :action => "index_hidden", :post_id => post.id} %>. - <% end %> - - <%= render :partial => "threshold_notice", :locals => {:post => post} %> \ No newline at end of file + %span= post.score + - if CurrentUser.user.is_privileged? + == (vote #{link_to("up", post_votes_path(:score => "up", :post_id => post.id), :remote => true, :method => :post)}/#{link_to("down", post_votes_path(:score => "down", :post_id => post.id), :remote => true, :method => :post)}) + %div{:class => "row list-of-tags"} + %strong Tags + - post.tag_array.each do |tag_name| + %span{:class => "tag-type-#{Tag.category_for(tag_name)}"} + = link_to(tag_name.tr("_", " "), posts_path(:tags => tag_name)) + %div{:class => "row notices"} + - if post.comments.count > 6 + %span{:class => "info", :id => "hidden-comments-notice-for-#{post.id}"} + == #{link_to "#{pluralize(post.comments.size - 6, 'comment')} hidden", comments_path(:post_id => post.id), :remote => true}. + - if post.comments.hidden(CurrentUser.user).count > 0 + %span{:class => "info", :id => "threshold-comments-notice-for-#{post.id}"} + == #{link_to "#{pluralize(post.comments.hidden(CurrentUser.user).count, 'comment')} below threshold", comments_path(:post_id => post.id, :include_hidden => true), :remote => true}. diff --git a/app/views/comments/partials/index/_list.html.haml b/app/views/comments/partials/index/_list.html.haml index bdba1b403..1a7c7cb84 100644 --- a/app/views/comments/partials/index/_list.html.haml +++ b/app/views/comments/partials/index/_list.html.haml @@ -1,4 +1,6 @@ %div{:class => "comments-for-post", "data-post-id" => post.id} + - if show_header + = render :partial => "comments/partials/index/header", :locals => {:post => post} %div{:class => "list-of-comments"} = render :partial => "comments/partials/show/comment", :collection => comments %div{:class => "clearfix"} diff --git a/app/views/comments/partials/show/_comment.html.haml b/app/views/comments/partials/show/_comment.html.haml index d49db71eb..62c5c3ef7 100644 --- a/app/views/comments/partials/show/_comment.html.haml +++ b/app/views/comments/partials/show/_comment.html.haml @@ -11,6 +11,6 @@ %span{:class => "link"} Reply - if CurrentUser.user.is_janitor? || CurrentUser.user.id == comment.creator_id %li= link_to "Delete", comment_path(comment.id), :confirm => "Do you really want to delete this comment?", :method => :delete - %li= link_to "Vote up", comment_vote_path(comment.id, :is_positive => true), :method => :post - %li= link_to "Vote down", comment_vote_path(comment.id, :is_positive => false), :method => :post + %li= link_to "Vote up", comment_votes_path(:comment_id => comment.id, :score => "up"), :method => :post, :remote => true + %li= link_to "Vote down", comment_votes_path(:comment_id => comment.id, :score => "down"), :method => :post, :remote => true %div{:class => "clearfix"} diff --git a/app/views/post_votes/create.js.rjs b/app/views/post_votes/create.js.rjs index d8a6f772d..e807bf877 100644 --- a/app/views/post_votes/create.js.rjs +++ b/app/views/post_votes/create.js.rjs @@ -1 +1,5 @@ -page["#score-for-post-#{@post.id} span"].val(@post.score) \ No newline at end of file +if @error + page.alert(@error.to_s) +else + page["#score-for-post-#{@post.id} span"].text(@post.score) +end \ No newline at end of file diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 685b63ea4..969d83622 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -9,7 +9,7 @@