Class: AspectsMembershipService

Inherits:
Object
  • Object
show all
Defined in:
app/services/aspects_membership_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(user = nil) ⇒ AspectsMembershipService

Returns a new instance of AspectsMembershipService.


4
5
6
# File 'app/services/aspects_membership_service.rb', line 4

def initialize(user=nil)
  @user = user
end

Instance Method Details

#all_contactsObject


46
47
48
49
50
# File 'app/services/aspects_membership_service.rb', line 46

def all_contacts
  order = ["profiles.first_name ASC", "profiles.last_name ASC",
           "profiles.diaspora_handle ASC"]
  @user.contacts.includes(person: :profile).order(order)
end

#contacts_in_aspect(aspect_id) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/services/aspects_membership_service.rb', line 31

def contacts_in_aspect(aspect_id)
  order = [Arel.sql("contact_id IS NOT NULL DESC"), "profiles.first_name ASC", "profiles.last_name ASC",
           "profiles.diaspora_handle ASC"]
  @user.aspects.find(aspect_id) # to provide better error code if aspect isn't correct
  contacts = @user.contacts.arel_table
  aspect_memberships = AspectMembership.arel_table
  @user.contacts.joins(
    contacts.join(aspect_memberships).on(
      aspect_memberships[:aspect_id].eq(aspect_id).and(
        aspect_memberships[:contact_id].eq(contacts[:id])
      )
    ).join_sources
  ).includes(person: :profile).order(order)
end

#create(aspect_id, person_id) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)

8
9
10
11
12
13
14
15
16
17
# File 'app/services/aspects_membership_service.rb', line 8

def create(aspect_id, person_id)
  person = Person.find(person_id)
  aspect = @user.aspects.where(id: aspect_id).first
  raise ActiveRecord::RecordNotFound unless person.present? && aspect.present?

  contact = @user.share_with(person, aspect)
  raise I18n.t("aspects.add_to_aspect.failure") if contact.blank?

  AspectMembership.where(contact_id: contact.id, aspect_id: aspect.id).first
end

#destroy_by_ids(aspect_id, contact_id) ⇒ Object


19
20
21
22
23
# File 'app/services/aspects_membership_service.rb', line 19

def destroy_by_ids(aspect_id, contact_id)
  aspect = @user.aspects.where(id: aspect_id).first
  contact = @user.contacts.where(person_id: contact_id).first
  destroy(aspect, contact)
end

#destroy_by_membership_id(membership_id) ⇒ Object


25
26
27
28
29
# File 'app/services/aspects_membership_service.rb', line 25

def destroy_by_membership_id(membership_id)
  aspect = @user.aspects.joins(:aspect_memberships).where(aspect_memberships: {id: membership_id}).first
  contact = @user.contacts.joins(:aspect_memberships).where(aspect_memberships: {id: membership_id}).first
  destroy(aspect, contact)
end