diff --git a/app/models/comment.rb b/app/models/comment.rb index 5fe754c8b..b71fcaa22 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -30,8 +30,8 @@ class Comment < ActiveRecord::Base validates :text, :presence => true, :length => {:maximum => 65535} validates :parent, :presence => true #should be in relayable (pending on fixing Message) - scope :including_author, includes(:author => :profile) - scope :for_a_stream, including_author.merge(order('created_at ASC')) + scope :including_author, -> { includes(:author => :profile) } + scope :for_a_stream, -> { including_author.merge(order('created_at ASC')) } before_save do self.text.strip! unless self.text.nil? diff --git a/app/models/contact.rb b/app/models/contact.rb index 1a428a898..1d7755784 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -7,7 +7,7 @@ class Contact < ActiveRecord::Base belongs_to :person validates :person, :presence => true - + delegate :name, :diaspora_handle, :guid, :first_name, to: :person, prefix: true @@ -26,26 +26,20 @@ class Contact < ActiveRecord::Base before_destroy :destroy_notifications - scope :all_contacts_of_person, lambda {|x| where(:person_id => x.id)} + scope :all_contacts_of_person, ->(x) { where(:person_id => x.id) } # contact.sharing is true when contact.person is sharing with contact.user - scope :sharing, lambda { - where(:sharing => true) - } + scope :sharing, -> { where(:sharing => true) } # contact.receiving is true when contact.user is sharing with contact.person - scope :receiving, lambda { - where(:receiving => true) - } + scope :receiving, -> { where(:receiving => true) } - scope :for_a_stream, lambda { + scope :for_a_stream, -> { includes(:aspects, :person => :profile). order('profiles.last_name ASC') } - scope :only_sharing, lambda { - sharing.where(:receiving => false) - } + scope :only_sharing, -> { sharing.where(:receiving => false) } def destroy_notifications Notification.where(:target_type => "Person", diff --git a/app/models/person.rb b/app/models/person.rb index 765a8539a..163f94e48 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -63,32 +63,40 @@ class Person < ActiveRecord::Base validates :serialized_public_key, :presence => true validates :diaspora_handle, :uniqueness => true - scope :searchable, joins(:profile).where(:profiles => {:searchable => true}) - scope :remote, where('people.owner_id IS NULL') - scope :local, where('people.owner_id IS NOT NULL') - scope :for_json, select('DISTINCT people.id, people.guid, people.diaspora_handle').includes(:profile) + scope :searchable, -> { joins(:profile).where(:profiles => {:searchable => true}) } + scope :remote, -> { where('people.owner_id IS NULL') } + scope :local, -> { where('people.owner_id IS NOT NULL') } + scope :for_json, -> { + select('DISTINCT people.id, people.guid, people.diaspora_handle') + .includes(:profile) + } # @note user is passed in here defensively - scope :all_from_aspects, lambda { |aspect_ids, user| + scope :all_from_aspects, ->(aspect_ids, user) { joins(:contacts => :aspect_memberships). where(:contacts => {:user_id => user.id}). where(:aspect_memberships => {:aspect_id => aspect_ids}) } - scope :unique_from_aspects, lambda{ |aspect_ids, user| + scope :unique_from_aspects, ->(aspect_ids, user) { all_from_aspects(aspect_ids, user).select('DISTINCT people.*') } #not defensive - scope :in_aspects, lambda { |aspect_ids| + scope :in_aspects, ->(aspect_ids) { joins(:contacts => :aspect_memberships). where(:aspect_memberships => {:aspect_id => aspect_ids}) } - scope :profile_tagged_with, lambda{|tag_name| joins(:profile => :tags).where(:tags => {:name => tag_name}).where('profiles.searchable IS TRUE') } + scope :profile_tagged_with, ->(tag_name) { + joins(:profile => :tags) + .where(:tags => {:name => tag_name}) + .where('profiles.searchable IS TRUE') + } - scope :who_have_reshared_a_users_posts, lambda{|user| - joins(:posts).where(:posts => {:root_guid => StatusMessage.guids_for_author(user.person), :type => 'Reshare'} ) + scope :who_have_reshared_a_users_posts, ->(user) { + joins(:posts) + .where(:posts => {:root_guid => StatusMessage.guids_for_author(user.person), :type => 'Reshare'} ) } def self.community_spotlight diff --git a/app/models/photo.rb b/app/models/photo.rb index 60494cc06..1c36cd641 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -48,8 +48,13 @@ class Photo < ActiveRecord::Base after_commit :on => :create do queue_processing_job if self.author.local? + end + scope :on_statuses, ->(post_guids) { + where(:status_message_guid => post_guids) + } + def clear_empty_status_message if self.status_message && self.status_message.text_and_photos_blank? self.status_message.destroy @@ -132,8 +137,4 @@ class Photo < ActiveRecord::Base def mutable? true end - - scope :on_statuses, lambda { |post_guids| - where(:status_message_guid => post_guids) - } end diff --git a/app/models/post.rb b/app/models/post.rb index 0cbc9f07d..832a7dbd6 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -31,14 +31,22 @@ class Post < ActiveRecord::Base end #scopes - scope :includes_for_a_stream, includes(:o_embed_cache, :open_graph_cache, {:author => :profile}, :mentions => {:person => :profile}) #note should include root and photos, but i think those are both on status_message - - - scope :commented_by, lambda { |person| - select('DISTINCT posts.*').joins(:comments).where(:comments => {:author_id => person.id}) + scope :includes_for_a_stream, -> { + includes(:o_embed_cache, + :open_graph_cache, + {:author => :profile}, + :mentions => {:person => :profile} + ) #note should include root and photos, but i think those are both on status_message } - scope :liked_by, lambda { |person| + + scope :commented_by, ->(person) { + select('DISTINCT posts.*') + .joins(:comments) + .where(:comments => {:author_id => person.id}) + } + + scope :liked_by, ->(person) { joins(:likes).where(:likes => {:author_id => person.id}) } diff --git a/app/models/share_visibility.rb b/app/models/share_visibility.rb index d2a92e2de..060f65cd2 100644 --- a/app/models/share_visibility.rb +++ b/app/models/share_visibility.rb @@ -6,10 +6,11 @@ class ShareVisibility < ActiveRecord::Base belongs_to :contact belongs_to :shareable, :polymorphic => :true - scope :for_a_users_contacts, lambda { |user| + scope :for_a_users_contacts, ->(user) { where(:contact_id => user.contacts.map {|c| c.id}) } - scope :for_contacts_of_a_person, lambda { |person| + + scope :for_contacts_of_a_person, ->(person) { where(:contact_id => person.contacts.map {|c| c.id}) } diff --git a/app/models/status_message.rb b/app/models/status_message.rb index 80a43c62c..07ac062db 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -40,7 +40,7 @@ class StatusMessage < Post after_commit :queue_gather_open_graph_data, :on => :create, :if => :contains_open_graph_url_in_text? #scopes - scope :where_person_is_mentioned, lambda { |person| + scope :where_person_is_mentioned, ->(person) { joins(:mentions).where(:mentions => {:person_id => person.id}) } diff --git a/app/models/user.rb b/app/models/user.rb index 4419058c2..61d61b299 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -10,11 +10,11 @@ class User < ActiveRecord::Base apply_simple_captcha :message => I18n.t('simple_captcha.message.failed'), :add_to_base => true - scope :logged_in_since, lambda { |time| where('last_seen > ?', time) } - scope :monthly_actives, lambda { |time = Time.now| logged_in_since(time - 1.month) } - scope :daily_actives, lambda { |time = Time.now| logged_in_since(time - 1.day) } - scope :yearly_actives, lambda { |time = Time.now| logged_in_since(time - 1.year) } - scope :halfyear_actives, lambda { |time = Time.now| logged_in_since(time - 6.month) } + scope :logged_in_since, ->(time) { where('last_seen > ?', time) } + scope :monthly_actives, ->(time = Time.now) { logged_in_since(time - 1.month) } + scope :daily_actives, ->(time = Time.now) { logged_in_since(time - 1.day) } + scope :yearly_actives, ->(time = Time.now) { logged_in_since(time - 1.year) } + scope :halfyear_actives, ->(time = Time.now) { logged_in_since(time - 6.month) } devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, diff --git a/lib/diaspora/shareable.rb b/lib/diaspora/shareable.rb index 6603a45b8..576c0dcce 100644 --- a/lib/diaspora/shareable.rb +++ b/lib/diaspora/shareable.rb @@ -19,7 +19,7 @@ module Diaspora delegate :id, :name, :first_name, to: :author, prefix: true #scopes - scope :all_public, where(:public => true, :pending => false) + scope :all_public, -> { where(:public => true, :pending => false) } def self.owned_or_visible_by_user(user) self.joins("LEFT OUTER JOIN share_visibilities ON share_visibilities.shareable_id = posts.id AND share_visibilities.shareable_type = 'Post'").