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



298
299
300
301
302
303
304
# File 'app/helpers/edgarj/assoc_helper.rb', line 298

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 ”



292
293
294
295
# File 'app/helpers/edgarj/assoc_helper.rb', line 292

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
# File 'app/helpers/edgarj/assoc_helper.rb', line 179

def draw_belongs_to_clear_link(f, col_name, popup_field, parent_name, default_label)
  f.hidden_field(col_name)
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().



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/helpers/edgarj/assoc_helper.rb', line 191

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 = '<span class="edgarj_field_belongs_to_button">選択</span>'.html_safe
  if parent_obj
    link_to(
        label + link_tag,
        popup_path,
        remote: true)
  else
    link_to(
        label + link_tag,
        popup_path,
        remote: true)
  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



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 +
      '<span class="edgarj_field_belongs_to_button">選択</span>'.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



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

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



287
288
289
# File 'app/helpers/edgarj/assoc_helper.rb', line 287

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?



223
224
225
226
# File 'app/helpers/edgarj/assoc_helper.rb', line 223

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.



232
233
234
235
236
237
238
239
240
# File 'app/helpers/edgarj/assoc_helper.rb', line 232

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.



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

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.



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

def permitted_on?(controller)
  true  # TBD
end