Sha256: 11e146849f2a4c51fe3086735ef2645733579cee7c7813c25f44c302bbd76ef2

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

class UserDecorator < ApplicationDecorator
  def can?(scope, account)
    return true if object.can_manage?(account.organization)
    user_scopes = account_permissions(account).map(&:scope)
    user_scopes.include?('manage') || user_scopes.include?(scope)
  end

  def cached_organizations
    garner
      .bind(h.controller.session_token)
      .bind(h.controller.current_organization) do
        object.organizations
      end
  end

  def organization_count
    cached_organizations.count
  end

  def cached_roles
    garner.bind(h.controller.session_token) do
      object.roles
    end
  end

  def cached_permissions
    garner.bind(h.controller.session_token) do
      permissions = []
      cached_roles.each do |role|
        permissions += role.permissions
      end
      permissions
    end
  end

  def account_permissions(account)
    cached_permissions.select do |permission|
      permission.links[:account].href == account.href
    end
  end

  def current_training
    training_logs.first
  end

  def training_current?
    training_logs.any?
  end

  def training_logs
    training_criterion.documents.select do |doc|
      doc.links['user'].href == object.href
    end
  end

  def training_criterion
    context[:criterion]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aptible-rails-0.5.1 lib/aptible/rails/decorators/user_decorator.rb
aptible-rails-0.5.0 lib/aptible/rails/decorators/user_decorator.rb