Class: LucyCms::SiteFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
lib/LucyCMS/site_form_builder.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Capturing all calls of cms_tag_* type. For those we’ll try to render a form element. Everything else can trigger MethodNotFound error.



121
122
123
124
125
126
127
# File 'lib/LucyCMS/site_form_builder.rb', line 121

def method_missing(method_name, *args)
  if m = method_name.to_s.match(/^cms_tag_(\w+)$/)
    send(m[1], *args) if respond_to?(m[1])
  else
    super
  end
end

Instance Method Details

#default_field(type, field, options = {}, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/LucyCMS/site_form_builder.rb', line 18

def default_field(type, field, options = {}, &block)
  errors = if object.errors[field].present?
    "<div class='errors'>#{[object.errors[field]].flatten.first}</div>"
  end
  if desc = options.delete(:desc)
    desc = "<div class='desc'>#{desc}</div>"
  end
  %(
    <div class='form_element #{type}_element #{'errors' if errors}'>
      <div class='site_label'>#{label_for(field, options)}</div>
      <div class='site_value'>#{yield}</div>
      #{desc}
      #{errors}
    </div>
  ).html_safe
end

#default_tag_field(tag, options = {}) ⇒ Object

– Tag Field Fields —————————————————–



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/LucyCMS/site_form_builder.rb', line 57

def default_tag_field(tag, options = {})
  label     = options[:label] || tag.label.to_s.titleize
  css_class = options[:css_class] || tag.class.name.underscore.downcase.idify
  
  field_css_class = case tag
    when CmsTag::PageRichText                 then 'rich_text'
    when CmsTag::PageText, CmsTag::FieldText  then 'code'
  end
  
  options[:content_field_method] ||= :text_field_tag
  field = 
    options[:field] || 
    @template.send(options[:content_field_method], 'cms_page[cms_blocks_attributes][][content]', tag.content, :id => nil, :class => field_css_class)
  
  %(
    <div class='form_element #{css_class}'>
      <div class='site_label'>#{label}</div>
      <div class='site_value'>
        #{field}
        #{@template.hidden_field_tag('cms_page[cms_blocks_attributes][][label]', tag.label, :id => nil)}
        #{@template.hidden_field_tag('cms_page[cms_blocks_attributes][][id]', tag.record_id, :id => nil) if tag.record_id}
      </div>
    </div>
  ).html_safe
end

#field_date_time(tag) ⇒ Object



83
84
85
# File 'lib/LucyCMS/site_form_builder.rb', line 83

def field_date_time(tag)
  default_tag_field(tag, :content_field_method => :datetime_field_tag)
end

#field_integer(tag) ⇒ Object



87
88
89
# File 'lib/LucyCMS/site_form_builder.rb', line 87

def field_integer(tag)
  default_tag_field(tag, :content_field_method => :number_field_tag)
end

#field_string(tag) ⇒ Object



91
92
93
# File 'lib/LucyCMS/site_form_builder.rb', line 91

def field_string(tag)
  default_tag_field(tag)
end

#field_text(tag) ⇒ Object



95
96
97
# File 'lib/LucyCMS/site_form_builder.rb', line 95

def field_text(tag)
  default_tag_field(tag, :content_field_method => :text_area_tag)
end

#label_for(field, options) ⇒ Object



45
46
47
48
# File 'lib/LucyCMS/site_form_builder.rb', line 45

def label_for(field, options)
  label = options.delete(:label) || field.to_s.titleize.capitalize_all
  "<label for=\"#{object_name}_#{field}\">#{label}</label>".html_safe
end

#page_date_time(tag) ⇒ Object



99
100
101
# File 'lib/LucyCMS/site_form_builder.rb', line 99

def page_date_time(tag)
  default_tag_field(tag, :content_field_method => :datetime_field_tag)
end

#page_integer(tag) ⇒ Object



103
104
105
# File 'lib/LucyCMS/site_form_builder.rb', line 103

def page_integer(tag)
  default_tag_field(tag, :content_field_method => :number_field_tag)
end

#page_rich_text(tag) ⇒ Object



115
116
117
# File 'lib/LucyCMS/site_form_builder.rb', line 115

def page_rich_text(tag)
  default_tag_field(tag, :content_field_method => :text_area_tag)
end

#page_string(tag) ⇒ Object



107
108
109
# File 'lib/LucyCMS/site_form_builder.rb', line 107

def page_string(tag)
  default_tag_field(tag)
end

#page_text(tag) ⇒ Object



111
112
113
# File 'lib/LucyCMS/site_form_builder.rb', line 111

def page_text(tag)
  default_tag_field(tag, :content_field_method => :text_area_tag)
end

#simple_field(label = nil, content = nil, options = {}, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/LucyCMS/site_form_builder.rb', line 35

def simple_field(label = nil, content = nil, options = {}, &block)
  content ||= @template.capture(&block) if block_given?
  %(
    <div class='form_element #{options.delete(:class)}'>
      <div class='site_label'>#{label}</div>
      <div class='site_value'>#{content}</div>
    </div>
  ).html_safe
end

#submit(value, options = {}, &block) ⇒ Object



50
51
52
53
54
# File 'lib/LucyCMS/site_form_builder.rb', line 50

def submit(value, options = {}, &block)
  return super if options.delete(:disable_builder)
  extra_content = @template.capture(&block) if block_given?
  simple_field(nil, "#{super(value, options)} #{extra_content}", :class => 'submit_element')
end