Sha256: 4a1838dc71eb85a4f2875fed6e10bddd3f4b016267782f9a52de07a47d5c0256

Contents?: true

Size: 1.95 KB

Versions: 24

Compression:

Stored size: 1.95 KB

Contents

module Rad::Html::BasicHtmlHelper
  BOOLEAN_ATTRIBUTES = %w(disabled readonly multiple checked autobuffer
                       autoplay controls loop selected hidden scoped async
                       defer reversed ismap seemless muted required
                       autofocus novalidate formnovalidate open).to_set
  BOOLEAN_ATTRIBUTES.merge(BOOLEAN_ATTRIBUTES.map {|attr| attr.to_sym })


  # 
  # JavaScript & StyleSheets
  #     
  def stylesheet_link_tag *stylesheets
    stylesheets.collect{|ssheet|
      tag :link, '', href: "#{ssheet}", media: "screen", rel: "stylesheet", type: "text/css"
    }.join("\n")
  end


  # 
  # Assets
  # 
  def image_tag src, opt = {}
    opt[:src] = src
    tag :img, '', opt
  end
        

  # 
  # Common HTML tags
  # 
  def label_tag name, text, options = {}
    tag :label, text, options.merge(for: name)
  end


  # 
  # Special
  # 
  def tag name, content_or_options_with_block = nil, options = nil, escape = true, &block      
    if block_given?
      options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
      content = capture(&block)
      concat "<#{name}#{tag_options(options)}>#{content}</#{name}>"
    else
      content = content_or_options_with_block
      "<#{name}#{tag_options(options)}>#{content}</#{name}>"
    end      
  end

  # Escape
  def h obj; obj.to_s.html_escape end    

  protected

    def tag_options options
      return "" unless options
      options.must_be.a Hash
      unless options.blank?
        attrs = []
        options.each_pair do |key, value|
          next if key == :content # used in common_interface don't delete it
          if BOOLEAN_ATTRIBUTES.include?(key)
            attrs << %(#{key}="#{key}") if value
          elsif !value.nil?
            final_value = value.is_a?(Array) ? value.join(" ") : value
            attrs << %(#{key}="#{final_value}")
          end
        end
        " #{attrs.sort * ' '}" unless attrs.empty?
      end
    end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
rad_core-0.2.6 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.2.5 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.2.4 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.2.3 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.2.2 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.2.1 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.2.0 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.0.30 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.0.29 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.0.28 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.0.27 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.0.26 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.0.25 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.0.24 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.0.23 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.0.22 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.0.21 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.0.20 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.0.19 lib/rad/html/_helpers/basic_html_helper.rb
rad_core-0.0.18 lib/rad/html/_helpers/basic_html_helper.rb