2012-04-10 16:45:38 +01:00
|
|
|
# -*- encoding : utf-8 -*-
|
|
|
|
module Modules
|
|
|
|
module Models
|
|
|
|
module ActsLikeMember
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2012-05-02 10:18:07 +01:00
|
|
|
included do
|
|
|
|
scope :not_member_of, lambda {|item|
|
2012-04-10 16:45:38 +01:00
|
|
|
where("
|
2012-05-02 10:18:07 +01:00
|
|
|
#{table_name}.id NOT IN (
|
2012-04-26 02:38:33 +01:00
|
|
|
SELECT relations.actor_id
|
2012-04-10 16:45:38 +01:00
|
|
|
FROM relations
|
|
|
|
WHERE (
|
2012-05-02 10:18:07 +01:00
|
|
|
relations.actor_type = '#{self.to_s}'
|
2012-04-10 16:45:38 +01:00
|
|
|
AND relations.target_type = '#{item.class.to_s}'
|
|
|
|
AND relations.target_id = #{item.id}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
")
|
|
|
|
}
|
|
|
|
scope :search_order, order("CHAR_LENGTH(uname) ASC")
|
2012-05-02 10:18:07 +01:00
|
|
|
scope :without, lambda {|a| where("#{table_name}.id NOT IN (?)", a)}
|
|
|
|
scope :by_uname, lambda {|n| where("#{table_name}.uname ILIKE ?", n)}
|
|
|
|
scope :search, lambda {|q| by_uname("%#{q.to_s.strip}%")}
|
|
|
|
end
|
2012-04-10 16:45:38 +01:00
|
|
|
|
2012-05-02 10:18:07 +01:00
|
|
|
def to_param
|
|
|
|
uname
|
2012-04-10 16:45:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2012-05-03 15:08:02 +01:00
|
|
|
def find_by_insensitive_uname(uname)
|
|
|
|
find_by_uname(uname) || by_uname(uname).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_by_insensitive_uname!(uname)
|
|
|
|
find_by_insensitive_uname(uname) or raise ActiveRecord::RecordNotFound
|
2012-05-02 10:18:07 +01:00
|
|
|
end
|
2012-04-10 16:45:38 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|