Sha256: 0ea6c5c2f60ac20f2972f4b678047c3bcbbcb5f7a97dc26289538b93c1e25315

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module CanCanDryHelper
  def ability_mapping
    @ability_mapping ||= ::AbilityMapping.new # rubocop:disable Rails/HelperInstanceVariable
  end

  def can_by_path?(path, method = :get)
    can_by_can_args(ability_mapping.can_args_by_path(main_app.root_path, path, method))
  end

  def can_by_path_hash?(path_hash)
    can_by_can_args(ability_mapping.can_args_by_path_hash(path_hash))
  end

  def link_or_text(name = nil, options = nil, html_options = nil)
    link_or_default(name, name, options, html_options)
  end

  def link_or_nil(name = nil, options = nil, html_options = nil)
    link_or_default(name, nil, options, html_options)
  end

  private

  def link_or_default(name, default, options, html_options)
    if can_by_link_options?(options, html_options)
      link_to(name, options, html_options)
    else
      default
    end
  end

  def can_by_link_options?(options, html_options)
    can_by_path?(url_for(options), link_method(options, html_options))
  end

  def link_method(*hashs)
    hashs.each do |h|
      return h[:method] if h.is_a?(Hash) && h[:method]
    end
    :get
  end

  def can_by_can_args(can_args_args)
    assert_can_method
    can_args_args.each do |c|
      next if c.empty?
      return true if can?(*c)
    end
    false
  end

  def assert_can_method
    return if respond_to?('can?')

    singleton_class.include(::CanCanDry::NoControllerCanCanAdditions)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
can_can_dry-0.5.5 app/helpers/can_can_dry_helper.rb
can_can_dry-0.5.4 app/helpers/can_can_dry_helper.rb
can_can_dry-0.5.3 app/helpers/can_can_dry_helper.rb
can_can_dry-0.5.2 app/helpers/can_can_dry_helper.rb
can_can_dry-0.5.1 app/helpers/can_can_dry_helper.rb
can_can_dry-0.5.0 app/helpers/can_can_dry_helper.rb