diff --git a/app/controllers/admins_controller.rb b/app/controllers/admins_controller.rb index 7bf3f6830..b9af10e9a 100644 --- a/app/controllers/admins_controller.rb +++ b/app/controllers/admins_controller.rb @@ -46,7 +46,7 @@ class AdminsController < Admin::AdminController end def stats - @popular_tags = ActsAsTaggableOn::Tagging.joins(:tag).limit(50).count(:group => :tag, :order => 'count(taggings.id) DESC') + @popular_tags = ActsAsTaggableOn::Tagging.joins(:tag).limit(50).order('count(taggings.id) DESC').group(:tag).count case params[:range] when "week" @@ -67,7 +67,7 @@ class AdminsController < Admin::AdminController create_hash(model, :range => range) end - @posts_per_day = Post.count(:group => "DATE(created_at)", :conditions => ["created_at >= ?", Date.today - 21.days], :order => "DATE(created_at) ASC") + @posts_per_day = Post.where("created_at >= ?", Date.today - 21.days).group("DATE(created_at)").order("DATE(created_at) ASC").count @most_posts_within = @posts_per_day.values.max.to_f @user_count = User.count diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 4354093bc..ef98e5b14 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -33,13 +33,11 @@ class NotificationsController < ApplicationController page = params[:page] || 1 per_page = params[:per_page] || 25 @notifications = WillPaginate::Collection.create(page, per_page, Notification.where(conditions).count ) do |pager| - result = Notification.find(:all, - :conditions => conditions, - :order => 'created_at desc', - :include => [:target, {:actors => :profile}], - :limit => pager.per_page, - :offset => pager.offset - ) + result = Notification.where(conditions) + .includes(:target, :actors => :profile) + .order('created_at desc') + .limit(pager.per_page) + .offset(pager.offset) pager.replace(result) end diff --git a/app/controllers/tag_followings_controller.rb b/app/controllers/tag_followings_controller.rb index c447d9e92..94299bf8f 100644 --- a/app/controllers/tag_followings_controller.rb +++ b/app/controllers/tag_followings_controller.rb @@ -16,7 +16,7 @@ class TagFollowingsController < ApplicationController if name_normalized.nil? || name_normalized.empty? render :nothing => true, :status => 403 else - @tag = ActsAsTaggableOn::Tag.find_or_create_by_name(name_normalized) + @tag = ActsAsTaggableOn::Tag.find_or_create_by(name: name_normalized) @tag_following = current_user.tag_followings.new(:tag_id => @tag.id) if @tag_following.save @@ -31,7 +31,7 @@ class TagFollowingsController < ApplicationController # DELETE /tag_followings/1.xml def destroy tag_following = current_user.tag_followings.find_by_tag_id( params['id'] ) - + if tag_following && tag_following.destroy respond_to do |format| format.any(:js, :json) { render :nothing => true, :status => 204 } diff --git a/app/models/conversation.rb b/app/models/conversation.rb index fee57d81f..ae776cd54 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -68,10 +68,10 @@ class Conversation < ActiveRecord::Base end def receive(user, person) - cnv = Conversation.find_or_create_by_guid(self.attributes) + cnv = Conversation.find_or_create_by(self.attributes) self.participants.each do |participant| - ConversationVisibility.find_or_create_by_conversation_id_and_person_id(cnv.id, participant.id) + ConversationVisibility.find_or_create_by(conversation_id: cnv.id, person_id: participant.id) end self.messages.each do |msg| diff --git a/app/models/o_embed_cache.rb b/app/models/o_embed_cache.rb index 9aee6d00b..17e6f073f 100644 --- a/app/models/o_embed_cache.rb +++ b/app/models/o_embed_cache.rb @@ -10,10 +10,10 @@ class OEmbedCache < ActiveRecord::Base t.add :data end - def self.find_or_create_by_url(url) - cache = OEmbedCache.find_or_initialize_by_url(url) + def self.find_or_create_by(opts) + cache = OEmbedCache.find_or_initialize_by(opts) return cache if cache.persisted? - cache.fetch_and_save_oembed_data! + cache.fetch_and_save_oembed_data! # make after create callback and drop this method ? cache end diff --git a/app/models/open_graph_cache.rb b/app/models/open_graph_cache.rb index 8ec435719..e3b78e48a 100644 --- a/app/models/open_graph_cache.rb +++ b/app/models/open_graph_cache.rb @@ -21,10 +21,10 @@ class OpenGraphCache < ActiveRecord::Base t.add :url end - def self.find_or_create_by_url(url) - cache = OpenGraphCache.find_or_initialize_by_url(url) + def self.find_or_create_by(opts) + cache = OpenGraphCache.find_or_initialize_by(opts) cache.fetch_and_save_opengraph_data! unless cache.persisted? - cache if cache.persisted? + cache if cache.persisted? # Make this an after create callback and drop this method ? end def fetch_and_save_opengraph_data! diff --git a/app/models/pod.rb b/app/models/pod.rb index fb5f35477..dcd23b310 100644 --- a/app/models/pod.rb +++ b/app/models/pod.rb @@ -1,7 +1,7 @@ class Pod < ActiveRecord::Base - def self.find_or_create_by_url(url) - u = URI.parse(url) - pod = self.find_or_initialize_by_host(u.host) + def self.find_or_create_by(opts) # Rename this method to not override an AR method + u = URI.parse(opts.fetch(:url)) + pod = self.find_or_initialize_by(host: u.host) unless pod.persisted? pod.ssl = (u.scheme == 'https')? true : false pod.save diff --git a/app/models/role.rb b/app/models/role.rb index 40438a035..928035223 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -9,10 +9,10 @@ class Role < ActiveRecord::Base end def self.add_admin(person) - find_or_create_by_person_id_and_name(person.id, 'admin') + find_or_create_by(person_id: person.id, name: 'admin') end def self.add_spotlight(person) - find_or_create_by_person_id_and_name(person.id, 'spotlight') + find_or_create_by(person_id: person.id, name: 'spotlight') end end diff --git a/app/models/share_visibility.rb b/app/models/share_visibility.rb index 67f42661a..d2a92e2de 100644 --- a/app/models/share_visibility.rb +++ b/app/models/share_visibility.rb @@ -25,7 +25,11 @@ class ShareVisibility < ActiveRecord::Base if AppConfig.postgres? contact_ids.each do |contact_id| - ShareVisibility.find_or_create_by_contact_id_and_shareable_id_and_shareable_type(contact_id, share.id, share.class.base_class.to_s) + ShareVisibility.find_or_create_by( + contact_id: contact_id, + shareable_id: share.id, + shareable_type: share.class.base_class.to_s + ) end else new_share_visibilities_data = contact_ids.map do |contact_id| diff --git a/app/models/status_message.rb b/app/models/status_message.rb index 5f10f6aef..80a43c62c 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -98,7 +98,7 @@ class StatusMessage < Post def create_mentions ppl = Diaspora::Mentionable.people_from_string(self.raw_message) ppl.each do |person| - self.mentions.find_or_create_by_person_id(person.id) + self.mentions.find_or_create_by(person_id: person.id) end end diff --git a/app/models/user.rb b/app/models/user.rb index 2e9168a80..4419058c2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -102,7 +102,7 @@ class User < ActiveRecord::Base def invitation_code - InvitationCode.find_or_create_by_user_id(self.id) + InvitationCode.find_or_create_by(user_id: self.id) end def hidden_shareables @@ -163,14 +163,14 @@ class User < ActiveRecord::Base def update_user_preferences(pref_hash) if self.disable_mail - UserPreference::VALID_EMAIL_TYPES.each{|x| self.user_preferences.find_or_create_by_email_type(x)} + UserPreference::VALID_EMAIL_TYPES.each{|x| self.user_preferences.find_or_create_by(email_type: x)} self.disable_mail = false self.save end pref_hash.keys.each do |key| if pref_hash[key] == 'true' - self.user_preferences.find_or_create_by_email_type(key) + self.user_preferences.find_or_create_by(email_type: key) else block = self.user_preferences.where(:email_type => key).first if block diff --git a/app/models/user/connecting.rb b/app/models/user/connecting.rb index e90133330..6eaf55bef 100644 --- a/app/models/user/connecting.rb +++ b/app/models/user/connecting.rb @@ -8,7 +8,7 @@ module User::Connecting # @param [Aspect] aspect The aspect to add them to. # @return [Contact] The newly made contact for the passed in person. def share_with(person, aspect) - contact = self.contacts.find_or_initialize_by_person_id(person.id) + contact = self.contacts.find_or_initialize_by(person_id: person.id) return false unless contact.valid? unless contact.receiving? @@ -22,7 +22,7 @@ module User::Connecting if notification = Notification.where(:target_id => person.id).first notification.update_attributes(:unread=>false) end - + deliver_profile_update register_share_visibilities(contact) contact diff --git a/app/workers/gather_o_embed_data.rb b/app/workers/gather_o_embed_data.rb index 2f8c90119..cb0ad0819 100644 --- a/app/workers/gather_o_embed_data.rb +++ b/app/workers/gather_o_embed_data.rb @@ -9,7 +9,7 @@ module Workers def perform(post_id, url, retry_count=1) post = Post.find(post_id) - post.o_embed_cache = OEmbedCache.find_or_create_by_url(url) + post.o_embed_cache = OEmbedCache.find_or_create_by(url: url) post.save rescue ActiveRecord::RecordNotFound # User created a post and deleted it right afterwards before we diff --git a/app/workers/gather_open_graph_data.rb b/app/workers/gather_open_graph_data.rb index d73954d86..18c97ab8b 100644 --- a/app/workers/gather_open_graph_data.rb +++ b/app/workers/gather_open_graph_data.rb @@ -9,7 +9,7 @@ module Workers def perform(post_id, url, retry_count=1) post = Post.find(post_id) - post.open_graph_cache = OpenGraphCache.find_or_create_by_url(url) + post.open_graph_cache = OpenGraphCache.find_or_create_by(url: url) post.save rescue ActiveRecord::RecordNotFound # User created a post and deleted it right afterwards before we diff --git a/features/step_definitions/user_steps.rb b/features/step_definitions/user_steps.rb index 89f49be03..b81869cd5 100644 --- a/features/step_definitions/user_steps.rb +++ b/features/step_definitions/user_steps.rb @@ -201,7 +201,7 @@ end Given /^I visit alice's invitation code url$/ do @alice ||= FactoryGirl.create(:user, :username => 'alice', :getting_started => false) - invite_code = InvitationCode.find_or_create_by_user_id(@alice.id) + invite_code = InvitationCode.find_or_create_by(user_id: @alice.id) visit invite_code_path(invite_code) end diff --git a/lib/diaspora/exporter.rb b/lib/diaspora/exporter.rb index 80f0814f1..6360d635c 100644 --- a/lib/diaspora/exporter.rb +++ b/lib/diaspora/exporter.rb @@ -37,7 +37,7 @@ module Diaspora #} xml.post_ids { - aspect.posts.find_all_by_author_id(user_person_id).each do |post| + aspect.posts.where(author_id: user_person_id).each do |post| xml.post_id post.id end } @@ -64,7 +64,7 @@ module Diaspora } xml.posts { - user.visible_shareables(Post).find_all_by_author_id(user_person_id).each do |post| + user.visible_shareables(Post).where(author_id: user_person_id).each do |post| #post.comments.each do |comment| # post_doc << comment.to_xml #end diff --git a/lib/diaspora/federated/request.rb b/lib/diaspora/federated/request.rb index 0b0f446b3..a920fba7c 100644 --- a/lib/diaspora/federated/request.rb +++ b/lib/diaspora/federated/request.rb @@ -75,10 +75,10 @@ class Request def receive(user, person) Rails.logger.info("event=receive payload_type=request sender=#{self.sender} to=#{self.recipient}") - contact = user.contacts.find_or_initialize_by_person_id(self.sender.id) + contact = user.contacts.find_or_initialize_by(person_id: self.sender.id) contact.sharing = true contact.save - + user.share_with(person, user.auto_follow_back_aspect) if user.auto_follow_back && !contact.receiving self diff --git a/lib/hydra_wrapper.rb b/lib/hydra_wrapper.rb index e22d94d7f..f7f3c7c2b 100644 --- a/lib/hydra_wrapper.rb +++ b/lib/hydra_wrapper.rb @@ -83,7 +83,7 @@ class HydraWrapper def prepare_request request, people_for_receive_url request.on_complete do |response| # Save the reference to the pod to the database if not already present - Pod.find_or_create_by_url response.effective_url + Pod.find_or_create_by(url: response.effective_url) if redirecting_to_https? response Person.url_batch_update people_for_receive_url, response.headers_hash['Location'] diff --git a/lib/tasks/migrations.rake b/lib/tasks/migrations.rake index f0b8498d0..20d7d42d8 100644 --- a/lib/tasks/migrations.rake +++ b/lib/tasks/migrations.rake @@ -82,7 +82,7 @@ namespace :migrations do puts "found #{evil_tags.count} tags to convert..." evil_tags.each_with_index do |tag, i| - good_tag = ActsAsTaggableOn::Tag.find_or_create_by_name(tag.name.mb_chars.downcase) + good_tag = ActsAsTaggableOn::Tag.first_or_create_by(name: tag.name.mb_chars.downcase) puts "++ '#{tag.name}' has #{tag.taggings.count} records attached" taggings = tag.taggings diff --git a/spec/lib/stream/multi_spec.rb b/spec/lib/stream/multi_spec.rb index 6b13c046f..bdee98683 100644 --- a/spec/lib/stream/multi_spec.rb +++ b/spec/lib/stream/multi_spec.rb @@ -40,7 +40,7 @@ describe Stream::Multi do describe "#publisher_prefill" do before do - @tag = ActsAsTaggableOn::Tag.find_or_create_by_name("cats") + @tag = ActsAsTaggableOn::Tag.find_or_create_by(name: "cats") @tag_following = alice.tag_followings.create(:tag_id => @tag.id) @stream = Stream::Multi.new(alice) diff --git a/spec/models/pod_spec.rb b/spec/models/pod_spec.rb index 5cee347e9..8a979a21f 100644 --- a/spec/models/pod_spec.rb +++ b/spec/models/pod_spec.rb @@ -1,14 +1,14 @@ require 'spec_helper' describe Pod do - describe '.find_or_create_by_url' do + describe '.find_or_create_by' do it 'takes a url, and makes one by host' do - pod = Pod.find_or_create_by_url('https://joindiaspora.com/maxwell') + pod = Pod.find_or_create_by(url: 'https://joindiaspora.com/maxwell') pod.host.should == 'joindiaspora.com' end it 'sets ssl boolean(side-effect)' do - pod = Pod.find_or_create_by_url('https://joindiaspora.com/maxwell') + pod = Pod.find_or_create_by(url: 'https://joindiaspora.com/maxwell') pod.ssl.should be_true end end diff --git a/spec/models/user/connecting_spec.rb b/spec/models/user/connecting_spec.rb index e01e679e8..5d64d172c 100644 --- a/spec/models/user/connecting_spec.rb +++ b/spec/models/user/connecting_spec.rb @@ -113,7 +113,7 @@ describe User::Connecting do it 'adds a contact to an aspect' do contact = alice.contacts.create(:person => eve.person) - alice.contacts.stub(:find_or_initialize_by_person_id).and_return(contact) + alice.contacts.stub(:find_or_initialize_by).and_return(contact) lambda { alice.share_with(eve.person, alice.aspects.first) @@ -128,7 +128,7 @@ describe User::Connecting do context 'dispatching' do it 'dispatches a request on initial request' do contact = alice.contacts.new(:person => eve.person) - alice.contacts.stub(:find_or_initialize_by_person_id).and_return(contact) + alice.contacts.stub(:find_or_initialize_by).and_return(contact) contact.should_receive(:dispatch_request) alice.share_with(eve.person, alice.aspects.first) @@ -138,7 +138,7 @@ describe User::Connecting do eve.share_with(alice.person, eve.aspects.first) contact = alice.contact_for(eve.person) - alice.contacts.stub(:find_or_initialize_by_person_id).and_return(contact) + alice.contacts.stub(:find_or_initialize_by).and_return(contact) contact.should_receive(:dispatch_request) alice.share_with(eve.person, alice.aspects.first) @@ -148,7 +148,7 @@ describe User::Connecting do a2 = alice.aspects.create(:name => "two") contact = alice.contacts.create(:person => eve.person, :receiving => true) - alice.contacts.stub(:find_or_initialize_by_person_id).and_return(contact) + alice.contacts.stub(:find_or_initialize_by).and_return(contact) contact.should_not_receive(:dispatch_request) alice.share_with(eve.person, a2) diff --git a/spec/workers/gather_o_embed_data_spec.rb b/spec/workers/gather_o_embed_data_spec.rb index 361502c0e..1593ed4e0 100644 --- a/spec/workers/gather_o_embed_data_spec.rb +++ b/spec/workers/gather_o_embed_data_spec.rb @@ -51,7 +51,7 @@ describe Workers::GatherOEmbedData do OEmbedCache.find_by_url(@flickr_photo_url).data.should == expected_data Workers::GatherOEmbedData.new.perform(@status_message.id, @flickr_photo_url) - OEmbedCache.count(:conditions => {:url => @flickr_photo_url}).should == 1 + OEmbedCache.where(url: @flickr_photo_url).count.should == 1 end it 'creates no cache entry for unsupported pages' do diff --git a/spec/workers/gather_open_graph_data_spec.rb b/spec/workers/gather_open_graph_data_spec.rb index c7a2a8f2b..dbe53ff81 100644 --- a/spec/workers/gather_open_graph_data_spec.rb +++ b/spec/workers/gather_open_graph_data_spec.rb @@ -51,7 +51,7 @@ describe Workers::GatherOpenGraphData do ogc.description.should == @ogsite_description Workers::GatherOpenGraphData.new.perform(@status_message.id, @ogsite_url) - OpenGraphCache.count(:conditions => {:url => @ogsite_url}).should == 1 + OpenGraphCache.where(url: @ogsite_url).count.should == 1 end it 'creates no cache entry for unsupported pages' do