refactoring
This commit is contained in:
@@ -3,7 +3,7 @@ class FavoritesController < ApplicationController
|
||||
if params[:tags]
|
||||
redirect_to(posts_path(:tags => "fav:#{CurrentUser.name} #{params[:tags]}"))
|
||||
else
|
||||
@post_set = PostSets::Favorite.new(CurrentUser.user)
|
||||
@favorite_set = PostSets::Favorite.new(CurrentUser.user, params[:page])
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -44,6 +44,9 @@ protected
|
||||
when "forum_posts"
|
||||
/^\/forum_topics/
|
||||
|
||||
when "uploads"
|
||||
/^\/post/
|
||||
|
||||
else
|
||||
/^\/#{controller}/
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module PostSets
|
||||
class Base
|
||||
def has_wiki?
|
||||
is_single_tag?
|
||||
false
|
||||
end
|
||||
|
||||
def wiki_page
|
||||
@@ -9,20 +9,20 @@ module PostSets
|
||||
end
|
||||
|
||||
def has_artist?
|
||||
is_single_tag?
|
||||
false
|
||||
end
|
||||
|
||||
def artist
|
||||
end
|
||||
|
||||
def presenter
|
||||
@presenter ||= PostSetPresenter.new(self)
|
||||
end
|
||||
|
||||
def is_single_tag?
|
||||
false
|
||||
end
|
||||
|
||||
def presenter
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def arbitrary_sql_order_clause(ids, table_name)
|
||||
if ids.empty?
|
||||
return "#{table_name}.id desc"
|
||||
|
||||
@@ -1,20 +1,10 @@
|
||||
module PostSets
|
||||
class Favorite < Base
|
||||
attr_reader :user, :page, :favorites, :posts
|
||||
attr_reader :user, :page, :favorites
|
||||
|
||||
def initialize(user_id, page)
|
||||
@user = ::User.find(user_id)
|
||||
@page = [page.to_i, 1].max
|
||||
@favorites = ::Favorite.model_for(user.id).for_user(user.id).page(page)
|
||||
@posts = ::Post.where("id in (?)", post_ids).order(arbitrary_sql_order_clause(post_ids, "posts")).page("b0")
|
||||
end
|
||||
|
||||
def post_ids
|
||||
@post_ids ||= favorites.map(&:post_id)
|
||||
end
|
||||
|
||||
def offset
|
||||
(page - 1) * records_per_page
|
||||
@favorites = ::Favorite.model_for(user.id).for_user(user.id).paginate(page)
|
||||
end
|
||||
|
||||
def tag_array
|
||||
@@ -24,5 +14,13 @@ module PostSets
|
||||
def tag_string
|
||||
tag_array.join(" ")
|
||||
end
|
||||
|
||||
def posts
|
||||
favorites.map(&:post)
|
||||
end
|
||||
|
||||
def presenter
|
||||
@presenter ||= ::PostSetPresenters::Favorite.new(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -13,11 +13,7 @@ module PostSets
|
||||
end
|
||||
|
||||
def has_wiki?
|
||||
if tag_array.any?
|
||||
::WikiPage.titled(tag_string).exists?
|
||||
else
|
||||
false
|
||||
end
|
||||
tag_array.any? && ::WikiPage.titled(tag_string).exists?
|
||||
end
|
||||
|
||||
def wiki_page
|
||||
@@ -28,8 +24,20 @@ module PostSets
|
||||
end
|
||||
end
|
||||
|
||||
def has_artist?
|
||||
tag_array.any? && ::Artist.name_equals(tag_string).exists?
|
||||
end
|
||||
|
||||
def artist
|
||||
::Artist.name_equals(tag_string).first
|
||||
end
|
||||
|
||||
def is_single_tag?
|
||||
tag_array.size == 1
|
||||
end
|
||||
|
||||
def presenter
|
||||
@presenter ||= ::PostSetPresenters::Post.new(self)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,6 +4,15 @@ class User < ActiveRecord::Base
|
||||
class Error < Exception ; end
|
||||
class PrivilegeError < Exception ; end
|
||||
|
||||
module Levels
|
||||
MEMBER = 0
|
||||
PRIVILEGED = 100
|
||||
CONTRIBUTOR = 200
|
||||
JANITOR = 300
|
||||
MODERATOR = 400
|
||||
ADMIN = 500
|
||||
end
|
||||
|
||||
attr_accessor :password, :old_password
|
||||
attr_accessible :password, :old_password, :password_confirmation, :password_hash, :email, :last_logged_in_at, :last_forum_read_at, :has_mail, :receive_email_notifications, :comment_threshold, :always_resize_images, :favorite_tags, :blacklisted_tags, :name, :ip_addr
|
||||
validates_length_of :name, :within => 2..20, :on => :create
|
||||
@@ -18,7 +27,6 @@ class User < ActiveRecord::Base
|
||||
before_save :encrypt_password
|
||||
after_save :update_cache
|
||||
before_create :promote_to_admin_if_first_user
|
||||
before_create :normalize_level
|
||||
has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy
|
||||
has_one :ban
|
||||
has_many :subscriptions, :class_name => "TagSubscription"
|
||||
@@ -140,33 +148,32 @@ class User < ActiveRecord::Base
|
||||
return if Rails.env.test?
|
||||
|
||||
if User.count == 0
|
||||
self.is_admin = true
|
||||
self.level = Levels::ADMIN
|
||||
end
|
||||
end
|
||||
|
||||
def level_string
|
||||
if is_admin?
|
||||
"Admin"
|
||||
elsif is_moderator?
|
||||
case level
|
||||
when Levels::MEMBER
|
||||
"Member"
|
||||
|
||||
when Levels::PRIVILEGED
|
||||
"Privileged"
|
||||
|
||||
when Levels::CONTRIBUTOR
|
||||
"Contributor"
|
||||
|
||||
when Levels::JANITOR
|
||||
"Janitor"
|
||||
|
||||
when Levels::MODERATOR
|
||||
"Moderator"
|
||||
end
|
||||
|
||||
def normalize_level
|
||||
if is_admin?
|
||||
self.is_moderator = true
|
||||
self.is_janitor = true
|
||||
self.is_contributor = true
|
||||
self.is_privileged = true
|
||||
elsif is_moderator?
|
||||
self.is_janitor = true
|
||||
self.is_privileged = true
|
||||
elsif is_janitor?
|
||||
self.is_privileged = true
|
||||
elsif is_contributor?
|
||||
self.is_privileged = true
|
||||
|
||||
when Levels::ADMIN
|
||||
"Admin"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def is_anonymous?
|
||||
false
|
||||
end
|
||||
@@ -174,6 +181,30 @@ class User < ActiveRecord::Base
|
||||
def is_member?
|
||||
true
|
||||
end
|
||||
|
||||
def is_privileged?
|
||||
level >= Levels::PRIVILEGED
|
||||
end
|
||||
|
||||
def is_contributor?
|
||||
level >= Levels::CONTRIBUTOR
|
||||
end
|
||||
|
||||
def is_janitor?
|
||||
level >= Levels::JANITOR
|
||||
end
|
||||
|
||||
def is_moderator?
|
||||
level >= Levels::MODERATOR
|
||||
end
|
||||
|
||||
def is_mod?
|
||||
level >= Levels::MODERATOR
|
||||
end
|
||||
|
||||
def is_admin?
|
||||
level >= Levels::ADMIN
|
||||
end
|
||||
end
|
||||
|
||||
module EmailVerificationMethods
|
||||
|
||||
33
app/presenters/post_set_presenters/favorite.rb
Normal file
33
app/presenters/post_set_presenters/favorite.rb
Normal file
@@ -0,0 +1,33 @@
|
||||
module PostSetPresenters
|
||||
class Favorite
|
||||
attr_accessor :favorite_set, :tag_set_presenter
|
||||
delegate :favorites, :posts, :to => :favorite_set
|
||||
|
||||
def initialize(favorite_set)
|
||||
@favorite_set = favorite_set
|
||||
@tag_set_presenter = TagSetPresenter.new(
|
||||
RelatedTagCalculator.calculate_from_sample_to_array(
|
||||
favorite_set.tag_string
|
||||
).map {|x| x[0]}
|
||||
)
|
||||
end
|
||||
|
||||
def tag_list_html(template)
|
||||
tag_set_presenter.tag_list_html(template)
|
||||
end
|
||||
|
||||
def post_previews_html(template)
|
||||
html = ""
|
||||
|
||||
if favorites.empty?
|
||||
return template.render(:partial => "post_sets/blank")
|
||||
end
|
||||
|
||||
favorites.each do |favorite|
|
||||
html << PostPresenter.preview(favorite.post)
|
||||
end
|
||||
|
||||
html.html_safe
|
||||
end
|
||||
end
|
||||
end
|
||||
36
app/presenters/post_set_presenters/post.rb
Normal file
36
app/presenters/post_set_presenters/post.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
module PostSetPresenters
|
||||
class Post
|
||||
attr_accessor :post_set, :tag_set_presenter
|
||||
|
||||
def initialize(post_set)
|
||||
@post_set = post_set
|
||||
@tag_set_presenter = TagSetPresenter.new(
|
||||
RelatedTagCalculator.calculate_from_sample_to_array(
|
||||
post_set.tag_string
|
||||
).map {|x| x[0]}
|
||||
)
|
||||
end
|
||||
|
||||
def posts
|
||||
post_set.posts
|
||||
end
|
||||
|
||||
def tag_list_html(template)
|
||||
tag_set_presenter.tag_list_html(template)
|
||||
end
|
||||
|
||||
def post_previews_html(template)
|
||||
html = ""
|
||||
|
||||
if posts.empty?
|
||||
return template.render(:partial => "post_sets/blank")
|
||||
end
|
||||
|
||||
posts.each do |post|
|
||||
html << PostPresenter.preview(post)
|
||||
end
|
||||
|
||||
html.html_safe
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -4,7 +4,7 @@
|
||||
<section id="search-box">
|
||||
<h1>Search Favorites</h1>
|
||||
<%= form_tag(favorites_path, :method => "get") do %>
|
||||
<%= text_field_tag("tags", params[:tags], :size => 20) %>
|
||||
<%= text_field_tag("tags", @favorite_set.tag_string, :size => 20) %>
|
||||
<%= submit_tag "Go" %>
|
||||
<% end %>
|
||||
</section>
|
||||
@@ -25,12 +25,12 @@
|
||||
|
||||
<section id="content">
|
||||
<h1>Posts</h1>
|
||||
<%= @post_set.presenter.post_previews_html %>
|
||||
<%= @favorite_set.presenter.post_previews_html(self) %>
|
||||
|
||||
<div class="clearfix"></div>
|
||||
|
||||
<div class="paginator">
|
||||
<%= sequential_paginator(@post_set.favorites) %>
|
||||
<%= sequential_paginator(@favorite_set.favorites) %>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@
|
||||
<%= f.input :name %>
|
||||
<%= f.input :password %>
|
||||
<%= f.input :password_confirmation %>
|
||||
<%= f.input :email %>
|
||||
<%= f.input :email, :required => false %>
|
||||
<%= f.button :submit %>
|
||||
<% end %>
|
||||
|
||||
|
||||
@@ -9,11 +9,7 @@ class CreateUsers < ActiveRecord::Migration
|
||||
t.column :email_verification_key, :string
|
||||
t.column :inviter_id, :integer
|
||||
t.column :is_banned, :boolean, :null => false, :default => false
|
||||
t.column :is_privileged, :boolean, :null => false, :default => false
|
||||
t.column :is_contributor, :boolean, :null => false, :default => false
|
||||
t.column :is_janitor, :boolean, :null => false, :default => false
|
||||
t.column :is_moderator, :boolean, :null => false, :default => false
|
||||
t.column :is_admin, :boolean, :null => false, :default => false
|
||||
t.column :level, :integer, :null => false, :default => 0
|
||||
t.column :base_upload_limit, :integer, :null => false, :default => 10
|
||||
|
||||
# Cached data
|
||||
|
||||
Reference in New Issue
Block a user