Sha256: 198739e2dd1007f18b123539507155cb028a9a23c06a7bc31d8bfba7ab17a42e

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

class Navigasmic::Item

  attr_accessor :link
  def initialize(label, link, visible, options = {})
    @label, @link, @visible = label, link, visible
    @disabled = options.delete(:disabled_if)
    options.delete(:hidden_unless)

    @rules = calculate_highlighting_rules(options.delete(:highlights_on))
  end

  def hidden?
    !@visible
  end

  def disabled?
    @disabled
  end

  def link?
    @link.present? && !disabled?
  end

  def highlights_on?(path, params)
    return false unless @rules.any?
    params = clean_unwanted_keys(params)
    @rules.each do |rule|
      case rule
      when String
        return false unless path == rule
      when Regexp
        return false unless path.match(rule)
      when TrueClass
        # no-op
      when FalseClass
        return false
      when Hash
        clean_unwanted_keys(rule).each do |key, value|
          value.gsub(/^\//, '') if key == :controller
          return false unless value == params[key].to_s
        end
      else
        raise ArgumentError, 'Highlighting rules should be an array containing any of/or a Boolean, String, Regexp, Hash or Proc'
      end
    end
    true
  end

  private

  def calculate_highlighting_rules(rules)
    [].tap do |highlighting_rules|
      if rules.nil?
        highlighting_rules << @link if link?
      else
        highlighting_rules.concat Array(rules)
      end
    end
  end

  def clean_unwanted_keys(hash)
    ignored_keys = [:only_path, :use_route]
    hash.dup.delete_if { |key, value| ignored_keys.include?(key) }
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
navigasmic-1.0.5 lib/navigasmic/core/item.rb