Module: EdgarHelper

Includes:
Edgar::FieldHelper, Edgar::FormDrawer, Edgar::ListDrawer, Edgar::MenuHelper, Edgar::PopupHelper
Defined in:
app/helpers/edgar_helper.rb

Defined Under Namespace

Classes: CsvVisitor, RemoteLinkRenderer

Instance Method Summary (collapse)

Methods included from Edgar::MenuHelper

#draw_controller_menu, #draw_item, #draw_menu

Methods included from Edgar::FieldHelper

#draw_address, #draw_bitset, #draw_boolean, #draw_checkbox, #draw_date, #draw_datetime, #draw_enum, #draw_field, #draw_file, #draw_form_buttons, #draw_question_history, #draw_search_form_buttons, #draw_search_save_popup, #error_wrapping, #file_field_sub, #find_col, #get_column_name

Instance Method Details

- (Object) adrs_str(model, adrs_prefix)

model & adrs_prefix -> address string



331
332
333
334
335
336
337
# File 'app/helpers/edgar_helper.rb', line 331

def adrs_str(model, adrs_prefix)
  result = ''
  for adrs_element in ['prefecture', 'city', 'other', 'bldg'] do
    result << adrs_str_sub(model, adrs_prefix, adrs_element)
  end
  result
end

- (Object) adrs_str_sub(model, prefix, element)

return address element string or "



325
326
327
328
# File 'app/helpers/edgar_helper.rb', line 325

def adrs_str_sub(model, prefix, element)
  e = model.send(prefix + element)
  e.blank? ? '' : e
end

- (Object) date_fmt(dt)

Edgar standard date format



84
85
86
87
88
89
90
# File 'app/helpers/edgar_helper.rb', line 84

def date_fmt(dt)
  if dt == nil then
    ''
  else
    dt.strftime(I18n.t('date.formats.default'))
  end
end

- (Object) datetime_fmt(dt)

Edgar standard datetime format



75
76
77
78
79
80
81
# File 'app/helpers/edgar_helper.rb', line 75

def datetime_fmt(dt)
  if dt.blank? then
    ''
  else
    I18n.l(dt, format: I18n.t('edgar.time.format'))
  end
end

- (Object) draw_belongs_to(parent_obj, link = true)

draw belongs_to as hyper link



165
166
167
168
169
170
171
172
173
174
# File 'app/helpers/edgar_helper.rb', line 165

def draw_belongs_to(parent_obj, link=true)
    if link
      link_to( parent_obj.name,
            :controller => '/' + parent_obj.class.name.underscore.pluralize,
            :action     => 'show',
            :id         => parent_obj.id)
    else
      h(parent_obj.name)
    end
end

- (Object) draw_belongs_to_field(f, col_name, model_class = f.object.class)

draw 'belongs_to' popup field

This is usually used with draw_belongs_to_label().

INPUTS

f

FormBuilder object

col_name

'belongs_to' column name

model_class

data model class



211
212
213
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
# File 'app/helpers/edgar_helper.rb', line 211

def draw_belongs_to_field(f, col_name, model_class = f.object.class)
  col   = model_class.columns.detect{|c| c.name == col_name.to_s}
  return "no column found" if !col

  parent_model = model_class.belongs_to_AR(col)
  return "parent_model is nil" if !parent_model

  parent_obj  = f.object.belongs_to_AR(col)
  popup_field = Edgar::PopupHelper::PopupField.new_builder(f, col_name)
  label       = sprintf("<span id='%s'>%s</span>",
                        popup_field.label_target,
                        parent_obj ? parent_obj.name : '').html_safe
  if parent_obj
    link_to(label,
        :controller => '/' + parent_obj.class.name.underscore.pluralize,
        :action     => 'show',
        :id         => parent_obj,
        :topic_path => 'add')
  else
    label
  end +
  ('&nbsp;&nbsp;' +
      link_to("[#{I18n.t('clear')}]", '#',
          onClick:  sprintf("Edgar.Popup.clear('%s'); return false;",
                            popup_field.id_target),
          id:       popup_field.clear_link,
          style:    'display:' + (parent_obj ? '' : 'none')) +
      f.hidden_field(col.name)).html_safe
end

- (Object) draw_belongs_to_label(f, col_name, model_class = f.object.class)

draw 'belongs_to' popup. This is used:

  • at edgar form for data entry

  • at edgar search form(Edgar::SearchForm) for search-condition entry

INPUTS

f

FormBuilder object

col_name

'belongs_to' column name

model_class

data model class



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/helpers/edgar_helper.rb', line 185

def draw_belongs_to_label(f, col_name, model_class = f.object.class)
  col   = model_class.columns.detect{|c| c.name == col_name.to_s}
  return "no column found" if !col

  parent_model_class = model_class.belongs_to_AR(col)
  return "parent_model is nil" if !parent_model_class

  popup_field = Edgar::PopupHelper::PopupField.new_builder(f, col_name)
  link_to(
      draw_belongs_to_label_sub(model_class, col.name, parent_model_class),
      main_app.url_for(
          controller:   '/edgar_popup',
          action:       'index',
          model_class:  parent_model_class.to_s,
          id_target:    popup_field.id_target),
      remote:           true)
end

- (Object) draw_belongs_to_label_sub(model_class, col_name, parent_model_class)

  1. column I18n is used if defined

  2. else, parent.human_name is used.



149
150
151
152
# File 'app/helpers/edgar_helper.rb', line 149

def draw_belongs_to_label_sub(model_class, col_name, parent_model_class)
  I18n.t("activerecord.attributes.#{model_class.to_s.underscore}.#{col_name}",
      default:  parent_model_class.human_name)
end

- (Object) draw_column_bitset(rec, col_or_sym, bitset)

draw bitset column in list.

INPUTS

rec

AR object

col_or_sym

column object returned by rec.class.columns

bitset

Module which contains bitset constants

SEE ALSO

get_bitset()

get bitset definition

draw_bitset()

draw bitste checkboxes field

draw_column_enum()

draw bitset column in list



272
273
274
275
276
277
278
279
# File 'app/helpers/edgar_helper.rb', line 272

def draw_column_bitset(rec, col_or_sym, bitset)
  turn_on_flags = []
  value         = rec.send(get_column_name(col_or_sym))
  for flag in bitset.constants do
    turn_on_flags << flag if flag_on?(value, bitset, flag)
  end
  turn_on_flags.map{|f| rec.class.human_const_name(bitset, f) }.join(' ')
end

- (Object) draw_column_enum(rec, col_or_sym, enum)

draw enum column in list.

When enum for col is defined, constant string (rather than rec.col value) is drawn. See get_enum() for more detail of enum for the col.

EXAMPLE

Question has status attribute and Question::Status enum. When question.status == 300, draw_column_enum(question, status_col, Question::Status) returns I18n.t('WORKING').

Where:

  • question is Question AR object.

  • status_col is one of Question.columns object for status column.

INPUTS

rec

AR object

col_or_sym

column object returned by rec.class.columns

enum

Module which contains constants

SEE ALSO

get_enum()

get enum definition

draw_enum()

draw enum selection field

draw_column_bitset()

draw bitset column in list



320
321
322
# File 'app/helpers/edgar_helper.rb', line 320

def draw_column_enum(rec, col_or_sym, enum)
  Edgar::EnumCache.instance.label(rec, get_column_name(col_or_sym), enum)
end

- (Object) draw_list_column_header(col, options = {})

draw list column header for both usual list and popup list



155
156
157
158
159
160
161
162
# File 'app/helpers/edgar_helper.rb', line 155

def draw_list_column_header(col, options={})
  parent = model_class.belongs_to_AR(col)
  if parent then
    draw_belongs_to_label_sub(model_class, col.name, parent)
  else
    draw_sort(col, {model_class: model_class}.merge(options))
  end
end

- (Object) draw_menu_top_and_pulldown(id, name) (private)

Provide consistency between <td> tag id and its sub contents <div> id. This concistency is required for javascript pull-down action.



402
403
404
405
406
407
408
# File 'app/helpers/edgar_helper.rb', line 402

def draw_menu_top_and_pulldown(id, name)
  top_menu_td(id, name) do
    (:div, :class=>'submenu', :id=>"sub#{id}") do
      yield
    end
  end
end

- (Object) draw_search_operator(o, method)

INPUTS

o

operator form builder

method

column name



68
69
70
71
72
# File 'app/helpers/edgar_helper.rb', line 68

def draw_search_operator(o, method)
  render '/edgar/search_operator',
      o:        o,
      method:   method
end

- (Object) draw_sort(col, options = {})

draw sort link on list column header

INPUTS

col

column data

options

options to url_for



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/helpers/edgar_helper.rb', line 123

def draw_sort(col, options={})
  label = model_class.human_attribute_name(col.name)
  dir   = 'asc'
  if @view_status.order_by == col.name
    # toggle direction
    if @view_status.dir == 'asc' || @view_status.dir.blank?
      label += ''
      dir    = 'desc'
    else
      label += ''
    end
  end
  link_to(label,
      {
      :controller                   => params[:controller],
      :action                       => 'view_status_save',
      :id                           => @view_status.id,
      'edgar_view_status[order_by]' => col.name,
      'edgar_view_status[dir]'      => dir
      }.merge(options),
      :remote => true,
      :method => :put)
end

- (Object) draw_view_status_load_menu



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/helpers/edgar_helper.rb', line 92

def draw_view_status_load_menu
  (:div, :id=>'edgar_load_condition_menu', :class=>'edgar_menu') do
    (:table) do
      (:tr) do
        if current_user.saved_view_statuss.count(:conditions=>['view=?', controller_name]) > 0
          draw_menu_top_and_pulldown('saved_conditions', t('load_condition')) do
            out = ""
            for svc in current_user.saved_view_statuss.find_all_by_view(controller_name) do
              out += edgar_link_to_remote(
                            svc.name, {:action=>'search_load', :id=>svc.id})
            end
            out
          end
        else
          # not to draw empty sub menu of 'saved_conditions' 
          ''
        end +
        (:td) do
          link_to 'Help', url_for(:controller=>'/help',
                            :action=>'search.html'), {:target=>'help'}
        end
      end
    end
  end
end

- (Object) edgar_form(&block)

form_for which supports Ajax-like file upload.

In order to support Ajax-like file upload, use form-target-iframe-responds_to_parent mechanism.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/helpers/edgar_helper.rb', line 39

def edgar_form(&block)
  url_hash = {
    controller: params[:controller],
    action:     @model.new_record? ? 'create' : 'update',
    format:     :js
  }
  url_hash[:id] = @model.id if @model.persisted?
  form_for(@model,
      remote: true,
      url:    url_hash,
      html:   {
          id:         '_edgar_form',
          method:     @model.new_record? ? 'post' : 'put',
          multipart:  true,
         #target:     'edgar_form_frame'
      }, &block)
end

link_to_remote + spinner

We may add 'spinner' feature in various level as:

  1. modify Ajax.request() javascript

  2. modify remote_function Rails API

  3. add new helper

Because of code size on browser-side, I chose 3 above.

INPUTS

name

link name

url

only options in link_to_remote is supported now

method

'get' or 'post', default='get'

html_options

same as link_to_remote



26
27
28
# File 'app/helpers/edgar_helper.rb', line 26

def edgar_link_to_remote(name, url, method='get', html_options={})
  link_to_function(name, edgar_remote_function(url, method), html_options)
end

- (Object) edgar_remote_function(url, method = 'get')

remote_function + spinner



31
32
33
# File 'app/helpers/edgar_helper.rb', line 31

def edgar_remote_function(url, method='get')
  "Edgar._simple_ajax('#{escape_javascript(url_for(url))}', '#{method}')"
end

- (Boolean) flag_on?(column_value, bitset, flag)

Is flag in column_value on?

Returns:

  • (Boolean)


242
243
244
245
# File 'app/helpers/edgar_helper.rb', line 242

def flag_on?(column_value, bitset, flag)
  val = column_value || 0
  (val & bitset.const_get(flag)) != 0
end

- (Object) get_bitset(model_class, col)

get bitset Module.

When ColBitset(camelized argument col name + 'Bitset') module exists, the ColBitset is assumed enum definition.



251
252
253
254
255
256
257
258
259
# File 'app/helpers/edgar_helper.rb', line 251

def get_bitset(model_class, col)
  bitset_name = col.name.camelize + 'Bitset'
  if model_class.const_defined?(bitset_name, false)
    _module = model_class.const_get(bitset_name)
    _module.is_a?(Module) ? _module : nil
  else
    nil
  end
end

- (Object) get_enum(model_class, col)

get enum Module.

When Col(camelized argument col name) module exists, the Col is assumed enum definition.



285
286
287
288
289
290
291
292
293
# File 'app/helpers/edgar_helper.rb', line 285

def get_enum(model_class, col)
  col_name  = col.name
  if model_class.const_defined?(col_name.camelize, false)
    enum = model_class.const_get(col_name.camelize)
    enum.is_a?(Module) ? enum : nil
  else
    nil
  end
end

- (Object) iframe_for_edgar_form

According to khamsouk.souvanlasy.com/articles/ajax_file_uploads_in_rails 'display:none' style should NOT used:



59
60
61
62
63
# File 'app/helpers/edgar_helper.rb', line 59

def iframe_for_edgar_form
  %Q(<iframe id=edgar_form_frame name=edgar_form_frame
    style='width:1px;height:1px;border:0px' src='about:blank'></iframe>
  ).html_safe
end

- (Boolean) permitted?(requested_flags = 0)

return true if login user has enough permission on current controller.

Returns:

  • (Boolean)


340
341
342
343
# File 'app/helpers/edgar_helper.rb', line 340

def permitted?(requested_flags = 0)
  current_user_roles.any?{|ug| ug.admin?} ||
  current_model_permissions.any?{|cp| cp.permitted?(requested_flags)}
end

- (Boolean) permitted_on?(controller)

return true if login user has any permission on the controller.

Returns:

  • (Boolean)


346
347
348
# File 'app/helpers/edgar_helper.rb', line 346

def permitted_on?(controller)
  true  # TBD
end

- (Object) top_menu_td(id, name) (private)



390
391
392
393
394
395
396
397
398
# File 'app/helpers/edgar_helper.rb', line 390

def top_menu_td(id, name)
  (:td,
    :id           => id,
    :onMouseOver  => "Edgar.Menu.show('#{id}')",
    :onMouseOut   => "Edgar.Menu.hide('#{id}')") do

    (:a, name, :href=>'#') + yield
  end
end