Sha256: dd7fd2cce4d95d4150422f4c07213ad0c8cd20064573dfce578ec377b0158f5c

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.lookup(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.pre2 app/helpers/open_conference_ware/snippets_helper.rb
open_conference_ware-1.0.0.pre1 app/helpers/open_conference_ware/snippets_helper.rb