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
-
#draw_address(f, col_or_sym) ⇒ Object
draw ‘edgarj_address’ field.
-
#draw_bitset(f, col, bitset = nil, options = {}) ⇒ Object
draw bitset checkboxes.
- #draw_boolean(f, col, options = {}) ⇒ Object
-
#draw_date(f, col_or_sym, options = {}) ⇒ Object
draw calendar date select.
-
#draw_datetime(f, col_or_sym, options = {}) ⇒ Object
draw calendar datetime select.
-
#draw_enum(f, col_or_sym, enum = nil, options = {}) ⇒ Object
draw enum selection.
-
#draw_field(f, col, options = {}) ⇒ Object
draw default field for col.type.
-
#draw_file(f, col_or_sym, options = {}) ⇒ Object
Field ‘file_NN’ in AR is handled as file attachement(upload/download) in Edgarj.
-
#draw_form_buttons(options = {}) ⇒ Object
Draw buttons for form.
- #draw_question_history(question) ⇒ Object
- #draw_search_form_buttons ⇒ Object
- #draw_search_save_popup ⇒ Object
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
180 181 182 183 184 185 186 187 |
# File 'app/helpers/edgarj/field_helper.rb', line 180 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)
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 233 |
# File 'app/helpers/edgarj/field_helper.rb', line 206 def draw_bitset(f, col, bitset=nil, ={}) 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
136 137 138 |
# File 'app/helpers/edgarj/field_helper.rb', line 136 def draw_boolean(f, col, ={}) f.check_box(col.name, ) 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
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'app/helpers/edgarj/field_helper.rb', line 146 def draw_date(f, col_or_sym, ={}) 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(<<-EOJS) $(function(){ $('##{dom_id}').datepicker({buttonImageOnly: true}). datepicker('option', { dateFormat: 'yy/mm/dd' }); }); EOJS end |
#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
166 167 168 169 170 |
# File 'app/helpers/edgarj/field_helper.rb', line 166 def draw_datetime(f, col_or_sym, ={}) 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.
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'app/helpers/edgarj/field_helper.rb', line 268 def draw_enum(f, col_or_sym, enum=nil, ={}) 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)} = .dup choice_1st = .delete(:choice_1st) class_4_human_const = .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)]}, ) 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.
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'app/helpers/edgarj/field_helper.rb', line 123 def draw_field(f, col, ={}) case col.type when :date draw_date(f, col, [:date] || {}) when :datetime draw_datetime(f, col, [:datetime] || {}) when :integer f.text_field(col.name, [:integer]) else f.text_field(col.name, [: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:
-
upload file (Create)
-
download file (Read)
-
upload another file (Update)
-
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
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 351 |
# File 'app/helpers/edgarj/field_helper.rb', line 325 def draw_file(f, col_or_sym, ={}) 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, .merge(:style=>'display:none')) + ' ' + content_tag(: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, ) 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.
- :namespace
-
delete url namespace
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 74 |
# File 'app/helpers/edgarj/field_helper.rb', line 19 def ( = {}) content_tag(:table) do content_tag(:tr) do # save button content_tag(: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 content_tag(:td) do (t('edgarj.default.search_form'), <<-JS, $('#edgarj_form').hide(); $('#edgarj_search_form').show(); JS {class: '_edgarj_form_search'}.merge([:search_form] ||{})) end + # clear button content_tag(:td) do (t('edgarj.default.clear'), {action: 'clear'}, { method: :get, remote: true, }) end + # delete button content_tag(:td) do (t('edgarj.default.delete'), if @record.new_record? url_for('/') else url_for({ controller: params[:controller], action: 'destroy', id: @record.id}) end, { method: :delete, remote: true, data: {confirm: t('edgarj.form.delete_confirm')}, disabled: @record.new_record? # || !permitted?(cp_bitset::DELETE), }) end end end end |
#draw_question_history(question) ⇒ Object
284 285 286 |
# File 'app/helpers/edgarj/field_helper.rb', line 284 def draw_question_history(question) render :partial => '/questions/history', :locals=>{:question=>question} end |
#draw_search_form_buttons ⇒ Object
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 103 |
# File 'app/helpers/edgarj/field_helper.rb', line 76 def content_tag(:table) do content_tag(:tr) do # search button content_tag(:td) do (v('search'), '$("#_edgarj_search_form").submit()') end + # search_end button content_tag(:td) do (v('search_end'), <<-JS) $('#edgarj_form').show(); $('#edgarj_search_form').hide(); JS end + # search_clear button content_tag(:td) do (v('search_clear'), {action: 'search_clear'}, { method: :get, remote: true, }) end end end end |
#draw_search_save_popup ⇒ Object
105 106 107 |
# File 'app/helpers/edgarj/field_helper.rb', line 105 def draw_search_save_popup render :partial => 'edgarj/search_save_popup' end |