Sha256: 7248e8633d2e71a2c24ffaf2f6fac7a5645caf5090b35058a1dbd469139a44e6

Contents?: true

Size: 1.29 KB

Versions: 9

Compression:

Stored size: 1.29 KB

Contents

module Calagator

module TagsHelper
  def tag_links_for(model)
    model.tags.sort_by(&:name).map do |tag|
      TagLink.new(model.class.table_name, tag, self).render
    end.join(', ').html_safe
  end

  class TagLink < Struct.new(:class_name, :tag, :context)
    def render
      context.link_to text, url, class: css_class
    end

    private

    def text
      icon = TagIcon.new(tag.name, context)
      [icon.exists? && icon.image_tag, context.escape_once(tag.name)].compact.join(' ').html_safe
    end

    def url
      machine_tag.url || "/#{class_name}/tag/#{tag.name}"
    end

    def css_class
      classes = ["p-category"]
      classes += ["external", machine_tag.namespace, machine_tag.predicate] if machine_tag.url
      classes.join(' ')
    end

    def machine_tag
      tag.machine_tag
    end
  end

  def display_tag_icons(event)
    event.tag_list.map do |tag_name|
      icon = TagIcon.new(tag_name, self)
      link_to(icon.image_tag, tag_events_path(tag_name)) if icon.exists?
    end.join(' ').html_safe
  end

  class TagIcon < Struct.new(:name, :context)
    def image_tag
      context.image_tag(image_path, title: name)
    end

    def exists?
      Rails.application.assets[image_path]
    end

    private

    def image_path
      "tag_icons/#{name}.png"
    end
  end
end

end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
grokus-1.0.0.9 app/helpers/calagator/tags_helper.rb
grokus-1.0.0.8 app/helpers/calagator/tags_helper.rb
grokus-1.0.0.7 app/helpers/calagator/tags_helper.rb
grokus-1.0.0.6 app/helpers/calagator/tags_helper.rb
grokus-1.0.0.5 app/helpers/calagator/tags_helper.rb
grokus-1.0.0.3 app/helpers/calagator/tags_helper.rb
grokus-1.0.0.2 app/helpers/calagator/tags_helper.rb
grokus-1.0.0.1 app/helpers/calagator/tags_helper.rb
calagator-1.0.0.rc3 app/helpers/calagator/tags_helper.rb