Class: CustomFields::Controller::CustomFields

Inherits:
Zen::Controller::AdminController show all
Defined in:
lib/zen/package/custom_fields/lib/custom_fields/controller/custom_fields.rb

Overview

Custom fields allow you to create fields for your entries in their own format and with their own types. This means you're not restricted to the typical "Title" and "Body" fields you'd get when using other systems.

Custom fields can be managed to by going to a custom field group and clicking the link "Manage custom fields" (see CustomFieldGroups for more information). Once you've reached this page you'll see an overview of all your custom fields or a message saying no fields were found (if this is the case).

Custom Fields

Editing a custom field can be done by clicking on the name of the field, creating a new one can be done by clicking on the button "Add custom field". In both cases you'll be shown a form that looks like the one in the image below.

General Settings

In this form you can specify the following fields:

  • Name (required): the name of the custom field, can be anything you like. Examples are "Body" and "Date picker".
  • Slug: a URL friendly version of the name. If none is specified one will be generated automatically.
  • Field type (required): the type of custom field.
  • Format (required): the markup engine to use for the custom field. If a custom field type doesn't allow the use of markup this setting will be ignored.
  • Description: a description of the custom field.
  • Possible values: in case a custom field type allows you to specify multiple values (such as a checkbox) you can specify a value on each line. These values can be specified as following:

    key|value

Example:

  Yes!|yes
  • Requires a value: whether or not this field requires a value.
  • Enable a text editor: when set to "Yes" the user can use the markup editor when adding/editing a value of a field.
  • Textarea rows: the amount of rows when the field type is a textarea.
  • Character limit: the maximum amount of characters a user can enter in the field.
  • Sort order: a number that indicates the sort order of the field.

Used Permissions

  • show_custom_field
  • new_custom_field
  • edit_custom_field
  • delete_custom_field

Events

All events in this controller receive an instance of Model::CustomField. Just like other packages the event after_delete_custom_field receives an instance that has already been destroyed, thus you won't be able to make any changes to the object and save them in the database.

Since:

Instance Method Summary (collapse)

Instance Method Details

- (Object) delete

Delete an existing custom field.

In order to delete a custom field group you'll need to send a POST request that contains a field named 'custom_field_ids[]'. This field should contain the primary values of each field that has to be deleted.

Since:

  • 0.1



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/zen/package/custom_fields/lib/custom_fields/controller/custom_fields.rb', line 294

def delete
  authorize_user!(:delete_custom_field)

  post = request.subset(:custom_field_ids)

  if post['custom_field_ids'].nil? or post['custom_field_ids'].empty?
    message(:error, lang('custom_fields.errors.no_delete'))
    redirect_referrer
  end

  request.params['custom_field_ids'].each do |id|
    custom_field = ::CustomFields::Model::CustomField[id]

    next if custom_field.nil?
    Zen::Event.call(:before_delete_custom_field, custom_field)

    begin
      custom_field.destroy
    rescue => e
      Ramaze::Log.error(e.inspect)
      message(:error, lang('custom_fields.errors.delete') % id)

      redirect_referrer
    end

    Zen::Event.call(:after_delete_custom_field, custom_field)
  end

  message(:success, lang('custom_fields.success.delete'))
  redirect_referrer
end

- (Object) edit(custom_field_group_id, id)

Show a form that lets the user edit an existing custom field group.

Parameters:

  • id (Fixnum)

    The ID of the custom field group to which all fields belong.

  • id (Fixnum)

    The ID of the custom field to retrieve so that we can edit it.

Since:

  • 0.1



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/zen/package/custom_fields/lib/custom_fields/controller/custom_fields.rb', line 137

def edit(custom_field_group_id, id)
  authorize_user!(:edit_custom_field)

  validate_custom_field_group(custom_field_group_id)

  set_breadcrumbs(
    CustomFieldGroups.a(
      lang('custom_field_groups.titles.index'),
      :index
    ),
    CustomFields.a(
      lang('custom_fields.titles.index'),
      :index,
      custom_field_group_id
    ),
    lang('custom_fields.titles.edit')
  )

  @custom_field_group_id = custom_field_group_id

  if flash[:form_data]
    @custom_field = flash[:form_data]
  else
    @custom_field = validate_custom_field(id, custom_field_group_id)
  end

  render_view(:form)
end

- (Object) index(custom_field_group_id)

Show an overview of all existing custom fields. Using this overview a user can manage an existing field, delete it or create a new one.

Parameters:

  • custom_field_group_id (Fixnum)

    The ID of the custom field group to which all fields belong.

Since:

  • 0.1



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/zen/package/custom_fields/lib/custom_fields/controller/custom_fields.rb', line 103

def index(custom_field_group_id)
  authorize_user!(:show_custom_field)

  set_breadcrumbs(
    CustomFieldGroups.a(lang('custom_field_groups.titles.index'), :index),
    lang('custom_fields.titles.index')
  )

  field_group = validate_custom_field_group(custom_field_group_id)
  @custom_field_group_id = custom_field_group_id
  @custom_fields         = search do |query|
    ::CustomFields::Model::CustomField \
      .search(query) \
      .filter(:custom_field_group_id => custom_field_group_id) \
      .order(:id.asc)
  end

  @custom_fields ||= ::CustomFields::Model::CustomField \
    .filter(:custom_field_group_id => custom_field_group_id) \
    .order(:id.asc)

  @custom_fields = paginate(@custom_fields)
end

- (Object) new(custom_field_group_id)

Show a form that lets the user create a new custom field group.

Parameters:

  • custom_field_group_id (Fixnum)

    The ID of the custom field group to which all fields belong.

Since:

  • 0.1



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/zen/package/custom_fields/lib/custom_fields/controller/custom_fields.rb', line 174

def new(custom_field_group_id)
  authorize_user!(:new_custom_field)

  validate_custom_field_group(custom_field_group_id)

  set_breadcrumbs(
    CustomFieldGroups.a(
      lang('custom_field_groups.titles.index'),
      :index
    ),
    CustomFields.a(
      lang('custom_fields.titles.index'),
      :index,
      custom_field_group_id
    ),
    lang('custom_fields.titles.new')
  )

  @custom_field_group_id = custom_field_group_id

  if flash[:form_data]
    @custom_field = flash[:form_data]
  else
    @custom_field = ::CustomFields::Model::CustomField.new
  end

  render_view(:form)
end

- (Object) save

Saves the changes made by #edit and #new.

Since:

  • 0.1



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/zen/package/custom_fields/lib/custom_fields/controller/custom_fields.rb', line 214

def save
  post = request.subset(
    :id,
    :name,
    :slug,
    :description,
    :sort_order,
    :format,
    :possible_values,
    :required,
    :text_editor,
    :textarea_rows,
    :text_limit,
    :custom_field_group_id,
    :custom_field_type_id
  )

  validate_custom_field_group(post['custom_field_group_id'])

  # Get or create a custom field group based on the ID from the hidden
  # field.
  if post['id'] and !post['id'].empty?
    authorize_user!(:edit_custom_field)

    custom_field = validate_custom_field(
      post['id'], post['custom_field_group_id']
    )

    save_action  = :save
    before_event = :before_edit_custom_field
    after_event  = :after_edit_custom_field
  else
    authorize_user!(:new_custom_field)

    custom_field = ::CustomFields::Model::CustomField.new
    save_action  = :new
    before_event = :before_new_custom_field
    after_event  = :after_new_custom_field
  end

  post.delete('id')

  success = lang("custom_fields.success.#{save_action}")
  error   = lang("custom_fields.errors.#{save_action}")

  begin
    post.each { |k, v| custom_field.send("#{k}=", v) }
    Zen::Event.call(before_event, custom_field)

    custom_field.save
  rescue => e
    Ramaze::Log.error(e.inspect)
    message(:error, error)

    flash[:form_data]   = custom_field
    flash[:form_errors] = custom_field.errors

    redirect_referrer
  end

  Zen::Event.call(after_event, custom_field)

  message(:success, success)
  redirect(
    CustomFields.r(:edit, post['custom_field_group_id'], custom_field.id)
  )
end