Class: Sunrise::Views::FormBuilder

Inherits:
SimpleForm::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::JavaScriptHelper, ActionView::Helpers::TagHelper
Defined in:
lib/sunrise/views/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#attach_file_field(attribute_name, options = {}, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
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
82
83
84
85
# File 'lib/sunrise/views/form_builder.rb', line 47

def attach_file_field(attribute_name, options = {}, &block)
  value = options.delete(:value) if options.key?(:value)
  value ||= object.fileupload_asset(attribute_name)
         
  element_guid = object.fileupload_guid        
  element_id = dom_id(object, [attribute_name, element_guid].join('_'))
  script_options = (options.delete(:script) || {}).stringify_keys
  
  params = {
    :method => attribute_name, 
    :assetable_id => object.new_record? ? nil : object.id, 
    :assetable_type => object.class.name,
    :guid => element_guid
  }.merge(script_options.delete(:params) || {})
  
  script_options['action'] ||= '/sunrise/fileupload?' + Rack::Utils.build_query(params)
  script_options['allowedExtensions'] ||=  ['jpg', 'jpeg', 'png', 'gif']
  script_options['multiple'] ||= object.fileupload_multiple?(attribute_name)
  script_options['element'] ||= element_id
  
  label ||= if object && object.class.respond_to?(:human_attribute_name)
    object.class.human_attribute_name(attribute_name)
  end
  
  locals = {
    :element_id => element_id,
    :file_title => (options[:file_title] || "JPEG, GIF, PNG or TIFF"),
    :file_max_size => (options[:file_max_size] || 10),
    :label => (label || attribute_name.to_s.humanize),
    :object => object,
    :attribute_name => attribute_name,
    :assets => [value].flatten.delete_if { |v| v.nil? || v.new_record? },
    :script_options => script_options.inspect.gsub('=>', ':'),
    :multiple => script_options['multiple'],
    :asset_klass => params[:klass]
  }
  
  template.render(:partial => "manage/fileupload/container", :locals => locals)
end

#button(type, *args, &block) ⇒ Object



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

def button(type, *args, &block)
  options = args.extract_options!
  
  html = options[:input_html] || {}
  url = options[:url] || [:manage, object_plural]
  value = object.new_record? ? 'create' : 'update'
  title = html[:title] || I18n.t(value, :scope => :manage)
  
  html = {
    :value => value, :type => type, 
    :class => "gr cupid-green", :name => "commit"
  }.merge(html)
  
  (:div, :class => "buts controls") do
    concat (:button, title, html)
    concat link_to(I18n.t('manage.cancel'), url, :class => "erase")
  end
end

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



21
22
23
24
25
26
# File 'lib/sunrise/views/form_builder.rb', line 21

def globalize(options={}, &block)
  locales = options[:locales] || Sunrise.available_locales
  html = []
  
  html.join.html_safe
end

#input(attribute_name, options = {}, &block) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/sunrise/views/form_builder.rb', line 12

def input(attribute_name, options = {}, &block)
  options[:input_html] ||= {}
  options[:input_html] = { :class => 'text' }.merge(options[:input_html])
  
  attribute_name = "#{attribute_name}_#{options[:locale]}" unless options[:locale].blank?
  
  super(attribute_name, options, &block)
end