http://www.zhlwish.com/2012/07/23/rails-activesupport-concern/
# autoload concerns
module YourApp
class Application < Rails::Application
# config.autoload_paths += %W(
# #{config.root}/app/controllers/concerns/
# #{config.root}/app/models/concerns/
# )
config.autoload_paths += Dir[ Rails.root.join('app', 'models', "concerns", '**/') ]
config.autoload_paths += Dir[ Rails.root.join('app', 'controllers', "concerns", '**/') ] end
end
# app/models/concerns/trashable.rb
module Trashable
extend ActiveSupport::Concern
included do
default_scope where(trashed: false)
scope :trashed, where(trashed: true)
end
def trash
update_attribute :trashed, true
end
end
# app/models/message.rb
class Message < ActiveRecord::Base
include Trashable, Subscribable, Commentable, Eventable
end
# app/controllers/commend_calculate.rb
# encoding: UTF-8
module CommendCalculate
extend ActiveSupport::Concern
included do
#before_filter :do_something
# ....
end
# 智能推荐
def smart_commend_users(user, exclude_users, paginate_params)
normal_user_smart_commend(user, exclude_users, paginate_params)
end
#provider an user and exclude_users return an user list
#users_similar(ids = [1,2,3],[1,2,3])
def users_similar(user, exclude_users)
hobbys_ids = user.Hobbys.split(",").collect{|h| h.first if h.present?}.uniq
look_for_ids = user.Lookfor.split(",").collect{|h| h.first if h.present?}.uniq
if hobbys_ids.present? and look_for_ids.present?
hobbys_regexp_str = hobbys_ids.collect{|i| "#{i}0" << '\d|'}.join('').gsub(/\|$/, "")
look_for_regexp_str = look_for_ids.collect{|i| "#{i}" << '|'}.join('').gsub(/\|$/, "")
users = GxUser.desc(:active_score).where(Hobbys: /#{hobbys_regexp_str}/, Lookfor: /#{look_for_regexp_str}/ ).where({:UserID => {"$nin" => exclude_users}, :UserName => {"$ne" => ""}, :HeadImage => {"$ne" => nil}, :Status => "1", :IsOnline => "1"})
end
end
def new_user_smart_commend(user, exclude_users, paginate_params)
#users = GxUser.desc(:active_score)
users = users_similar(hobbys_id, exclude_users)
# 分页
if paginate_params.present?
user_paginate = MongoPaginator.new(users, paginate_params)
else
users
end
end
# 智能推荐 主体函数 可接收 用户实例, 排除用户ids, 分页参数 示例见最下方测试代码
def normal_user_smart_commend(user, exclude_users, paginate_params)
if user.present? and user.is_a? GxUser
users = GxCommendUser.desc(:relation_score).where({commend_user_id: {"$nin" => exclude_users}}).where({user_id: user.UserID})
# 如果 没有 推送用户则 按新用户处理
# users = users_same_hobby(user, exclude_users) unless users.present?
unless users.present?
user.generate_commend_users
users = GxCommendUser.desc(:relation_score).where({commend_user_id: {"$nin" => exclude_users}}).where({user_id: user.UserID})
end
users
end
# 分页
if paginate_params.present?
user_paginate = MongoPaginator.new(users, paginate_params)
else
users
end
end
# 可能认识
def friend_users(user, exclude_users)
if user.present? and user.is_a? GxUser
GxUser.near(:GPS => user[:GPS]).where({:UserID => {"$nin" => exclude_users}, :UserName => {"$ne" => ""}, :HeadImage => {"$ne" => nil}, :Status => "1", :IsOnline => "1"})
end
end
# 兴趣爱好
def hobby_users(user, exclude_users)
if user.present? and user.is_a? GxUser
GxUser.near(:GPS => user[:GPS]).where({:UserID => {"$nin" => exclude_users}, :UserName => {"$ne" => ""}, :HeadImage => {"$ne" => nil}, :Status => "1", :IsOnline => "1"})
end
end
# 附近的人
# nearby_users(user, debug = "1", paginate_params = {pagesize: '10', pageindex: "1"})
def nearby_users(user, exclude_users, debug = nil, paginate_params = {})
if user.present? and user.is_a? GxUser
if debug.eql? "1"
users = GxUser.where({:UserID => {"$nin" => exclude_users}, :UserName => {"$ne" => ""}, :HeadImage => {"$ne" => nil}, :XjTag => "0", :Status => "1",:IsOnline => "1"}).sort(_id:-1)
else
users = GxUser.near(:GPS => user[:GPS]).where({:UserID => {"$nin" => exclude_users}, :UserName => {"$ne" => ""}, :HeadImage => {"$ne" => nil}, :Status => "1",:IsOnline => "1"})
end
# 分页
if paginate_params.present?
user_paginate = MongoPaginator.new(users, paginate_params)
else
users
end
end
end
# gender "0" "1"
def nearby_single_gender_users(user, exclude_users, gender, paginate_params = {})
users = GxUser.near(:GPS => user[:GPS]).where({:UserID => {"$nin" => exclude_users}, :UserName => {"$ne" => ""}, :HeadImage => {"$ne" => nil}, :Status => "1", :Gender => gender, :IsOnline => "1"})
# 分页
if paginate_params.present?
user_paginate = MongoPaginator.new(users, paginate_params)
else
users
end
end
# 格式化 commend_user_list 返回 由 user hash 组成的数组
def format_commend_user_list(commend_user_list, noGPS = false)
commend_users = []
commend_user_list.each do |commend_user|
if commend_user.is_a? GxCommendUser
commend_user = GxUser.where(UserID: commend_user.commend_user_id).first
end
if commend_user.present?
if noGPS
commend_user.Distance= 999000
commend_user.JLscore= commend_user.Distance
end
commend_users.append(commend_user.commend_user_hash)
end
end
commend_users
end
end
############################################
=begin
#test case should be better rspec or unit test
include CommendCalculate
user = GxUser.last
exclude_users = GxBlock.where({UserID: @userid}).distinct("TargetuserID")
exclude_users.concat([@userid, "1", "2"])
exclude_users.concat([user.UserID, "1", "2"])
paginate_params = {pagesize: "20", pageindex: "1"}
smart_commend_users(user, exclude_users, paginate_params).count
friend_users(user, exclude_users).count
hobby_users(user, exclude_users).count
nearby_users(user, exclude_users, debug = @debug).count
nearby_single_gender_users(user, exclude_users, gender ="0", paginate_params).count
=end