Module: Edgarj::FieldHelper

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

Overview

Edgarj::FieldHelper is independent from Edgarj default form (FormDrawer::Base) so you can use helper methods here for your customized form.

Instance Method Summary collapse

Instance Method Details

#draw_address(f, col_or_sym) ⇒ Object

draw ‘edgarj_address’ field

The column, which is declared as ‘edgarj_address’, can be drawn by this helper.

INPUTS

f

FormBuilder

col_or_sym

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



179
180
181
182
183
184
185
186
# File 'app/helpers/edgarj/field_helper.rb', line 179

def draw_address(f, col_or_sym)
  address_name  = f.object.class.get_belongs_to_name(col_or_sym)
  render('edgarj/address',
      f:            f,
      rec:          f.object,
      address_name: address_name
  )
end

#draw_bitset(f, col, bitset = nil, options = {}) ⇒ Object

draw bitset checkboxes.

When model class has integer field (e.g. ‘flags’) and Flags module which defines ‘bitflag’ constant, model.flags integer column is drawn as bitset checkboxes. Constant name will be translated by config/locales/*.yml See ModelPermission for example.

‘flags’ column value is calculated at client side by JavaScript.

options is not used now.

INPUTS

f

Form builder object.

col

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

bitset

ruby module contains ‘bitflag’ integer constants

options

(not used)



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
230
231
232
# File 'app/helpers/edgarj/field_helper.rb', line 205

def draw_bitset(f, col, bitset=nil, options={})
  html          = ''
  bitset        = model.const_get(col_name.to_s.camelize + 'Bitset') if !bitset
  i             = 0
  id_array_var  = sprintf('%s_%s_var', f.object_name, col.name)
  ids           = []
  for flag in bitset.constants do
    checkbox_id = sprintf('%s_%s_%d', f.object_name, col.name, i)
    html += draw_checkbox(f, checkbox_id, flag, bitset, id_array_var) +
            label_tag(
                checkbox_id,
                f.object.class.human_const_name(bitset, flag)) +
            '  '.html_safe
    ids << checkbox_id
    i += 1
  end
  # draw hidden field to send sum-up value
  html += f.hidden_field(col.name)

  # add hidden-field name to ids' last
  ids << sprintf("%s_%s", f.object_name, col.name)

  # define arrays to calculate flags
  html += "<script> var #{id_array_var}=[" +
                ids.map{|id| "'" + id + "'"}.join(',') +
            "];</script>"
  html.html_safe
end

#draw_boolean(f, col, options = {}) ⇒ Object



135
136
137
# File 'app/helpers/edgarj/field_helper.rb', line 135

def draw_boolean(f, col, options={})
  f.check_box(col.name, options)
end

#draw_date(f, col_or_sym, options = {}) ⇒ Object

draw calendar date select

INPUTS

f

Form builder object.

col_or_sym

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

options

passed to calendar_date_select



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/helpers/edgarj/field_helper.rb', line 145

def draw_date(f, col_or_sym, options={})
  col_name  = get_column_name(col_or_sym)
  dom_id    = sprintf("%s_%s", f.object_name, col_name)
  f.text_field(col_name, id: dom_id, size: 14) +
  javascript_tag("    $(function(){\n      $('#\#{dom_id}').datepicker({buttonImageOnly: true}).\n          datepicker('option', {\n            dateFormat:       'yy/mm/dd'\n          });\n    });\n  EOJS\nend\n")

#draw_datetime(f, col_or_sym, options = {}) ⇒ Object

draw calendar datetime select

INPUTS

f

Form builder object.

col_or_sym

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

options

passed to calendar_date_select



165
166
167
168
169
# File 'app/helpers/edgarj/field_helper.rb', line 165

def draw_datetime(f, col_or_sym, options={})
  col_name  = get_column_name(col_or_sym)
  f.text_field(col_name,
      value: datetime_fmt(f.object.send(col_name)))
end

#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

#draw_field(f, col, options = {}) ⇒ Object

draw default field for col.type

options is passed to each rails helper. Following types are supported:

  • :date

  • :datetime

  • :integer

  • :boolean

  • :text

INPUTS

f

FormBuilder object

col

column info returned by AR.columns, or symbol

options

options hash passed to each helper.



122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/helpers/edgarj/field_helper.rb', line 122

def draw_field(f, col, options={})
  case col.type
  when :date
    draw_date(f, col, options[:date] || {})
  when :datetime
    draw_datetime(f, col, options[:datetime] || {})
  when :integer
    f.text_field(col.name, options[:integer])
  else
    f.text_field(col.name, options[:text])
  end
end

#draw_file(f, col_or_sym, options = {}) ⇒ Object

Field ‘file_NN’ in AR is handled as file attachement(upload/download) in Edgarj. Where, NN is 2-digits from 00 to 99.

draw_file() helper draws file attachment(upload/download) user-interface for file_NN field. It supports:

  1. upload file (Create)

  2. download file (Read)

  3. upload another file (Update)

  4. clear the field (Delete)

Model

Integer value of file_NN column in any AR is interpreted a ID value to point to a FileInfo record. As NN means, any number of files (max 100) can be attached to any AR.

belongs_to/has_one relation can be declared between the AR and FileInfo, but it is not required. Those declarations are just for your customization level convenience.

attr_accessible(or attr_protected) to hide file_NN from mass-assignment *SHOULD BE* applied.

See Report model unit test for testing.

INPUTS

f

Form builder object.

col_or_sym

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

options

passed to select helper.

EXAMPLE

draw_file(f, :file_00) draws:

<input type=file id='file_info[file_00][uploaded_data]' ...>

SEE ALSO

ActiveRecord::Base#upsert_file_NN



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'app/helpers/edgarj/field_helper.rb', line 324

def draw_file(f, col_or_sym, options={})
  col_name  = get_column_name(col_or_sym)
  file_info = FileInfo.safe_find(f.object.send(col_name))
  error_wrapping(
    if file_info
      file_field_dom    = "file_info_#{col_name}_uploaded_data"
      file_link_dom     = "file_info_#{col_name}_link"

      file_field_sub(col_name, file_info, options.merge(:style=>'display:none')) +
      ' ' +
      (:span, :id=>file_link_dom) do
        link_to(file_info.filename,
            {:action    => 'file_download',
             :id        => f.object.id,
             :column    => col_name}) + ' ' +
        link_to_function("[#{t('edgarj.default.clear')}]",
            sprintf("Edgarj.clear_file('%s', '%s', '%s')",
                file_field_dom,
                "#{f.object_name}_#{col_name}",
                file_link_dom))
      end +
      f.hidden_field(col_name)
    else
      file_field_sub(col_name, FileInfo.new, options)
    end, f.object.errors.on(col_name)
  )
end

#draw_form_buttons(options = {}) ⇒ Object

Draw buttons for form.

When no CREATE/UPDATE permission, save button is disabled. It can be overwritten by options.

When no DELETE permission, delete button is disabled. It can be overwritten by options.

options may have:

:save

html options for ‘save’ button.

:search_form

html options for ‘search_form’ button.

:delete

html options for ‘delete’ button.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/edgarj/field_helper.rb', line 18

def draw_form_buttons(options = {})
  (:table) do
    (:tr) do
      # save button
      (:td) do
        #cp_bitset = Edgarj::ModelPermission::FlagsBitset
        #create_or_update = cp_bitset::CREATE + cp_bitset::UPDATE
        tag(:input, {
            type:     'button',
            name:     'save',
            onClick:  '$("#_edgarj_form").submit()',
            value:    t('edgarj.default.save'),
            class:    '_edgarj_form_save',})
           #disabled: !permitted?(create_or_update)}.merge(options[:save]||{}))
      end +

      # search button
      (:td) do
        button_for_js(t('edgarj.default.search_form'), "            $('#edgarj_form').hide();\n            $('#edgarj_search_form').show();\n          JS\n          {class: '_edgarj_form_search'}.merge(options[:search_form] ||{}))\n      end +\n\n      # clear button\n      content_tag(:td) do\n        button_to(t('edgarj.default.clear'),\n            {action: 'clear'},\n            {\n              method:   :get,\n              remote:   true,\n            })\n      end +\n\n      # delete button\n      content_tag(:td) do\n        button_to(t('edgarj.default.delete'),\n            if @record.new_record?\n                url_for('/')\n            else\n                url_for({\n                    controller: params[:controller],\n                    action:     'destroy',\n                    id:         @record.id})\n            end,\n            {\n              method:   :delete,\n              remote:   true,\n              data:     {confirm:  t('edgarj.form.delete_confirm')},\n              disabled: @record.new_record?   # || !permitted?(cp_bitset::DELETE),\n            })\n      end\n    end\n  end\nend\n",

#draw_question_history(question) ⇒ Object



283
284
285
# File 'app/helpers/edgarj/field_helper.rb', line 283

def draw_question_history(question)
  render :partial => '/questions/history', :locals=>{:question=>question}
end

#draw_search_form_buttonsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/edgarj/field_helper.rb', line 75

def draw_search_form_buttons
  (:table) do
    (:tr) do
      # search button
      (:td) do
        button_for_js(v('search'), '$("#_edgarj_search_form").submit()')
      end +

      # search_end button
      (:td) do
        button_for_js(v('search_end'), "          $('#edgarj_form').show();\n          $('#edgarj_search_form').hide();\n        JS\n      end +\n\n      # search_clear button\n      content_tag(:td) do\n        button_to(v('search_clear'),\n            {action: 'search_clear'},\n            {\n              method:   :get,\n              remote:   true,\n            })\n      end\n    end\n  end\nend\n")

#draw_search_save_popupObject



104
105
106
# File 'app/helpers/edgarj/field_helper.rb', line 104

def draw_search_save_popup
  render :partial => 'edgarj/search_save_popup'
end