Class: TwitterFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::FormTagHelper
Defined in:
app/helpers/twitter_form_builder.rb

Constant Summary collapse

COLS_CLASS =
'col-md-8'

Instance Method Summary collapse

Instance Method Details

#apply(text = I18n.t('admin.common.apply')) ⇒ Object



91
92
93
# File 'app/helpers/twitter_form_builder.rb', line 91

def apply text = I18n.t('admin.common.apply')
  submit text, name: :apply, class: 'btn btn-default wymupdate'
end

#cancelObject



95
96
97
# File 'app/helpers/twitter_form_builder.rb', line 95

def cancel
  @template.back_action
end

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



57
58
59
# File 'app/helpers/twitter_form_builder.rb', line 57

def checkbox method, options = {}
  row method, (:div, check_box(method, options), class: 'checkbox'), options
end

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



65
66
67
# File 'app/helpers/twitter_form_builder.rb', line 65

def ckeditor(method, options = {})
  row method, cktext_area(method, with_default_options(options))
end

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



36
37
38
# File 'app/helpers/twitter_form_builder.rb', line 36

def combobox method, choices, options = {}, html_options = {}
  row method, select(method, choices, options, with_default_options(html_options)), div_class: options.delete(:div_class) || 'col-md-6'
end

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



73
74
75
# File 'app/helpers/twitter_form_builder.rb', line 73

def date method, options = {}
  string(method, merge_options({data: {'calendar-date' => true}, div_class: 'col-md-2'}, options))
end

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



77
78
79
80
# File 'app/helpers/twitter_form_builder.rb', line 77

def datetime(method, options = {})
  value = object.try(method).try(:strftime, '%Y-%m-%d %H:%M')
  string(method, merge_options({data: {'calendar-datetime' => true}, div_class: 'col-md-3', value: value}, options))
end

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



40
41
42
# File 'app/helpers/twitter_form_builder.rb', line 40

def enum(method, options = {}, html_options = {})
  combobox method, object.class.send(method.to_s.pluralize).collect { |s| [s[0].humanize, s[0]] }, options, html_options
end

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



44
45
46
# File 'app/helpers/twitter_form_builder.rb', line 44

def enumerize(method, options = {}, html_options = {})
  combobox method, object.class.send(method).values.collect { |s| [s.text, s] }, options, html_options
end

#errorsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/helpers/twitter_form_builder.rb', line 7

def errors
  return nil unless @object.errors.any?

  messages = @object.errors.full_messages.collect do |msg|
    "<p>#{ERB::Util.h(msg)}</p>".html_safe
  end

  (:div,
              (:div,
                          (:div,
                                      (:button, '&times;'.html_safe, class: 'close', type: 'button', :'data-dismiss' => 'alert') + messages.join('').html_safe,
                                      class: 'alert alert-danger'),
                          class: 'col-md-8 col-md-offset-2'),
              class: 'row')
end

#file_upload(method) ⇒ Object



138
139
140
141
142
143
# File 'app/helpers/twitter_form_builder.rb', line 138

def file_upload method
  @template.render partial: '/admin/common/file_upload_template', locals: {
      f: self,
      field: method
  }
end

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/helpers/twitter_form_builder.rb', line 107

def image_upload method, options = {}
  version = options[:version]

  uri = if version.present?
          @object.send("#{method}_url", version)
        else
          @object.send("#{method}_url")
        end

  if options[:size]
    url = uri || 'placeholder'
  else
    url = uri
  end

  upload_uri = options[:upload_path]
  unless upload_uri
    upload_uri = @template.admin_upload_image_path(image_class: @object.class.name.underscore, field: method)
  end
  @template.render partial: '/admin/common/image_upload_template', locals: {
      f: self, url: url, size: options[:size],
      version: version || '',
      title: options[:title] || '',
      text: options[:text],
      field: method, upload_uri: upload_uri,
      container: options[:parent_id] || upload_container_id(method),
      options: options.delete(:options) || {},
      image_style: options.delete(:image_style) || ''
  }
end

#list_all_combobox(association_name, options = {}, html_options = {}) ⇒ Object



48
49
50
51
# File 'app/helpers/twitter_form_builder.rb', line 48

def list_all_combobox(association_name, options = {}, html_options = {})
  collection_class = object.class.reflect_on_association(association_name).klass
  combobox("#{association_name}_id", collection_class.all.sort_by(&:name).map { |element| [element.name, element.id] }, {include_blank: true}.merge(options), html_options)
end

#multi_field(relation_name, options = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/helpers/twitter_form_builder.rb', line 145

def multi_field(relation_name, options = {})
  reflection = self.object.class.reflections[relation_name] || self.object.class.reflections[relation_name.to_s]
  options = {search_field_name: :name,
             show_all_on_focus: false,
             use_cache: true,
             relation_model_name: reflection.klass.model_name.singular}.merge!(options)
  options[:source] ||= @template.admin_autocomplete_path(options[:relation_model_name], options[:search_field_name])
  string "#{relation_name}_tokens", title: self.object.class.human_attribute_name(relation_name),
         data: {
             multi_field: true,
             source: options[:source],
             pre: self.object.send("#{relation_name}_json", options[:search_field_name]),
             show_all_on_focus: options[:show_all_on_focus],
             use_cache: options[:use_cache],
             object_url_name: options[:object_url_name]}
end

#nested_fields_for(association) ⇒ Object

Editing has_many collection parameters: association - name of has_many association association should be defined as accepts_nested_attributes with allow_destroy = true in model You should create partial with name #{association)_fields



103
104
105
# File 'app/helpers/twitter_form_builder.rb', line 103

def nested_fields_for(association)
  @template.render 'admin/common/nested_fields_for', f: self, section_name: @object.class.human_attribute_name(association), collection: association
end

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



69
70
71
# File 'app/helpers/twitter_form_builder.rb', line 69

def password method, options = {}
  row method, password_field(method, with_default_options(options)), options
end

#row(method, controls, options = {}) ⇒ Object



162
163
164
165
166
167
168
# File 'app/helpers/twitter_form_builder.rb', line 162

def row(method, controls, options = {})
  label_class = options[:label_class] || 'control-label col-md-4 col-lg-2'
  label_tag = label(method, options[:title], class: label_class) unless options[:hide_label]
  options[:div_class] ||= COLS_CLASS
  div_tag =  :div, controls.html_safe, class: options[:div_class]
   :div, "#{label_tag}#{div_tag}".html_safe, class: 'form-group'
end

#save(text = I18n.t('admin.common.save')) ⇒ Object



87
88
89
# File 'app/helpers/twitter_form_builder.rb', line 87

def save text = I18n.t('admin.common.save')
  submit text, name: :save, class: 'btn btn-success wymupdate'
end

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

Displays multi-line static text in form



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

def static_memo_text(method, options = {})
  row method, simple_format(@object.send(method), class: 'form-control-static'), options
end

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



23
24
25
# File 'app/helpers/twitter_form_builder.rb', line 23

def static_text(method, options = {})
  row method, (:p, static_text_formatter.format(method), class: 'form-control-static'), options
end

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



32
33
34
# File 'app/helpers/twitter_form_builder.rb', line 32

def string method, options = {}
  row method, text_field(method, with_default_options(options)), options
end

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



53
54
55
# File 'app/helpers/twitter_form_builder.rb', line 53

def textarea method, options = {}
  row method, text_area(method, with_default_options(options)), options
end

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



82
83
84
85
# File 'app/helpers/twitter_form_builder.rb', line 82

def time(method, options = {})
  value = object.try(method).try(:strftime, '%H:%M')
  string(method, merge_options({data: {'calendar-time' => true}, div_class: 'col-md-2', value: value}, options))
end

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



61
62
63
# File 'app/helpers/twitter_form_builder.rb', line 61

def wysiwyg method, options = {}
  textarea method, with_default_options(merge_classes(options, 'wymeditor'))
end