Module: Edgarj::AssocHelper

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

Defined Under Namespace

Classes: CsvVisitor

Instance Method Summary collapse

Instance Method Details

#adrs_str(model, adrs_prefix) ⇒ Object

model & adrs_prefix -> address string



320
321
322
323
324
325
326
# File 'app/helpers/edgarj/assoc_helper.rb', line 320

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

#adrs_str_sub(model, prefix, element) ⇒ Object

return address element string or ”



314
315
316
317
# File 'app/helpers/edgarj/assoc_helper.rb', line 314

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

draw ‘clear’ link for ‘belongs_to’ popup data-entry field

INPUTS

f

FormBuilder object

col_name

‘belongs_to’ column name

popup_field

Edgarj::PopupHelper::PopupField object

parent_name

initial parent name



179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/helpers/edgarj/assoc_helper.rb', line 179

def draw_belongs_to_clear_link(f, col_name, popup_field, parent_name, default_label)
  if Settings.edgarj.belongs_to.disable_clear_link
    f.hidden_field(col_name)
  else
    ('&nbsp;&nbsp;' +
        link_to("[#{I18n.t('edgarj.default.clear')}]", '#',
            onClick:  "Edgarj.Popup.clear('#{j(popup_field.id_target)}','#{j(default_label)}'); return false;",
            id:       popup_field.clear_link,
            style:    'display:' + (parent_name.blank? ? 'none' : '')) +
        f.hidden_field(col_name)).html_safe
  end
end

#draw_belongs_to_field(f, popup_path, col_name, model = f.object.class) ⇒ Object

draw ‘belongs_to’ popup data-entry field

This is usually used with draw_belongs_to_label().

Parameters:

  • f (FormBuilder)

    FormBuilder object

  • popup_path (String)

    popup path(url)

  • col_name (String)

    ‘belongs_to’ column name

  • model (AR) (defaults to: f.object.class)

    data model class



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
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'app/helpers/edgarj/assoc_helper.rb', line 200

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

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

  parent_obj  = f.object.belongs_to_AR(col)
  popup_field = Edgarj::PopupHelper::PopupField.new_builder(
      f.object_name, col_name)
  default_label = '[' + draw_belongs_to_label_sub(model, col.name, parent_model) + ']'
  label = (:span,
      parent_obj ? parent_obj.name : default_label.html_safe,
      id: popup_field.label_target)
  link_tag = Settings.edgarj.belongs_to.link_tag.html_safe
  if parent_obj
    if Settings.edgarj.belongs_to.popup_on == 'field'
      link_to(
          label + link_tag,
          popup_path,
          remote: true)
    else
      link_to(label,
          # TODO: Hardcoded 'master' prefix should be fixed
          controller: url_prefix + parent_obj.class.name.underscore.pluralize,
          action:     'show',
          id:         parent_obj,
          topic_path: 'add')
    end
  else
    if Settings.edgarj.belongs_to.popup_on == 'field'
      link_to(
          label + link_tag,
          popup_path,
          remote: true)
    else
      label
    end
  end +
  draw_belongs_to_clear_link(f, col.name, popup_field,
      parent_obj && parent_obj.name,
      default_label)
end

#draw_belongs_to_label(f, popup_path, col_name, model = f.object.class) ⇒ Object

draw ‘belongs_to’ popup button(link) label. This is used at:

  • edgarj form for data entry

  • edgarj search form(Edgarj::SearchForm) for search-condition entry

INPUTS

Parameters:

  • f (FormBuilder)

    FormBuilder object

  • popup_path (String)

    popup path(url)

  • col_name (String)

    ‘belongs_to’ column name

  • model (AR) (defaults to: f.object.class)

    data model class



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/helpers/edgarj/assoc_helper.rb', line 158

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

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

  link_to(
      draw_belongs_to_label_sub(model, col.name, parent_model).html_safe +
      Settings.edgarj.belongs_to.link_tag.html_safe,
      popup_path,
      remote: true)
end

#draw_belongs_to_label_sub(model, col_name, parent_model) ⇒ Object

  1. t(‘view.CONTROLLER.MODEL.COL_NAME’)

  2. MODEL.human_attribute_name(COL_NAME)

  3. else, parent.human_name is used.



125
126
127
128
129
130
131
132
# File 'app/helpers/edgarj/assoc_helper.rb', line 125

def draw_belongs_to_label_sub(model, col_name, parent_model)
  @controller_model ||= controller.send(:model)

  I18n.t(col_name,
      scope:    "view.#{controller_path}.#{@controller_model.name.underscore}",
      default:  model.human_attribute_name(col_name,
          default:  parent_model.human_name))
end

#draw_column_bitset(rec, col_or_sym, bitset) ⇒ Object

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



275
276
277
278
279
280
281
282
# File 'app/helpers/edgarj/assoc_helper.rb', line 275

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

#draw_column_enum(rec, col_or_sym, enum) ⇒ Object

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



309
310
311
# File 'app/helpers/edgarj/assoc_helper.rb', line 309

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

#flag_on?(column_value, bitset, flag) ⇒ Boolean

Is flag in column_value on?

Returns:

  • (Boolean)


245
246
247
248
# File 'app/helpers/edgarj/assoc_helper.rb', line 245

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

#get_bitset(model, col) ⇒ Object

get bitset Module.

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



254
255
256
257
258
259
260
261
262
# File 'app/helpers/edgarj/assoc_helper.rb', line 254

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

#permitted?(requested_flags = 0) ⇒ Boolean

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

Returns:

  • (Boolean)


329
330
331
332
# File 'app/helpers/edgarj/assoc_helper.rb', line 329

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

#permitted_on?(controller) ⇒ Boolean

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

Returns:

  • (Boolean)


335
336
337
# File 'app/helpers/edgarj/assoc_helper.rb', line 335

def permitted_on?(controller)
  true  # TBD
end