diff --git a/app/controllers/conversations_controller.rb b/app/controllers/conversations_controller.rb index e6a4e09f9..593217770 100644 --- a/app/controllers/conversations_controller.rb +++ b/app/controllers/conversations_controller.rb @@ -56,19 +56,14 @@ class ConversationsController < ApplicationController end def new - all_contacts_and_ids = Contact.connection.execute(current_user.contacts.joins(:person => :profile - ).select("contacts.id, profiles.first_name, profiles.last_name, people.diaspora_handle").to_sql).map do |r| - person_json_from_row r - end + all_contacts_and_ids = Contact.connection.select_rows( + current_user.contacts.joins(:person => :profile). + select("contacts.id, profiles.first_name, profiles.last_name, people.diaspora_handle").to_sql + ).map{|r| {:value => r[0], :name => Person.name_from_attrs(r[1], r[2], r[3]).gsub(/(")/, "'")} } + @contacts_json = all_contacts_and_ids.to_json.gsub!(/(")/, '\\"') @contact = current_user.contacts.find(params[:contact_id]) if params[:contact_id] render :layout => false end - def person_json_from_row r - r = [r["id"], r["first_name"], r["last_name"], r["diaspora_handle"]] if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) - {:value => r[0], - :name => Person.name_from_attrs(r[1], r[2], r[3]).gsub(/(")/, "'")} - end - end diff --git a/app/models/activity_streams/photo.rb b/app/models/activity_streams/photo.rb index d6641ddc1..a9ca83ced 100644 --- a/app/models/activity_streams/photo.rb +++ b/app/models/activity_streams/photo.rb @@ -23,9 +23,9 @@ class ActivityStreams::Photo < Post def socket_to_user(user_or_id, opts={}) #adds aspect_ids to opts if they are not there unless opts[:aspect_ids] user_id = user_or_id.instance_of?(Fixnum) ? user_or_id : user_or_id.id - aspect_ids = AspectMembership.connection.execute( + aspect_ids = AspectMembership.connection.select_values( AspectMembership.joins(:contact).where(:contacts => {:user_id => user_id, :person_id => self.author_id}).select('aspect_memberships.aspect_id').to_sql - ).map{|r| r.first} + ) opts.merge!(:aspect_ids => aspect_ids) end super(user_or_id, opts) diff --git a/app/models/person.rb b/app/models/person.rb index b59437b78..66b9994b5 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -80,7 +80,7 @@ class Person < ActiveRecord::Base # @return [Array] postgreSQL and mysql deal with null values in orders differently, it seems. def self.search_order @search_order ||= Proc.new { - order = if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) + order = if postgres? "ASC" else "DESC" diff --git a/app/models/status_message.rb b/app/models/status_message.rb index 7a6813427..74402012f 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -132,9 +132,9 @@ class StatusMessage < Post def socket_to_user(user_or_id, opts={}) unless opts[:aspect_ids] user_id = user_or_id.instance_of?(Fixnum) ? user_or_id : user_or_id.id - aspect_ids = AspectMembership.connection.execute( + aspect_ids = AspectMembership.connection.select_values( AspectMembership.joins(:contact).where(:contacts => {:user_id => user_id, :person_id => self.author_id}).select('aspect_memberships.aspect_id').to_sql - ).map{|r| r.first} + ) opts.merge!(:aspect_ids => aspect_ids) end super(user_or_id, opts) diff --git a/config/environment.rb b/config/environment.rb index 26adfb6b1..ba7e062ed 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -36,3 +36,8 @@ module Devise end end end + +# check what database you have +def postgres? + @using_postgres ||= defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) +end diff --git a/lib/diaspora/user/querying.rb b/lib/diaspora/user/querying.rb index 84b522ad1..4a6fa6680 100644 --- a/lib/diaspora/user/querying.rb +++ b/lib/diaspora/user/querying.rb @@ -48,26 +48,11 @@ module Diaspora all_posts = "#{posts_from_others.to_sql} UNION ALL #{posts_from_self.to_sql} ORDER BY #{opts[:order]} LIMIT #{opts[:limit]}" end - post_ids = Post.connection.execute(all_posts).map {|post| id_for(post) } + post_ids = Post.connection.select_values(all_posts) Post.where(:id => post_ids).select('DISTINCT posts.*').limit(opts[:limit]).order(order_with_table) end - # Determine, cache, and execute the method call needed to extract the id from a raw result row. - # Returns row["id"] for PostgreSQL - # Returns row.first for everything else (MYSQL) - # - # @param row The row to get the id from. - # @return The id of the database row passed in. - def id_for row - @@id_method_for_row ||= if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) - [:[], "id"] - else - :first - end - row.send(*@@id_method_for_row) - end - def visible_photos(opts = {}) visible_posts(opts.merge(:type => 'Photo')) end @@ -117,9 +102,13 @@ module Diaspora p = Post.arel_table post_ids = [] if contact = self.contact_for(person) - post_ids = Post.connection.execute(contact.post_visibilities.where(:hidden => false).select('post_visibilities.post_id').to_sql).map{|r| r.first} + post_ids = Post.connection.select_values( + contact.post_visibilities.where(:hidden => false).select('post_visibilities.post_id').to_sql + ) end - post_ids += Post.connection.execute(person.posts.where(:public => true).select('posts.id').to_sql).map{|r| r.first} + post_ids += Post.connection.select_values( + person.posts.where(:public => true).select('posts.id').to_sql + ) Post.where(:id => post_ids, :pending => false).select('DISTINCT posts.*').order("posts.created_at DESC") end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3eef3e598..6d4612e19 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -81,9 +81,5 @@ def remote_raphael end def photo_fixture_name - @photo_fixture_name = File.join(File.dirname(__FILE__), 'fixtures', 'button.png') -end - -def postgres? - defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) + @photo_fixture_name = File.join(File.dirname(__FILE__), 'fixtures', 'button.png') end