Class: Locomotive::MembershipService

Inherits:
Struct
  • Object
show all
Includes:
Concerns::ActivityService
Defined in:
app/services/locomotive/membership_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::ActivityService

#track_activity, #without_tracking_activity

Instance Attribute Details

#policyObject

Returns the value of attribute policy

Returns:

  • (Object)

    the current value of policy


2
3
4
# File 'app/services/locomotive/membership_service.rb', line 2

def policy
  @policy
end

#siteObject

Returns the value of attribute site

Returns:

  • (Object)

    the current value of site


2
3
4
# File 'app/services/locomotive/membership_service.rb', line 2

def site
  @site
end

Instance Method Details

#accountObject


56
57
58
# File 'app/services/locomotive/membership_service.rb', line 56

def 
  policy.
end

#change_role(membership, role) ⇒ Boolean

Change the role of a membership depending on the current policy.

Accounts should not be able to set the role of another account to be higher than their own. A designer for example is not able to set another account to be an administrator.

Parameters:

  • membership (String)

    The membership to update

  • role (String)

    The new role

Returns:

  • (Boolean)

    True if everything went well


45
46
47
48
49
50
51
52
53
54
# File 'app/services/locomotive/membership_service.rb', line 45

def change_role(membership, role)
  membership.role = role if role.present?

  if role.present? && policy.change_role?
    membership.save
  else
    membership.errors.add(:role, :invalid)
    false
  end
end

#create(email_or_account) ⇒ Object

Create a new membership for the site assigned to that service. In case, no account is found from the email passed in parameter, this method will return nil. By default, the author role will be set to the new membership.

Parameters:

  • email_or_account (Object)

    The email or the account ifself

Returns:

  • (Object)

    A new membership (with errors or not) or nil (no account found)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/locomotive/membership_service.rb', line 15

def create()
   = if .respond_to?(:email)
    
  else
    Locomotive::Account.find_by_email()
  end

  if 
    site.memberships.create(account: , email: .email).tap do |success|
      if success
        track_activity 'membership.created', parameters: { name: .name, email: .email }
      end
    end
  else
    nil
  end
end