Method: Edgarj::FieldHelper#draw_enum

Defined in:
app/helpers/edgarj/field_helper.rb

#draw_enum(f, col_or_sym, enum = nil, options = {}) ⇒ Object

draw enum selection.

‘Enum’ in Edgarj is a module which integer constants are defined. draw_enum() draws selection rather than simple integer text field.

Selection-option label is I18 supported by AR human_const_name API. See lib/edgarj/model.rb rdoc.

EXAMPLE

Followings draws Question module’s Priority selection on @question.priority integer column:

<%= edgarj_form do |f| %>
    :
  <%= draw_enum(f, :priority) %>
    :
<% end %>

INPUTS

f

Form builder object.

col_or_sym

column object returned by rec.class.columns, or symbol

enum

enum module. When nil, guess by column name.

options

draw_enum options and/or passed to select helper.

Supported options

:choice_1st

additional 1st choice (mainly used SearchForm enum selection)

:class AR class which will be used for human_const_name()

SEE ALSO

get_enum()

get enum definition

draw_column_enum()

draw enum column in list

FIXME: choices for selection should be cached.



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'app/helpers/edgarj/field_helper.rb', line 267

def draw_enum(f, col_or_sym, enum=nil, options={})
  col_name            =  get_column_name(col_or_sym)
  enum                = model.const_get(col_name.to_s.camelize) if !enum
  sorted_elements     = enum.constants.sort{|a,b|
                            enum.const_get(a) <=> enum.const_get(b)}
  options_for_select  = options.dup
  choice_1st          = options_for_select.delete(:choice_1st)
  class_4_human_const = options_for_select.delete(:class) || f.object.class
  f.select(col_name,
            (choice_1st ? [choice_1st] : []) +
            sorted_elements.map{|member|
              [class_4_human_const.human_const_name(enum, member),
               enum.const_get(member)]},
            options_for_select)
end