Permalink
Browse files

Return User objects instead of hashes

  • Loading branch information...
1 parent 1cd565e commit 5b01ac92880e8468f012abd2c844f63dd0db5b7a @blowmage committed Feb 7, 2013
Showing with 11 additions and 9 deletions.
  1. +2 −1 app/controllers/users_controller.rb
  2. +9 −8 app/models/user_search.rb
@@ -294,7 +294,8 @@ def search_users
results = UserSearch.search term, topic_id
- render :json => results
+ render json: { users: results.as_json( only: [ :username, :name ],
+ methods: :avatar_template ) }
end
private
@@ -1,15 +1,11 @@
class UserSearch
def self.search term, topic_id
- sql = sql term, topic_id
- results = User.exec_sql(sql, topic_id: topic_id, term_like: "#{term}%", term: term)
- results = results.map do |r|
- r["avatar_template"] = User.avatar_template(r["email"])
- r.delete("email")
- r
- end
+ User.find_by_sql sql(term, topic_id)
end
+ private
+
def self.sql term, topic_id
sql = "select username, name, email from users u "
if topic_id
@@ -36,7 +32,12 @@ def self.sql term, topic_id
end
sql << " case when last_seen_at is null then 0 else 1 end desc, last_seen_at desc, username asc limit(20)"
- sql
+
+ sanitize_sql_array(sql, topic_id: topic_id, term_like: "#{term}%", term: term)
+ end
+
+ def self.sanitize_sql_array *args
+ ActiveRecord::Base.send(:sanitize_sql_array, args)
end
end

0 comments on commit 5b01ac9

Please sign in to comment.