Class: Locomotive::MembershipService
- Inherits:
-
Struct
- Object
- Struct
- Locomotive::MembershipService
- Includes:
- Concerns::ActivityService
- Defined in:
- app/services/locomotive/membership_service.rb
Instance Attribute Summary collapse
-
#policy ⇒ Object
Returns the value of attribute policy.
-
#site ⇒ Object
Returns the value of attribute site.
Instance Method Summary collapse
- #account ⇒ Object
-
#change_role(membership, role) ⇒ Boolean
Change the role of a membership depending on the current policy.
-
#create(email_or_account) ⇒ Object
Create a new membership for the site assigned to that service.
Methods included from Concerns::ActivityService
#track_activity, #without_tracking_activity
Instance Attribute Details
#policy ⇒ Object
Returns the value of attribute policy
2 3 4 |
# File 'app/services/locomotive/membership_service.rb', line 2 def policy @policy end |
#site ⇒ Object
Returns the value of attribute site
2 3 4 |
# File 'app/services/locomotive/membership_service.rb', line 2 def site @site end |
Instance Method Details
#account ⇒ Object
56 57 58 |
# File 'app/services/locomotive/membership_service.rb', line 56 def account policy.account 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.
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.
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(email_or_account) _account = if email_or_account.respond_to?(:email) email_or_account else Locomotive::Account.find_by_email(email_or_account) end if _account site.memberships.create(account: _account, email: _account.email).tap do |success| if success track_activity 'membership.created', parameters: { name: _account.name, email: _account.email } end end else nil end end |