diff --git a/app/models/group.rb b/app/models/group.rb index 4e8f6c12a..393069884 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 463264110..81545e56a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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? diff --git a/lib/modules/models/acts_like_member.rb b/lib/modules/models/acts_like_member.rb new file mode 100644 index 000000000..0db4dd577 --- /dev/null +++ b/lib/modules/models/acts_like_member.rb @@ -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 +