Sha256: c00be7bb89e866c576ea4aefef5f7a9a851ebf53baf78c6e47fe748080a3fea0

Contents?: true

Size: 1.69 KB

Versions: 7

Compression:

Stored size: 1.69 KB

Contents

module Trestle
  class Navigation
    class Item
      attr_reader :name, :path, :options

      def initialize(name, path=nil, options={})
        @name, @path, @options = name, path, options
      end

      def ==(other)
        other.is_a?(self.class) && name == other.name && path == other.path
      end
      alias eql? ==

      def hash
        [name, path].hash
      end

      def <=>(other)
        priority <=> other.priority
      end

      def priority
        case options[:priority]
        when :first
          -Float::INFINITY
        when :last
          Float::INFINITY
        else
          options[:priority] || 0
        end
      end

      def group
        options[:group] || NullGroup.new
      end

      def admin
        options[:admin]
      end

      def label
        options[:label] || I18n.t("admin.navigation.items.#{name}", default: name.to_s.titlecase)
      end

      def icon
        options[:icon] || Trestle.config.default_navigation_icon
      end

      def badge?
        !!options[:badge]
      end

      def badge
        Badge.new(options[:badge]) if badge?
      end

      def visible?(context)
        if options[:if]
          context.instance_exec(&options[:if])
        elsif options[:unless]
          !context.instance_exec(&options[:unless])
        else
          true
        end
      end

      class Badge
        attr_reader :text

        def initialize(options)
          case options
          when Hash
            @html_class = options[:class]
            @text = options[:text]
          else
            @text = options
          end
        end

        def html_class
          @html_class || "label-primary"
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
trestle-0.8.13 lib/trestle/navigation/item.rb
trestle-0.8.12 lib/trestle/navigation/item.rb
trestle-0.8.11 lib/trestle/navigation/item.rb
trestle-0.8.10 lib/trestle/navigation/item.rb
trestle-0.8.9 lib/trestle/navigation/item.rb
trestle-0.8.8 lib/trestle/navigation/item.rb
trestle-0.8.7 lib/trestle/navigation/item.rb