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_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.


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

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



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

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.



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

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_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.

Will not render if instructions are blank/nil.

  • instructions - The text of the instructions to show (Defaults to blank)



101
102
103
# File 'app/helpers/cms/form_builder.rb', line 101

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

#cms_tag_list(options = {}) ⇒ Object



64
65
66
67
68
69
70
# File 'app/helpers/cms/form_builder.rb', line 64

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)


134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/helpers/cms/form_builder.rb', line 134

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.



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

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.



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

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