Class: Cms::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
app/helpers/cms/form_builder.rb

Overview

Adds additional form fields to the Rails FormBuilder which can be used to create CMS forms.

Instance Method Summary collapse

Instance Method Details

#cms_attachment_managerObject

Renders a multiple file uploader for attachments. Allows users to add as many attachments to this model as needed.



83
84
85
86
87
88
89
90
# File 'app/helpers/cms/form_builder.rb', line 83

def cms_attachment_manager
  defs = Cms::Attachment.definitions_for(object.class.name, :multiple)
  names = defs.keys.sort
  return if names.empty?

  names.unshift "Select a type to upload a file" if names.size > 1
  render_cms_form_partial :attachment_manager, :asset_definitions => defs, :asset_types => names
end

#cms_check_box(method, options = {}) ⇒ Object

Renders a label and checkbox suitable for allow editors to update a boolean field.

Params:

* method - The name of the field this check_box will update.
* options - Hash of values including:
    - :label
    - :instructions
    - :default_value
    - Any other standard FormBuilder.check_box options that will be passed directly to the check_box method.


154
155
156
157
158
159
# File 'app/helpers/cms/form_builder.rb', line 154

def cms_check_box(method, options={})
  add_tabindex!(options)
  set_default_value!(method, options)
  cms_options = options.extract_only!(:label, :instructions, :default_value)
  render_cms_form_partial "check_box", :method => method, :options => options, :cms_options => cms_options
end

#cms_drop_down(method, choices, options = {}, html_options = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'app/helpers/cms/form_builder.rb', line 94

def cms_drop_down(method, choices, options={}, html_options={})
  add_tabindex!(html_options)
  set_default_value!(method, options)
  cms_options = options.extract_only!(:label, :instructions, :default_value)
  render_cms_form_partial :drop_down,
                          :object_name => @object_name, :method => method,
                          :choices => choices, :options => options,
                          :cms_options => cms_options, :html_options => html_options
end

#cms_error_messagesObject

Basic replacement for the error_messages provided by Rails 2, which were deprecated/removed in Rails 3.



191
192
193
194
195
196
197
198
199
200
# File 'app/helpers/cms/form_builder.rb', line 191

def cms_error_messages
  return unless object.respond_to?(:errors) && object.errors.any?

  errors_list = ""
  errors_list << @template.(:h2, "#{object.errors.size} error prohibited this #{object_name.humanize} from being saved.".html_safe)
  errors_list << @template.(:p, "There were problems with the following fields:")
  errors_list << @template.(:ul, object.errors.full_messages.map { |message| @template.(:li, message).html_safe }.join("\n").html_safe).html_safe

  @template.(:div, errors_list.html_safe, :class => "errorExplanation", :id => "errorExplanation")
end

#cms_file_field(method, options = {}) ⇒ Object

Returns a file upload input tag for a given block, along with label and instructions.

Parameters:

  • method (Symbol)

    The name of the model this form upload is associated with

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

Options Hash (options):

  • :label (String) — default: method

    If no label is specified, the human readable name for method will be used.

  • :instructions (String) — default: blank

    Helpful tips for the person entering the field, appears blank if nothing is specified.

  • :edit_path (Boolean) — default: false

    If true, render a text field to allow users to edit path for this file.

  • :edit_section (Boolean) — default: false

    If true, render a select box which allows users to choose which section this attachment should be placed in.



73
74
75
76
77
78
79
80
# File 'app/helpers/cms/form_builder.rb', line 73

def cms_file_field(method, options={})
  @object.ensure_attachment_exists if @object.respond_to?(:ensure_attachment_exists)

  unless options[:label]
    options[:label] = method.to_s.humanize
  end
  render_form_field("file_field", method, options)
end

#cms_instructions(instructions) ⇒ Object

Renders instructions for a given field below the field itself. Instructions can be used to provide helpful guidance to content editors including formatting help or just explaining what a field is for.

If instructions are blank/nil, then nothing will be shown.

Parameters:

  • instructions (String)

    (blank) The help text to show



141
142
143
# File 'app/helpers/cms/form_builder.rb', line 141

def cms_instructions(instructions)
  render_cms_form_partial :instructions, :instructions => instructions
end

#cms_label(field, label_value) ⇒ Object

Returns a label for a given field

Parameters:

  • field (Symbol)

    Name of the field

  • label_value (String)

    If nil, will use default logic for Rails::FormBuilder#label



57
58
59
60
61
62
63
# File 'app/helpers/cms/form_builder.rb', line 57

def cms_label(field, label_value)
  if label_value
    label field, label_value
  else
    label field
  end
end

#cms_tag_list(options = {}) ⇒ Object



104
105
106
107
108
109
110
# File 'app/helpers/cms/form_builder.rb', line 104

def cms_tag_list(options={})
  add_tabindex!(options)
  set_default_value!(:tag_list, options)
  cms_options = options.extract_only!(:label, :instructions, :default_value)
  render_cms_form_partial :tag_list,
                          :options => options, :cms_options => cms_options
end

#cms_template_editor(method, options = {}) ⇒ Object

Renders a template editor that allows developers to edit the view used to render a specific block. Render both a 'Handler' select box (erb, builder, etc) and a text_area for editing. Will not display the editor if the underlying object is marked as 'render_inline(false)'. This allows developers to edit the render.html.erb directly to update how the model displays.

For example, Portlets will often specify a :template to allow runtime update of their view.

Options:

:default_handler - Which handler will be the default when creating new instances. (Defaults to erb)
:instructions - Instructions that will be displayed below the text area. (Blank by default)
:label - The name for the label (Defaults to humanized version of field name)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/helpers/cms/form_builder.rb', line 174

def cms_template_editor(method, options={})
  if object.class.render_inline

    # Set some defaults
    options[:default_value] = @object.class.default_template
    set_default_value!(method, options)
    options[:default_handler] = "erb" unless options[:default_handler]

    cms_options = options.extract_only!(:label, :instructions)
    dropdown_options = options.extract_only!(:default_handler)
    add_tabindex!(options)
    render_cms_form_partial :template_editor, :method => method, :dropdown_options => dropdown_options, :options => options, :cms_options => cms_options
  end
end

#cms_text_editor(method, options = {}) ⇒ Object

Renders a WYWIWYG editor with the 'type' selector.



124
125
126
127
128
129
130
131
132
133
# File 'app/helpers/cms/form_builder.rb', line 124

def cms_text_editor(method, options = {})
  add_tabindex!(options)
  set_default_value!(method, options)
  cms_options = options.extract_only!(:label, :instructions, :default_value)
  render_cms_form_partial :text_editor,
                          :id => (options[:id] || "#{@object_name}_#{method}"),
                          :editor_enabled => (cookies["editorEnabled"].blank? ? true : (cookies["editorEnabled"] == 'true' || cookies["editorEnabled"] == ['true'])),
                          :object_name => @object_name, :method => method,
                          :options => options, :cms_options => cms_options
end

#date_picker(method, options = {}) ⇒ Object



28
29
30
# File 'app/helpers/cms/form_builder.rb', line 28

def date_picker(method, options={})
  text_field(method, {:size => 10, :class => "date_picker", :value => Cms::DatePicker.format_for_ui(@object.send(method))}.merge(options))
end

Renders a CMS styled JavaScript/CSS styled select box, by itself with no label or other markup besides the js.

Options:

* All standard select tag options plus:
* :default_value - The default item to have selected (defaults to the value of the underlying model)
* :width - The width for the select (defaults to 455px).


14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/helpers/cms/form_builder.rb', line 14

def drop_down(method, choices, options = {}, html_options = {})
  select_class = "#{@object_name}_#{method}"
  h_opts = add_tabindex!(@default_options.merge(html_options))
  h_opts[:class] = select_class

  opts = objectify_options(options)
  set_default_value!(method, options)
  cms_options = options.extract_only!(:default_value, :width)
  render_cms_form_partial :fancy_drop_down,
                          :object_name => @object_name, :method => method,
                          :choices => choices, :options => opts,
                          :cms_options => cms_options, :html_options => h_opts
end

#tag_list(options = {}) ⇒ Object



32
33
34
35
# File 'app/helpers/cms/form_builder.rb', line 32

def tag_list(options={})
  field_name = options.delete(:name) || :tag_list
  text_field(field_name, {:size => 50, :class => "tag-list"}.merge(options))
end

#text_editor(method, options = {}) ⇒ Object

Renders a WYWIWYG editor without the 'type' selector.



115
116
117
118
119
120
121
# File 'app/helpers/cms/form_builder.rb', line 115

def text_editor(method, options = {})
  @template.send(
      "text_editor",
      @object_name,
      method,
      objectify_options(options))
end