Sha256: 639ff8af3dfe70d18ae004e72e11399614f73abfd7ef5a2488dc087dce9c7624

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

require 'hanami/utils/string'

module Hanami
  module Helpers
    # Routing helper for full stack Hanami web applications.
    #
    # For a given application called <tt>Web::Application</tt>, at runtime
    # Hanami creates a routes factory called <tt>Web.routes</tt>.
    #
    # By including this module in a view, it makes that factory avaliable as
    # <tt>routes</tt>.
    #
    # @since 0.1.0
    #
    # @example Basic usage in template
    #   require 'hanami'
    #
    #   module Web::Views::Home
    #     class Index
    #       include Web::View
    #     end
    #   end
    #
    #   # ERB template
    #   # <%= routes.home_path %>
    #
    # @example Basic usage in view
    #   require 'hanami'
    #
    #   module Web::Views::Home
    #     class Index
    #       include Web::View
    #
    #       def link_to_home
    #         %(<a href="#{ routes.home_path }">Home</a>)
    #       end
    #     end
    #   end
    #
    #   # ERB template
    #   # <%= link_to_home %>
    module RoutingHelper
      def self.included(base)
        factory = "#{Utils::String.new(base).namespace}.routes"

        base.class_eval <<-END_EVAL, __FILE__, __LINE__
          def routes
            #{factory}
          end
        END_EVAL
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hanami-helpers-1.0.0.beta1 lib/hanami/helpers/routing_helper.rb
hanami-helpers-0.5.1 lib/hanami/helpers/routing_helper.rb
hanami-helpers-0.5.0 lib/hanami/helpers/routing_helper.rb