Module: UserCorporations

Extended by:
ActiveSupport::Concern
Included in:
User
Defined in:
app/models/concerns/user_corporations.rb

Overview

This module contains all the Corporation-related methods of a User.

A user can be member of several corporations. Therefore, User responds to ‘User#corporations`.

There is also a usecase, where a User should have a primary corporation or be just in one Corporation at all. For those usecases, we provide the ‘User#corporation` method—and the `User#corporation_name` method, which is more convenient for the view layer.

Instance Method Summary collapse

Instance Method Details

#corporationObject

Returns the (single) Corporation the user is associated with. If in your domain, a User is member of several corporations, use the ‘corporations` method instead.



26
27
28
# File 'app/models/concerns/user_corporations.rb', line 26

def corporation
  cached { Corporation.find corporation_id if corporation_id }
end

#corporation_idObject



18
19
20
# File 'app/models/concerns/user_corporations.rb', line 18

def corporation_id
  (Corporation.pluck(:id) & self.ancestor_group_ids).first
end

#corporation_nameObject

Returns the name of the Corporation the user is associated with.



32
33
34
# File 'app/models/concerns/user_corporations.rb', line 32

def corporation_name
  corporation.try(:name)
end

#corporation_name=(new_corporation_name) ⇒ Object

Sets the name of the Corporation the user is associated with. If no matching corporation exists, the corporation is created. The user is added as member to this corporation.



40
41
42
# File 'app/models/concerns/user_corporations.rb', line 40

def corporation_name=(new_corporation_name)
  Corporation.find_or_create_by(name: new_corporation_name).assign_user self
end