Class: Locomotive::MembershipService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#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



33
34
35
36
37
38
39
40
41
42
# File 'app/services/locomotive/membership_service.rb', line 33

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) ⇒ 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 (String)

    The email

Returns:

  • (Object)

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



13
14
15
16
17
18
19
# File 'app/services/locomotive/membership_service.rb', line 13

def create(email)
  if  = Locomotive::Account.find_by_email(email)
    site.memberships.create(account: , email: email)
  else
    nil
  end
end