class Strongbolt::Capability

Constants

Actions
DEFAULT_MODELS

Attributes

models[W]

Public Class Methods

actions_from_list(actions) click to toggle source

Virtual setter of actions

# File lib/strongbolt/capability.rb, line 131
def self.actions_from_list(actions)
  # Transform actions array
  if actions.respond_to?(:to_sym) && actions.to_sym == :all
    Actions # All actions
  else
    [*actions] # Transform into an array
  end
end
add_models(models) click to toggle source
# File lib/strongbolt/capability.rb, line 38
def self.add_models(models)
  @models ||= DEFAULT_MODELS
  @models |= [*models]
  @models.sort!
end
from_hash(hash) click to toggle source

Create a set capabilities from a hash which has: {

model: "ModelName",
require_ownership: true,
require_tenant_access: false,
actions: [:find, :update]}

Actions can be either one operation, an array of operations, or :all meaning all operations

# File lib/strongbolt/capability.rb, line 118
def self.from_hash(hash)
  hash.symbolize_keys!
  actions_from_list(hash[:actions]).map do |action|
    new model: hash[:model],
        require_ownership: hash[:require_ownership],
        require_tenant_access: hash[:require_tenant_access],
        action: action
  end
end
models() click to toggle source

List all the models to be used in capabilities

# File lib/strongbolt/capability.rb, line 30
def self.models
  @models ||= DEFAULT_MODELS
end
to_hash() click to toggle source

Group by model, ownership and tenant access and tells whether each action is set or not in a hash

# File lib/strongbolt/capability.rb, line 86
def self.to_hash
  hash = {}
  all.ordered.each do |capability|
    key = {
      model: capability.model,
      require_ownership: capability.require_ownership,
      require_tenant_access: capability.require_tenant_access
    }

    hash[key] ||= {
      find: false,
      create: false,
      update: false,
      destroy: false
    }

    hash[key][capability.action.to_sym] = true
  end
  hash
end
to_table() click to toggle source

Group by model, ownership and tenant access and tells whether each action is set or not

# File lib/strongbolt/capability.rb, line 57
def self.to_table
  table = []
  all.ordered.each do |capability|
    if table.last.nil? ||
       !(table.last[:model] == capability.model &&
         table.last[:require_ownership] == capability.require_ownership &&
         table.last[:require_tenant_access] == capability.require_tenant_access)

      table << {
        model: capability.model,
        require_ownership: capability.require_ownership,
        require_tenant_access: capability.require_tenant_access,
        find: false,
        create: false,
        update: false,
        destroy: false
      }
    end

    table.last[capability.action.to_sym] = true
  end
  table
end

Private Instance Methods

model_exists?() click to toggle source

Checks that the model given as a string exists

# File lib/strongbolt/capability.rb, line 145
def model_exists?
  if model.present?
    begin
      model.constantize
    rescue NameError
      errors.add :model, "#{model} is not a valid model"
    end
  end
end
set_default() click to toggle source

Default parameters

# File lib/strongbolt/capability.rb, line 158
def set_default
  self.require_ownership = true if require_ownership.nil?
  self.require_tenant_access = true if require_tenant_access.nil?
  true # Ensures it passes
end