implement saved searches, move user boolean settings to bitprefs

This commit is contained in:
r888888888
2014-06-03 15:54:22 -07:00
parent 901aa1264c
commit f02f72fac1
27 changed files with 547 additions and 105 deletions

View File

@@ -0,0 +1,31 @@
class SavedSearch < ActiveRecord::Base
belongs_to :user
validates :tag_query, :presence => true
validate :validate_count
attr_accessible :tag_query, :category
before_create :update_user_on_create
after_destroy :update_user_on_destroy
validates_uniqueness_of :tag_query, :scope => :user_id
def self.tagged(tags)
where(:tag_query => tags).first
end
def validate_count
if user.saved_searches.count + 1 > user.max_saved_searches
self.errors[:user] << "can only have up to #{user.max_saved_searches} " + "saved search".pluralize(user.max_saved_searches)
end
end
def update_user_on_create
if !user.has_saved_searches?
user.update_attribute(:has_saved_searches, true)
end
end
def update_user_on_destroy
if user.saved_searches.count == 0
user.update_attribute(:has_saved_searches, false)
end
end
end

View File

@@ -16,6 +16,22 @@ class User < ActiveRecord::Base
ADMIN = 50
end
BOOLEAN_ATTRIBUTES = {
:is_banned => 0x0001,
:has_mail => 0x0002,
:receive_email_notifications => 0x0004,
:always_resize_images => 0x0008,
:enable_post_navigation => 0x0010,
:new_post_navigation_layout => 0x0020,
:enable_privacy_mode => 0x0040,
:enable_sequential_post_navigation => 0x0080,
:hide_deleted_posts => 0x0100,
:style_usernames => 0x0200,
:enable_auto_complete => 0x0400,
:show_deleted_children => 0x0800,
:has_saved_searches => 0x1000
}
attr_accessor :password, :old_password
attr_accessible :enable_privacy_mode, :enable_post_navigation, :new_post_navigation_layout, :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, :time_zone, :default_image_size, :enable_sequential_post_navigation, :per_page, :hide_deleted_posts, :style_usernames, :enable_auto_complete, :custom_style, :show_deleted_children, :as => [:moderator, :janitor, :contributor, :gold, :member, :anonymous, :default, :builder, :admin]
attr_accessible :level, :as => :admin
@@ -45,6 +61,7 @@ class User < ActiveRecord::Base
has_many :subscriptions, lambda {order("tag_subscriptions.name")}, :class_name => "TagSubscription", :foreign_key => "creator_id"
has_many :note_versions, :foreign_key => "updater_id"
has_many :dmails, lambda {order("dmails.id desc")}, :foreign_key => "owner_id"
has_many :saved_searches
belongs_to :inviter, :class_name => "User"
after_update :create_mod_action
@@ -417,6 +434,20 @@ class User < ActiveRecord::Base
end
module LimitMethods
def max_saved_searches
if is_platinum?
1_000
elsif is_gold?
200
else
100
end
end
def show_saved_searches?
id < 1_000
end
def can_upload?
if is_contributor?
true
@@ -703,6 +734,24 @@ class User < ActiveRecord::Base
include CountMethods
extend SearchMethods
BOOLEAN_ATTRIBUTES.each do |boolean_attribute, bit_flag|
define_method(boolean_attribute) do
bit_prefs & bit_flag > 0
end
define_method("#{boolean_attribute}?") do
bit_prefs & bit_flag > 0
end
define_method("#{boolean_attribute}=") do |val|
if val.to_s =~ /t|1|y/
self.bit_prefs = bit_prefs | bit_flag
else
self.bit_prefs = bit_prefs - bit_flag
end
end
end
def initialize_default_image_size
self.default_image_size = "large"
end