Module: Changeful::ViewHelpers

Includes:
ActionView::Helpers::CaptureHelper, ActionView::Helpers::OutputSafetyHelper
Defined in:
lib/changeful/helpers/view_helpers.rb

Overview

The Changeful ViewHelpers expose the methods to be used in view to make the static content editable

Instance Method Summary collapse

Instance Method Details

#changeful_content(key, options = {}) ⇒ String Also known as: cc

Returns a content based on a key and store in the database if it does not exist

Examples:

changeful_content(:about_us_title) 
changeful_content(:about_us_title, default: 'About Us')
changeful_content(:about_us_title, default: 'About Us', type: :plain) 

cc :about_us_title
cc :about_us_title, default: 'About Us'
cc :about_us_title, default: 'About Us', type: :plain

Parameters:

  • key (Symbol)

    A unique key in the view file to be referenced

  • options (Hash) (defaults to: {})

    Additional configuration options (refer below)

Options Hash (options):

  • :default (Symbol)

    It sets the default content to return if the key is not found in database

  • :type (Symbol)

    Specify the type of input to be displayed in the backend. Accepted types are :plain, :html

Returns:

  • (String)

    a content based on a key



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/changeful/helpers/view_helpers.rb', line 28

def changeful_content(key, options = {})
  contents ||= Content.all_contents_in(current_view)

  if contents.present?
    contents.each do |datum|
      return raw(datum.content) if datum.key == key.to_s
    end
  end

  content   = options.delete(:default) || capture(&proc)
  view_type = options.delete(:type)    || :plain
  Content.create!(key: key, 
                  content: content, 
                  view_type: view_type, 
                  file_path: current_view)

  return content
end