[issue #347] Added not_member_of scope. Added ActsLikeMember module

This commit is contained in:
George Vinogradov 2012-04-10 19:45:38 +04:00
parent 71c8efabf9
commit 442b44a68e
3 changed files with 37 additions and 6 deletions

View File

@ -16,13 +16,12 @@ class Group < ActiveRecord::Base
validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => { :with => /^[a-z0-9_]+$/ }
validate { errors.add(:uname, :taken) if User.where('uname LIKE ?', uname).present? }
scope :search_order, order("CHAR_LENGTH(uname) ASC")
scope :without, lambda {|a| where("groups.id NOT IN (?)", a)}
scope :search, lambda {|q| where("uname ILIKE ?", "%#{q.strip}%")}
scope :opened, where('1=1')
scope :by_owner, lambda {|owner| where(:owner_id => owner.id)}
scope :by_admin, lambda {|admin| joins(:objects).where(:'relations.role' => 'admin', :'relations.object_id' => admin.id, :'relations.object_type' => 'User')}
include Modules::Models::ActsLikeMember
attr_accessible :description
attr_readonly :own_projects_count

View File

@ -47,14 +47,13 @@ class User < ActiveRecord::Base
attr_readonly :uname, :own_projects_count
attr_accessor :login
scope :search_order, order("CHAR_LENGTH(uname) ASC")
scope :without, lambda {|a| where("users.id NOT IN (?)", a)}
scope :search, lambda {|q| where("uname ILIKE ?", "%#{q.strip}%")}
scope :opened, where('1=1')
scope :banned, where(:role => 'banned')
scope :admin, where(:role => 'admin')
scope :real, where(:role => ['', nil])
include Modules::Models::ActsLikeMember
after_create lambda { self.create_notifier }
def admin?

View File

@ -0,0 +1,33 @@
# -*- encoding : utf-8 -*-
module Modules
module Models
module ActsLikeMember
extend ActiveSupport::Concern
included do |klass|
scope :not_member_of, lambda { |item|
where("
#{klass.table_name}.id NOT IN (
SELECT relations.object_id
FROM relations
WHERE (
relations.object_type = '#{klass.to_s}'
AND relations.target_type = '#{item.class.to_s}'
AND relations.target_id = #{item.id}
)
)
")
}
scope :search_order, order("CHAR_LENGTH(uname) ASC")
scope :without, lambda {|a| where("#{klass.table_name}.id NOT IN (?)", a)}
scope :search, lambda {|q| where("#{klass.table_name}.uname ILIKE ?", "%#{q.strip}%")}
end
module ClassMethods
end
end
end
end