Class: Edgarj::Drawer::Base

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/edgarj/drawer/base.rb

Overview

‘Mediator’ to draw list and form of the model on the view.

This collaborates with the following sub classes:

Edgarj::ListDrawer::Normal

for list

Edgarj::FormDrawer::Base

for data entry form

Edgarj::FormDrawer::Search

for search form

Direct Known Subclasses

Normal, Popup

Defined Under Namespace

Classes: ColumnInfoCache

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context, params, page_info, model, options = {}) ⇒ Base

  • options

    • list_drawer_options - options for Edgarj::ListDrawer::Normal

    • draw_form_options - options for draw_form_options



215
216
217
218
219
220
221
# File 'app/helpers/edgarj/drawer/base.rb', line 215

def initialize(view_context, params, page_info, model, options={})
  @vc         = view_context
  @params     = params
  @page_info  = page_info
  @model      = model
  @options    = options.dup
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



210
211
212
# File 'app/helpers/edgarj/drawer/base.rb', line 210

def model
  @model
end

#page_infoObject

Returns the value of attribute page_info.



210
211
212
# File 'app/helpers/edgarj/drawer/base.rb', line 210

def page_info
  @page_info
end

#paramsObject

Returns the value of attribute params.



210
211
212
# File 'app/helpers/edgarj/drawer/base.rb', line 210

def params
  @params
end

#vcObject

Returns the value of attribute vc.



210
211
212
# File 'app/helpers/edgarj/drawer/base.rb', line 210

def vc
  @vc
end

Instance Method Details

#columnsObject

define model-wide default columns for view.

If you need to customize, overwrite it at derived model class. Example:

def columns
  %w(id name email updated_at)
end

SEE ALSO

list_columns

define list columns

form_columns

define form columns

search_form_columns

define search form columns



237
238
239
# File 'app/helpers/edgarj/drawer/base.rb', line 237

def columns
  @model.columns.map{|c| c.name}
end

#columns_for(column_name_list, kind = :default) ⇒ Object

return array of model columns (ActiveRecord::ConnectionAdapters::X_Column type).

INPUTS

column_name_list

column name list

kind

:list, :form, or :search_form



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'app/helpers/edgarj/drawer/base.rb', line 422

def columns_for(column_name_list, kind = :default)
  if (val = ColumnInfoCache.instance.presence(@vc.controller.class, kind))
    val
  else
    #Rails.logger.debug("ColumnInfoCache non-cached access for (#{@vc.controller.class.name} x #{kind})")
    ColumnInfoCache.instance.set(@vc.controller.class, kind,
        [].tap do |result|
          for col_name in column_name_list do
            result <<
                if col_name.is_a?(ColumnInfo::Base)
                  col_name
                elsif (col = @model.columns_hash[col_name])
                  if (parent = @model.belongs_to_AR(col))
                    ColumnInfo::BelongsTo.new(@model, col_name, parent, false)
                  else
                    ColumnInfo::Normal.new(@model, col_name)
                  end
                end
          end
        end)
  end
end

#draw_form(record) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'app/helpers/edgarj/drawer/base.rb', line 365

def draw_form(record)
  url_hash = {
    controller: @params[:controller],
    action:     record.new_record? ? 'create' : 'update',
  }
  url_hash[:id] = record.id if record.persisted?
  @vc.draw_form_buttons(@options[:draw_form_options] || {}) +
  @vc.form_for(record,
      remote: true,
      url:    url_hash,
      html:   {
          id:         '_edgarj_form',
          method:     record.new_record? ? 'post' : 'put',
          multipart:  true,
         #target:     'edgarj_form_frame'
      }) do |f|
    form_drawer_class.new(self, record, f).draw() +

    # to avoid submit on 1-textfield form when hit [ENTER] key
    '<input type="text" name="dummy" style="visibility:hidden" size=0>'.html_safe
  end
end

#draw_list(list) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'app/helpers/edgarj/drawer/base.rb', line 333

def draw_list(list)
  @line_color = 1
  d           = list_drawer_class.new(
      self,
      @options[:list_drawer_options] || {})

  @vc.(:table, width: '100%', class: 'list') do
    @vc.(:tr) do
      for col in columns_for(list_columns, :list) do
        @vc.concat d.draw_column_header(col)
      end
    end +
    @vc.capture do
      for rec in list do
        @line_color = 1 - @line_color
        @vc.concat(draw_row(rec) do
          @vc.capture do
            for col in columns_for(list_columns, :list) do
              @vc.concat d.draw_column(rec, col)
            end
          end
        end)
      end
    end
  end
end

#draw_row(record, &block) ⇒ Object



325
326
327
328
329
330
331
# File 'app/helpers/edgarj/drawer/base.rb', line 325

def draw_row(record, &block)
  @vc.(:tr,
      class:  "list_line#{@line_color} edgarj_row",
      data:   {url: url_for_show(record)}) do
    yield
  end
end

#draw_search_form(record) ⇒ Object



450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'app/helpers/edgarj/drawer/base.rb', line 450

def draw_search_form(record)
  @vc.form_for(record, url: {action: 'search'}, html: {
      id:     '_edgarj_search_form',
      remote: true,
      method: :get}) do |f|
    f.fields_for(record._operator) do |o|
      search_form_drawer_class.new(self, record, f, o).draw()
    end +

    # to avoid submit on 1-textfield form when hit [ENTER] key
    '<input type="text" name="dummy" style="visibility:hidden" size=0>'.html_safe
  end
end

#form_columnsObject

This defines form columns. You can overwrite this method at each model if it is different from columns. Default is calling columns().

SEE ALSO

columns

define default columns

list_columns

define list columns

search_form_columns

define search form columns



263
264
265
# File 'app/helpers/edgarj/drawer/base.rb', line 263

def form_columns
  columns
end

#form_drawer_classObject

overwrite to replace form drawer for the model



361
362
363
# File 'app/helpers/edgarj/drawer/base.rb', line 361

def form_drawer_class
  Edgarj::FormDrawer::Base
end

#list_columnsObject

This defines list columns. You can overwrite this method at each model if it is different from columns. Default is calling columns().

SEE ALSO

columns

define default columns

form_columns

define form columns

search_form_columns

define search form columns



250
251
252
# File 'app/helpers/edgarj/drawer/base.rb', line 250

def list_columns
  columns
end

#list_drawer_classObject



317
318
319
# File 'app/helpers/edgarj/drawer/base.rb', line 317

def list_drawer_class
  Edgarj::ListDrawer::Normal
end

This defines popup path for the column on the model.

Default returns parent model’s popup-controller. For example, book.author_id -> ‘authors_popup’ path

You can overwrite this method at each model if it is different from columns.



291
292
293
294
295
296
297
298
299
# File 'app/helpers/edgarj/drawer/base.rb', line 291

def popup_path(col)
  parent_model = @model.belongs_to_AR(col)
  raise 'Parent is nil' if !parent_model

  popup_field = PopupHelper::PopupField.new_builder(@model.model_name.param_key, col.name)
  @vc.main_app.url_for(
      controller: parent_model.model_name.collection + '_popup',
      id_target:  popup_field.id_target)
end

This defines popup path for the search column on the model.

Default returns parent model’s popup-controller with id_target on the search column.



307
308
309
310
311
312
313
314
315
# File 'app/helpers/edgarj/drawer/base.rb', line 307

def popup_path_on_search(col)
  parent_model = @model.belongs_to_AR(col)
  raise 'Parent is nil' if !parent_model

  popup_field = PopupHelper::PopupField.new_builder(Edgarj::SearchForm.model_name.param_key, col.name)
  @vc.main_app.url_for(
      controller: parent_model.model_name.collection + '_popup',
      id_target:  popup_field.id_target)
end

#search_form_columnsObject

This defines search-form columns. You can overwrite this method at each model if it is different from columns. Default is calling columns().

SEE ALSO

columns

define default columns

list_columns

define list columns

form_columns

define form columns



276
277
278
# File 'app/helpers/edgarj/drawer/base.rb', line 276

def search_form_columns
  columns
end

#search_form_drawer_classObject

overwrite to replace form drawer for search



446
447
448
# File 'app/helpers/edgarj/drawer/base.rb', line 446

def search_form_drawer_class
  Edgarj::FormDrawer::Search
end

#url_for_show(record) ⇒ Object



321
322
323
# File 'app/helpers/edgarj/drawer/base.rb', line 321

def url_for_show(record)
  @vc.url_for(action: 'show', id: record.id, format: :js)
end