Sha256: 0701d832ce80878d77530ccd3e373be5fc5b977d3ed90b3954fa151d13c27225

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module Authorizy
  class Expander
    def expand(permissions)
      return [] if permissions.blank?

      result = {}

      permissions.each do |permission|
        controller = permission[0].to_s
        action = permission[1].to_s

        result["#{controller}##{action}"] = [controller, action]

        if (items = controller_dependency(controller, action))
          items.each do |controller_item, action_item|
            result["#{controller_item}##{action_item}"] = [controller_item, action_item]
          end
        end

        actions = [default_aliases[action]].flatten.compact

        next if actions.blank?

        actions.each do |action_item|
          result["#{controller}##{action_item}"] = [controller, action_item.to_s]
        end
      end

      result.values # TODO: garantir o uniq
    end

    private

    def aliases
      Authorizy.config.aliases.stringify_keys
    end

    def controller_dependency(controller, action)
      return if (actions = dependencies[controller]).blank?
      return if (permissions = actions[action]).blank?

      permissions.map { |c, a| [c.to_s, a.to_s] }
    end

    def default_aliases
      {
        'create' => 'new',
        'edit'   => 'update',
        'new'    => 'create',
        'update' => 'edit',
      }.merge(aliases)
    end

    def dependencies
      Authorizy.config.dependencies.deep_stringify_keys
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
authorizy-0.4.1 lib/authorizy/expander.rb
authorizy-0.4.0 lib/authorizy/expander.rb
authorizy-0.3.0 lib/authorizy/expander.rb
authorizy-0.2.2 lib/authorizy/expander.rb
authorizy-0.2.1 lib/authorizy/expander.rb
authorizy-0.2.0 lib/authorizy/expander.rb