Module: Locomotive::CustomFieldsHelper

Defined in:
app/helpers/locomotive/custom_fields_helper.rb

Instance Method Summary collapse

Instance Method Details

#_date_custom_field_options(field, icon, css, format) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 92

def _date_custom_field_options(field, icon, css, format)
  {
    name:         :"formatted_#{field.name}",
    append:       icon_tag("fa-#{icon}"),
    wrapper_html: { class: css },
    input_html:   {
      data:       { format: format },
      maxlength:  10
    }
  }
end

#belongs_to_custom_field_options(field, entry) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 66

def belongs_to_custom_field_options(field, entry)
  slug      = field.class_name_to_content_type.slug
  target_id = entry.send(:"#{field.name}_id")

  {
    as:       :document_picker,
    edit:     {
      label:  custom_field_t(:edit, field.type),
      url:    target_id ? edit_content_entry_path(current_site, slug, target_id, _location: false) : nil
    },
    picker:   custom_field_picker_options(field, slug)
  }
end

#boolean_custom_field_options(field, entry) ⇒ Object



80
81
82
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 80

def boolean_custom_field_options(field, entry)
  { as: :toggle }
end

#color_custom_field_options(field, entry) ⇒ Object



179
180
181
182
183
184
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 179

def color_custom_field_options(field, entry)
  {
    as: :color,
    wrapper_html: { class: 'color' }
  }
end

#custom_field_options(field, form) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 43

def custom_field_options(field, form)
  method = "#{field.type}_custom_field_options"

  begin
    send(method, field, form.object)
  rescue NoMethodError => e
    Rails.logger.error e.message
    nil
  end
end

#custom_field_picker_options(field, slug) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 215

def custom_field_picker_options(field, slug)
  {
    id:           slug,
    label_method: :_label,
    list_url:     content_entries_path(current_site, slug, format: :json),
    placeholder:  custom_field_t(:placeholder, field.type, name: field.label.downcase),
    searching:    custom_field_t(:searching, field.type),
    no_matches:   custom_field_t(:no_matches, field.type),
    too_short:    custom_field_t(:too_short, field.type)
  }
end

#custom_field_t(name, type, interpolation = {}) ⇒ Object



227
228
229
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 227

def custom_field_t(name, type, interpolation = {})
  t(name, { scope: ['locomotive.custom_fields.types', type] }.merge(interpolation))
end

#date_custom_field_options(field, entry) ⇒ Object



84
85
86
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 84

def date_custom_field_options(field, entry)
  _date_custom_field_options(field, 'calendar', 'date', date_moment_format)
end

#date_time_custom_field_options(field, entry) ⇒ Object



88
89
90
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 88

def date_time_custom_field_options(field, entry)
  _date_custom_field_options(field, 'clock-o', 'date-time', datetime_moment_format)
end

#default_custom_field_options(field, form, highlighted) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 54

def default_custom_field_options(field, form, highlighted)
  {
    name:     field.name,
    label:    label_for_custom_field(form.object, field),
    hint:     field.hint,
    required: field.required,
    input_html: {
      class: "#{'input-lg' if highlighted}"
    }
  }
end

#email_custom_field_options(field, entry) ⇒ Object



104
105
106
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 104

def email_custom_field_options(field, entry)
  { as: :email }
end

#file_custom_field_options(field, entry) ⇒ Object



112
113
114
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 112

def file_custom_field_options(field, entry)
  { as: :file, select_content_asset: true }
end

#float_custom_field_options(field, entry) ⇒ Object



116
117
118
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 116

def float_custom_field_options(field, entry)
  { as: :float, step: 0.1 }
end

#has_many_custom_field_options(field, entry) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 120

def has_many_custom_field_options(field, entry)
  return nil if entry.new_record? || !field.ui_enabled?

  slug = field.class_name_to_content_type.slug

  {
    as:           :array,
    template:     {
      path:   'locomotive/content_entries/entry',
      locals: { field: field, slug: slug }
    },
    wrapper_html: { class: 'has_many' },
    new_item: {
      label:  custom_field_t(:new_label, field.type),
      url:    new_content_entry_path(current_site, slug, {
        "content_entry[#{field.inverse_of}_id]" => entry._id,
        _location: false
      })
    }
  }
end

#integer_custom_field_options(field, entry) ⇒ Object



142
143
144
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 142

def integer_custom_field_options(field, entry)
  { as: :integer }
end

#json_custom_field_options(field, entry) ⇒ Object



205
206
207
208
209
210
211
212
213
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 205

def json_custom_field_options(field, entry)
  {
    as: :text,
    input_html: {
      rows:   10,
      value:  entry.send(field.name).try(:to_json)
    }
  }
end

#many_to_many_custom_field_options(field, entry) ⇒ Object



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

def many_to_many_custom_field_options(field, entry)
  slug = field.class_name_to_content_type.slug

  {
    as:           :array,
    collection:   entry.send(field.name).filtered,
    template:     {
      path:   'locomotive/content_entries/entry',
      locals: { field: field, slug: slug }
    },
    template_url: show_in_form_content_entries_path(current_site, slug, parent_slug: entry.content_type.slug, field_id: field._id),
    picker:       custom_field_picker_options(field, slug)
  }
end

#password_custom_field_options(field, entry) ⇒ Object



108
109
110
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 108

def password_custom_field_options(field, entry)
  { as: :password }
end

#render_custom_field(field, form) ⇒ String

Render the form input based on the type of the field.

Parameters:

  • field (Object)

    The field part of the content type

  • form (Object)

    The SimpleForm form instance

Returns:



31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 31

def render_custom_field(field, form)
  highlighted     = field._id == field._parent.label_field_id
  default_options = default_custom_field_options(field, form, highlighted)
  field_options   = custom_field_options(field, form)

  return '' if field_options.nil?

  options = default_options.merge(field_options)

  form.input options.delete(:name), options
end

#render_custom_fields(content_type, form, group) ⇒ String

Render all the form inputs of a given group based on the fields of a content type.

Parameters:

  • content_type (Object)

    The content type the fields belong to

  • form (Object)

    The SimpleForm form instance

  • group (Object)

    The group that will be rendered

Returns:

  • (String)

    The form inputs



12
13
14
15
16
17
18
19
20
21
22
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 12

def render_custom_fields(content_type, form, group)
  html = content_type.ordered_entries_custom_fields.map do |field|
    if (field.ui_enabled? || field.name == content_type.label_field_name) && field.group == group
      render_custom_field(field, form)
    else
      ''
    end
  end

  html.join("\n").html_safe
end

#select_custom_field_options(field, entry) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 161

def select_custom_field_options(field, entry)
  {
    name:         "#{field.name}_id",
    error_name:   field.name,
    as:           :editable_select,
    wrapper_html: { class: 'select' },
    collection:   field.ordered_select_options.map { |option| [option.name, option.id] },
    manage_collection:    {
      label:  custom_field_t(:edit, field.type),
      url:    edit_custom_fields_select_options_path(current_site, entry.content_type.slug, field.name)
    }
  }
end

#string_custom_field_options(field, entry) ⇒ Object



175
176
177
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 175

def string_custom_field_options(field, entry)
  { as: :string }
end

#tags_custom_field_options(field, entry) ⇒ Object



186
187
188
189
190
191
192
193
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 186

def tags_custom_field_options(field, entry)
  {
    wrapper_html: { class: 'tags' },
    input_html: {
      value: entry.send(field.name).try(:join, ',')
    }
  }
end

#text_custom_field_options(field, entry) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'app/helpers/locomotive/custom_fields_helper.rb', line 195

def text_custom_field_options(field, entry)
  type = case field.text_formatting
  when 'html', nil, ''  then :rte
  when 'markdown'       then :markdown
  else :text
  end

  { as: type }
end