Class: Scrivito::MembershipCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
app/cms/scrivito/membership_collection.rb

Overview

The Scrivito::MembershipCollection includes all members of a given Workspace. You can access it using the Workspace#memberships method.

Instance Method Summary collapse

Instance Method Details

#[](id_or_user) ⇒ Scrivito::Membership?

Returns the membership of a user or nil.

Parameters:

Returns:



57
58
59
60
61
62
63
64
65
# File 'app/cms/scrivito/membership_collection.rb', line 57

def [](id_or_user)
  id = if id_or_user.respond_to?(:id)
    id_or_user.id
  else
    id_or_user
  end

  to_h[id]
end

#each {|Scrivito::Membership| ... } ⇒ Enumerator

Note:

For a complete list of the provided methods, please refer to the documentation of the Enumerable module.

Iterate over all Memberships of a specific Workspace. Allows you to use all the methods defined by Ruby’s Enumerable module.

Examples:

# Obtain all owners of a workspace.
my_workspace.memberships.select do |membership|
  membership.role == "owner"
end

# Get an array of all the members' user_ids.
my_workspace.memberships.map { |membership| membership.user_id }

# Or use it directly to iterate over all items.
my_workspace.memberships.each do |membership|
  puts "User #{membership.user_id} has the role #{membership.role}"
end

Yields:

Returns:

  • (Enumerator)

    if no block is given, an Enumerator is returned



36
# File 'app/cms/scrivito/membership_collection.rb', line 36

def_delegator :memberships, :each

#to_hHash<String, Scrivito::Membership>

Returns a hash where the keys are user_ids and the values are Membership instances.

Returns:



45
46
47
48
49
50
# File 'app/cms/scrivito/membership_collection.rb', line 45

def to_h
  memberships.inject(HashWithIndifferentAccess.new) do |hash, membership|
    hash[membership.user_id] = membership
    hash
  end
end