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 collaborate 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

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



15
16
17
18
19
20
21
# File 'app/helpers/edgarj/drawer/base.rb', line 15

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.



10
11
12
# File 'app/helpers/edgarj/drawer/base.rb', line 10

def model
  @model
end

#page_infoObject

Returns the value of attribute page_info.



10
11
12
# File 'app/helpers/edgarj/drawer/base.rb', line 10

def page_info
  @page_info
end

#paramsObject

Returns the value of attribute params.



10
11
12
# File 'app/helpers/edgarj/drawer/base.rb', line 10

def params
  @params
end

#vcObject

Returns the value of attribute vc.



10
11
12
# File 'app/helpers/edgarj/drawer/base.rb', line 10

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



37
38
39
# File 'app/helpers/edgarj/drawer/base.rb', line 37

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

#columns_for(column_name_list) ⇒ Object

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

INPUTS

column_name_list

column name list



188
189
190
191
192
193
194
195
196
# File 'app/helpers/edgarj/drawer/base.rb', line 188

def columns_for(column_name_list)
  [].tap do |result|
    for col_name in column_name_list do
      if (col = @model.columns_hash[col_name])
        result << col
      end
    end
  end
end

#draw_form(record) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/helpers/edgarj/drawer/base.rb', line 161

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/helpers/edgarj/drawer/base.rb', line 121

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
      ''.html_safe.tap do |result|
        for col in columns_for(list_columns) do
          result << d.draw_column_header(col)
        end
      end
    end +
    ''.html_safe.tap do |trs|
      for rec in list do
        line_color = 1 - line_color
        d.set_path(rec)
        trs << @vc.(:tr, class: "list_line#{line_color}") do
          ''.html_safe.tap do |cols|
            for col in columns_for(list_columns) do
              cols << d.draw_column(rec, col)
            end
          end
        end
      end
    end
  end
end

#draw_search_form(record) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/helpers/edgarj/drawer/base.rb', line 203

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



63
64
65
# File 'app/helpers/edgarj/drawer/base.rb', line 63

def form_columns
  columns
end

#form_drawer_classObject

overwrite to replace form drawer for the model



157
158
159
# File 'app/helpers/edgarj/drawer/base.rb', line 157

def form_drawer_class
  Edgarj::FormDrawer::Base
end

#fullname(col) ⇒ Object

return table_name + col.name



152
153
154
# File 'app/helpers/edgarj/drawer/base.rb', line 152

def fullname(col)
  @model.table_name + '.' + col.name
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



50
51
52
# File 'app/helpers/edgarj/drawer/base.rb', line 50

def list_columns
  columns
end

#list_drawer_classObject



117
118
119
# File 'app/helpers/edgarj/drawer/base.rb', line 117

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.



91
92
93
94
95
96
97
98
99
# File 'app/helpers/edgarj/drawer/base.rb', line 91

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.



107
108
109
110
111
112
113
114
115
# File 'app/helpers/edgarj/drawer/base.rb', line 107

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



76
77
78
# File 'app/helpers/edgarj/drawer/base.rb', line 76

def search_form_columns
  columns
end

#search_form_drawer_classObject

overwrite to replace form drawer for search



199
200
201
# File 'app/helpers/edgarj/drawer/base.rb', line 199

def search_form_drawer_class
  Edgarj::FormDrawer::Search
end