Sha256: c191b640a8ac39de6e4d1ed09ade23689f66ed42e3b5b8bdf8a962ecd952f884

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

module OpenConferenceWare
  # SnippetsHelper retrieves Snippet records and contents for views.
  module SnippetsHelper
    # Return the Snippet record matching the +slug+, else raise a
    # ActiveRecord::RecordNotFound.
    def snippet_record_for(slug)
      if record = Snippet.find_by_slug(slug.to_s)
        return record
      else
        raise ActiveRecord::RecordNotFound, "Can't find snippet: #{slug}"
      end
    end

    # Return the raw snippet content for a +slug+, else raises a
    # ActiveRecord::RecordNotFound.
    def raw_snippet_for(slug)
      snippet_record_for(slug).content
    end

    # Returns the snippet content adorned with an "Edit" link if the user is an
    # admin, the raw content if the user is a mortal, else raises a
    # ActiveRecord::RecordNotFound.
    def snippet_for(slug, use_simple_format=true)
      record = snippet_record_for(slug)
      string = record.content
      if admin?
        if matcher = string.match(%r{(.+)(</(?:div|p)>)\s*$}s)
          string = matcher[1] + link_to("Edit", edit_manage_snippet_path(record), class: :snippet_edit_link) + matcher[2]
        else
          string = string + link_to("Edit", edit_manage_snippet_path(record), class: :snippet_edit_link)
        end
      end
      return use_simple_format ? simple_format(string) : string
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
open_conference_ware-1.0.0.pre4 app/helpers/open_conference_ware/snippets_helper.rb
open_conference_ware-1.0.0.pre3 app/helpers/open_conference_ware/snippets_helper.rb