refactored search

This commit is contained in:
albert
2013-01-10 17:45:52 -05:00
parent 13271e9bf5
commit 8749c43b3e
85 changed files with 946 additions and 304 deletions

View File

@@ -8,8 +8,29 @@ class PostAppeal < ActiveRecord::Base
validate :validate_creator_is_not_limited
before_validation :initialize_creator, :on => :create
validates_uniqueness_of :creator_id, :scope => :post_id, :message => "have already appealed this post"
scope :for_user, lambda {|user_id| where(["creator_id = ?", user_id])}
scope :recent, lambda {where(["created_at >= ?", 1.day.ago])}
module SearchMethods
def for_user(user_id)
where("creator_id = ?", user_id)
end
def recent
where("created_at >= ?", 1.day.ago)
end
def search(params)
q = scoped
return q if params.blank?
if params[:post_id]
q = q.where("post_id = ?", params[:post_id].to_i)
end
q
end
end
extend SearchMethods
def validate_creator_is_not_limited
if appeal_count_for_creator >= Danbooru.config.max_appeals_per_day