Class: CustomFields::Controller::CustomFieldTypes

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

Overview

Custom field types allow you to create your own types of fields. Being able to create your own field types without having to write any code can be very useful. For example, say you want to be able to create a textarea with a special class (maybe you can to use CKEditor), all you'd have to do is create a new field type, add the class and you're good to go.

In order to manage field types you'll have to navigate to /admin/custom-field-types. This can be done by either manually entering the URL into your browser's URL bar or by hovering over the "Custom fields" menu item, this will cause the menu to expand and show a URL called "Custom field types". Clicking this URL will take you to an overview of all existing field types.

Types

Adding/Editing Field Types

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

Edit Type

In this form you can specify the following fields:

  • Name (required): the name of the custom field type. This name can be anything you like.
  • Language string (required): a valid language string that will result in a language specific block of text. This text will be used for the label.
  • HTML Class: a space separated list of HTML classes to apply to the field type. The format of this value has to match the regular expression /^[a-zA-Z\-_0-9\s]*$/.
  • Serialize: whether or not the value of a field using this type should be serialized. Set this to "Yes" if a field takes multiple values such as a checkbox or a select element with the attribute multiple="multiple".
  • Allow markup: whether or not users can use markup, such as Markdown in a field using this type.
  • Custom field method (required): the name of a method in BlueFormParameters. This method will be used to generate all the parameters for the BlueForm helper.

Note that the name, language string and HTML class can not be longer than 255 characters.

Used Permissions

  • show_custom_field_type
  • edit_custom_field_type
  • new_custom_field_type
  • delete_custom_field_type

Events

All events in this controller will receive an instance of Model::CustomFieldType. Just like other packages the event after_delete_custom_field_type receives an instance that has already been destroyed. This means that this event can not be used to make changes to the object and save them.

Since:

Instance Method Summary (collapse)

Instance Method Details

- (Object) delete

Deletes a number of custom field types. These types should be specified in the POST array "custom_field_type_ids".

Since:

  • 0.2.8



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
# File 'lib/zen/package/custom_fields/lib/custom_fields/controller/custom_field_types.rb', line 240

def delete
  authorize_user!(:delete_custom_field_type)

  if !request.params['custom_field_type_ids'] \
  or request.params['custom_field_type_ids'].empty?
    message(:error, lang('custom_field_types.errors.no_delete'))
    redirect_referrer
  end

  request.params['custom_field_type_ids'].each do |id|
    type = ::CustomFields::Model::CustomFieldType[id]

    next if type.nil?
    Zen::Event.call(:before_delete_custom_field_type, type)

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

      redirect_referrer
    end

    Zen::Event.call(:after_delete_custom_field_type, type)
  end

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

- (Object) edit(id)

Allows a user to edit an existing custom field type.

Parameters:

  • id (Fixnum)

    The ID of the custom field type to edit.

Since:

  • 0.2.8



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/zen/package/custom_fields/lib/custom_fields/controller/custom_field_types.rb', line 123

def edit(id)
  authorize_user!(:edit_custom_field_type)

  set_breadcrumbs(
    CustomFieldTypes.a(lang('custom_field_types.titles.index'), :index),
    lang('custom_field_types.titles.edit')
  )

  @custom_field_type = flash[:form_data] || validate_custom_field_type(id)
  @custom_field_methods = ::CustomFields::Model::CustomFieldMethod \
    .pk_hash(:name)

  render_view(:form)
end

- (Object) index

Shows an overview of all the available custom field types and allows the user to create new ones, edit existing ones or delete a group of field types.

Since:

  • 0.2.8



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/zen/package/custom_fields/lib/custom_fields/controller/custom_field_types.rb', line 100

def index
  authorize_user!(:show_custom_field_type)

  set_breadcrumbs(lang('custom_field_types.titles.index'))

  @field_types = search do |query|
    ::CustomFields::Model::CustomFieldType.search(query).order(:id.asc)
  end

  @field_types ||= ::CustomFields::Model::CustomFieldType \
    .eager(:custom_field_method) \
    .order(:id.asc)

  @field_types = paginate(@field_types)
end

- (Object) new

Allows a user to add a new custom field type.

Since:

  • 0.2.8



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/zen/package/custom_fields/lib/custom_fields/controller/custom_field_types.rb', line 144

def new
  authorize_user!(:new_custom_field_type)

  set_breadcrumbs(
    CustomFieldTypes.a(lang('custom_field_types.titles.index'), :index),
    lang('custom_field_types.titles.new')
  )

  @custom_field_methods = ::CustomFields::Model::CustomFieldMethod \
    .pk_hash(:name)

  if flash[:form_data]
    @custom_field_type = flash[:form_data]
  else
    @custom_field_type = ::CustomFields::Model::CustomFieldType.new
  end

  render_view(:form)
end

- (Object) save

Creates a new custom field type or edits an existing one.

This method requires either create or update permissions based on the supplied data.

Since:

  • 0.2.8



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/zen/package/custom_fields/lib/custom_fields/controller/custom_field_types.rb', line 178

def save
  post = request.subset(
    :id,
    :name,
    :language_string,
    :html_class,
    :serialize,
    :allow_markup,
    :custom_field_method_id
  )

  if post['id'] and !post['id'].empty?
    authorize_user!(:edit_custom_field_type)

    field_type   = validate_custom_field_type(post['id'])
    save_action  = :save
    before_event = :before_edit_custom_field_type
    after_event  = :after_edit_custom_field_type
  else
    authorize_user!(:new_custom_field_type)

    field_type   = ::CustomFields::Model::CustomFieldType.new
    save_action  = :new
    before_event = :before_new_custom_field_type
    after_event  = :after_new_custom_field_type
  end

  post.delete('id')

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

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

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

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

    redirect_referrer
  end

  Zen::Event.call(after_event, field_type)

  message(:success, success)
  redirect(CustomFieldTypes.r(:edit, field_type.id))
end