Sha256: eb5ff36e37f387d878d741ce352cb1362a1f59253eb149a5304d839813eec882

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# 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.
#
module UserCorporations
  extend ActiveSupport::Concern
  
  included do
    attr_accessible :corporation_name if defined? attr_accessible
  end
    
  def corporation_id
    (Corporation.pluck(:id) & self.ancestor_group_ids).first
  end
  
  # 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.
  #
  def corporation
    cached { Corporation.find corporation_id if corporation_id }
  end
  
  # Returns the name of the Corporation the user is associated with.
  #
  def corporation_name
    corporation.try(:name)
  end
  
  # 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.
  #
  def corporation_name=(new_corporation_name)
    Corporation.find_or_create_by_name(new_corporation_name).assign_user self
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
your_platform-1.0.0 app/models/concerns/user_corporations.rb