Commit 89afb616 authored by Jonne Haß's avatar Jonne Haß

replace deprecated scope syntax and unify it

parent 33c3b38f
......@@ -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?
......
......@@ -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",
......
......@@ -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
......
......@@ -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
......@@ -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 :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 :commented_by, ->(person) {
select('DISTINCT posts.*')
.joins(:comments)
.where(:comments => {:author_id => person.id})
}
scope :liked_by, lambda { |person|
scope :liked_by, ->(person) {
joins(:likes).where(:likes => {:author_id => person.id})
}
......
......@@ -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})
}
......
......@@ -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})
}
......
......@@ -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,
......
......@@ -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'").
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment