Module: FormHelper

Defined in:
lib/forge/app/helpers/form_helper.rb

Instance Method Summary collapse

Instance Method Details



81
82
83
# File 'lib/forge/app/helpers/form_helper.rb', line 81

def add_child_link(name, association)
  link_to(name, "#", :class => "add_child", :"data-association" => association)
end

#content_row(title, options = {}) ⇒ Object

form row helper for content blocks



45
46
47
48
49
# File 'lib/forge/app/helpers/form_helper.rb', line 45

def content_row(title, options = {})
  title = (:h3, title)
  explanation = options[:explanation].blank? ? "" : (:div, options[:explanation], :class => "explanation")
  return title.html_safe + explanation.html_safe + "<br />".html_safe + yield
end

#file_select_widget(f, method, options = {}) ⇒ Object

asset library helper for adding an image to a model



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/forge/app/helpers/form_helper.rb', line 62

def file_select_widget(f, method, options = {})
  unique_id = Time.now.to_i
  field = "#{f.object.class.to_s.downcase}_#{method}"
  thumbnail_url = f.object.send("#{method}_asset_url".to_sym).blank? ? f.object.send("#{method}_icon_path".to_sym) : f.object.send("#{method}_asset_url".to_sym)
  content = ""

  # Build the links
  links = [link_to("Upload From Computer", '#', :class => "upload-file"), link_to("Choose From Library", '#', :class => "select-asset")]
  links << remove_child_link("Remove", f) if options[:allow_destroy]
  links << (:label, check_box_tag("remove_asset") + "Remove this asset")  if options[:allow_remove]

  # Build the rest of the widget
  content << image_tag(thumbnail_url, :class => "form-thumbnail") unless thumbnail_url.blank? # If the form failed, show the thumbnail of the previously selected asset
  content << f.hidden_field("#{method}_asset_id".to_sym, :class => "file-widget-asset-id")  # Eg. avatar_asset_id
  content << f.hidden_field("#{method}_asset_url".to_sym, :class => "file-widget-asset-url") # Eg. avatar_asset_url
  content << (:small, (:div, "", :class => "spacer") + links.join('<br />').html_safe)
  return  :div, content.html_safe, :class => "extendable-inset"
end

#forge_form_for(*args, &block) ⇒ Object

form-header form row helper



3
4
5
6
7
# File 'lib/forge/app/helpers/form_helper.rb', line 3

def forge_form_for(*args, &block)
  options = args.extract_options!
  options.merge(:builder => ::ForgeFormBuilder)
  form_for(*(args + [options]), &block)
end

#form_row(label = nil, options = {}) ⇒ Object

form row helper for pop-up window forms



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/forge/app/helpers/form_helper.rb', line 26

def form_row(label = nil, options = {})
  explanation = options[:explanation].blank? ? nil : "<div class='explanation'>#{options[:explanation]}</div>"
  required = options[:required].blank? ? nil : "&nbsp; *"
  style = options[:style].blank? ? nil : options[:style]
  id = options[:id].blank? ? nil :  options[:id]

  file_link = nil
  unless options[:file].nil? || (options[:file].url && options[:file].url.blank?) || (options[:file].is_a?(ActiveRecord) && options[:file].new_record?)
    #check for a_fu or paperclip
    options[:file].url = options[:file].public_filename if options[:file].url.nil?
    file_link = '<br /><small><a href="' + options[:file].url + '" target="_">View Current</a></small>'
  end

  label_td = (:td, "#{label}#{required}#{file_link}".html_safe, :class => "label")
  control_td = (:td, yield + "#{explanation}", :class => "control")
  return (:tr, label_td.html_safe + control_td.html_safe, :id => id, :style => style).html_safe
end

#language_switcher(table) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/forge/app/helpers/form_helper.rb', line 101

def language_switcher(table)
  if Forge.config.languages
    if I18nLookup.fields[:tables][table]
      select_tag "current_language", options_for_select([["English", "en"]] + Forge.config.languages.map {|k, v| [k.to_s.titleize, v]})
    end
  end
end

#new_child_fields_template(form_builder, association, options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/forge/app/helpers/form_helper.rb', line 89

def new_child_fields_template(form_builder, association, options = {})
  options[:object] ||= form_builder.object.class.reflect_on_association(association).klass.new
  options[:partial] ||= association.to_s.singularize
  options[:form_builder_local] ||= :f

  (:div, :id => "#{association}_fields_template", :style => "display: none") do
    form_builder.fields_for(association, options[:object], :child_index => "new_#{association}") do |f|
      render(:partial => options[:partial], :locals => {options[:form_builder_local] => f})
    end
  end
end

#publish_box(f, object) ⇒ Object

form row helper for boolean published block



52
53
54
55
56
57
58
59
# File 'lib/forge/app/helpers/form_helper.rb', line 52

def publish_box(f, object)
  if can?(:publish, object)
    return (:h3, "Publish") +
    (:div, "Is your #{object.class.to_s.downcase} ready to appear on your website?", :class => "explanation") +
    slider("Yes", f.radio_button(:published, true), "Not Yet", f.radio_button(:published, false)) +
    "<hr />".html_safe
  end
end


85
86
87
# File 'lib/forge/app/helpers/form_helper.rb', line 85

def remove_child_link(name, f)
  f.hidden_field(:_destroy) + link_to(name, "javascript:void(0)", :class => "remove_child")
end

#side_row(title, options = {}) ⇒ Object

form row helper for narrow side column rows



17
18
19
20
21
22
23
# File 'lib/forge/app/helpers/form_helper.rb', line 17

def side_row(title, options = {})
  title = (:h3, title)
  explanation = options[:explanation].blank? ? "" : (:div, options[:explanation], :class => "explanation")
  content = title.html_safe + explanation.html_safe + yield
  content += "<hr />".html_safe unless options[:last]
  content
end

#title_row(options = {}) ⇒ Object



9
10
11
12
13
14
# File 'lib/forge/app/helpers/form_helper.rb', line 9

def title_row(options = {})
  title = (:h3, options[:title] ? options[:title] : "Title")
  explanation = options[:explanation] ? (:div, options[:explanation], :class => "explanation") : ""
  row_label = (:div, title.html_safe + explanation.html_safe, :class => "label")
  return row_label.html_safe + yield + (:div, "", :class => "spacer")
end