Sha256: 4426c882a76e77ac4ac0962bb0513383e254d9b67d855881a806ae844d68a911

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

module Sinatra
  module DarkHelpers
    module HelperMethods
      def view(view, options = {})
        html = haml(view,options)
        flash.clear
        html
      end

      def partial(template, options = {})
        options.merge!(:layout => false)
        if collection = options.delete(:collection) then
          collection.inject([]) do |buffer, member|
            buffer << haml(template, options.merge(:layout => false,
                                                   :locals => {template.to_sym => member}))
          end.join("\n")
        else
          haml(template, options)
        end
      end

      def css_link_tag(sheet, media = 'screen,projection')
        partial("%link{ :type => 'text/css', :href => '#{options.slash ? '/' : ''}stylesheets/#{sheet}.css', :rel => 'stylesheet', :media => '#{media}' }")
      end

      def js_script_tag(script)
        partial("%script{ :type => 'text/javascript', :src => '#{options.slash ? '/' : ''}javascripts/#{script}.js' }")
      end

      def js_tag(script)
        @dsh_script = script
        temp = ["%script{ :type => 'text/javascript' }",
                "  //<![CDATA[",
                "  = @dsh_script",
                "  //]]>"].join("\n")
        partial(temp)
      end

      def flash
        session[:flash] = {} if session[:flash] && session[:flash].class != Hash
        session[:flash] ||= {}
      end

      def host
        port = request.env['SERVER_PORT'].to_i
        port = port == 80 ? "" : ":#{port}"
        "#{protocol}://#{server_name}#{port}"
      end

      def protocol
        request.env['rack.url_scheme']
      end

      def server_name
        request.env['SERVER_NAME']
      end

      def set_title(extra)
        @title = "#{options.site_name} - #{extra}"
      end

      def base
        host + options.site_base
      end
    end

    def self.registered(app)
      app.helpers HelperMethods
      app.disable(:slash)
      app.enable(:sessions)
    end
  end

  register DarkHelpers
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
darkhelmet-darkext-0.9.0 lib/darkext/sinatra.rb