Class: Hatio::Generators::ResourceViewUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/hatio/util/resource_view_util.rb

Constant Summary collapse

DATA_TYPE_NUMBER =
['integer', 'float', 'long', 'double', 'decimal']
DATA_TYPE_DATE =
['date', 'datetime', 'timestamp']
DATA_TYPE_TEXT =
['string', 'text']
SKIP_COLUMN_NAME =
['id', 'domain_id', 'creator_id', 'updater_id', 'created_at', 'updated_at', 'deleted_at', 'version']

Class Method Summary collapse

Class Method Details

.checkDateColumn(col_type) ⇒ Object

날짜형 컬럼인지 체크



377
378
379
# File 'lib/generators/hatio/util/resource_view_util.rb', line 377

def self.checkDateColumn(col_type)
  return DATA_TYPE_DATE.include?(col_type) ? true : false
end

.checkNumberColumn(col_type) ⇒ Object

number형 컬럼인지 체크



363
364
365
# File 'lib/generators/hatio/util/resource_view_util.rb', line 363

def self.checkNumberColumn(col_type)
  return DATA_TYPE_NUMBER.include?(col_type) ? true : false
end

.checkSkipColumn(col) ⇒ Object

처리를 skip할 columns 정보인지 체크



356
357
358
# File 'lib/generators/hatio/util/resource_view_util.rb', line 356

def self.checkSkipColumn(col)
  return SKIP_COLUMN_NAME.include?(col.name) ? true : false
end

.checkTextColumn(col_type) ⇒ Object

문자형 컬럼인지 체크



370
371
372
# File 'lib/generators/hatio/util/resource_view_util.rb', line 370

def self.checkTextColumn(col_type)
  return DATA_TYPE_TEXT.include?(col_type) ? true : false
end

.find_store_name(domain, column, data_index) ⇒ Object

entity name으로 store명을 추출



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/generators/hatio/util/resource_view_util.rb', line 98

def self.find_store_name(domain, column, data_index)
  entity = Entity.find_by_name(column.ref_name);
    
  if(SKIP_COLUMN_NAME.include?(column.name))
    title = "T('label.#{data_index ? data_index : column.name}')"
  else
    title = data_index ? "T('label.#{data_index}')" : "T('label.#{column.name}')";
  end
    
  ["#{title}", "#{entity.bundle.camelcase}.store.#{entity.name}"]
end

.generate_form(domain, resourceName, columns, options, indent) ⇒ Object

resource의 columns로 부터 form 정보를 생성하여 리턴



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/generators/hatio/util/resource_view_util.rb', line 179

def self.generate_form(domain, resourceName, columns, options, indent)
  output = "items : [\n\t"
  indent = "\t" unless indent

  columns.each do |col|
    columnInfo = ""
    if(col.name == 'id')
      columnInfo = "{ name : '#{col.name}', fieldLabel : T('label.#{col.name}'), hidden : true }"
    elsif(col.name == 'domain_id')
    elsif(col.name == 'creator_id' || col.name == 'updater_id' || col.name == 'created_at' || col.name == 'updated_at')
    elsif(col.ref_type && !col.ref_type.empty?)
      columnInfo = self.get_form_column_by_ref(domain, col)
    else
      columnInfo = self.get_form_column(domain, col)
    end
    output << "\t#{columnInfo},\n\t" if columnInfo && !columnInfo.empty?
  end

  output << "\t{ xtype : 'timestamp' }\n\t]"
  output
end

.generate_grid(domain, singular_name, columns, options, indent) ⇒ Object

columns 정보로 부터 그리드를 생성



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/hatio/util/resource_view_util.rb', line 13

def self.generate_grid(domain, singular_name, columns, options, indent)
  output = "columns : [\n"
  indent = "\t\t" unless indent
  
  if('y' == options.use_ext_prop)
    output << "#{indent}{ xtype : 'actioncolumn', icon : 'assets/std/iconSlideshow.png', itemId : 'slideshow', width : 30, align : 'center' },\n"
  end
    
  columns.each do |col|
    col_info = ""
    
    if(col.name == 'id')
        col_info = "{ header : T('label.id'), dataIndex : 'id', hidden : true }"
    elsif(col.name == 'domain_id')
      next
    else
      # list_rank가 0보다 큰 경우만 grid에 표시
      if((col.list_rank && col.list_rank > 0) || (col.sort_rank && col.sort_rank > 0))
        # 기본 생성, 수정시간 필드는 무조건 disabled 처리 
        if(col.name == 'created_at' || col.name == 'updated_at')
          col_info = "{ header : T('label.#{col.name}'), dataIndex : '#{col.name}', xtype : 'datecolumn', format : T('format.datetime'), width : 120 }"
        # reference 인 경우는 해당 reference에 맞게 처리 
        elsif(col.ref_type && !col.ref_type.empty?)
          col_info = self.get_grid_column_by_ref(domain, col)
        # 일반적인 필드인 경우
        else
          col_info = self.get_grid_column(domain, col)
        end
      end
    end

    output << "#{indent}#{col_info},\n" unless(col_info.empty?)
  end

  output << "\t],"
  output
end

.generate_model_fields(options, columns, indent) ⇒ Object

columns 정보로 부터 model fields 정보를 설정



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/generators/hatio/util/resource_view_util.rb', line 309

def self.generate_model_fields(options, columns, indent)
  indent = "\t\t" unless indent
  validations, output = [], "fields : [\n"
  
  columns.each do |col|
    next if(col.name == 'domain_id')
    output << "#{indent}{ name : '#{col.name}', type : '"
    output << ((col.col_type == 'datetime' || col.col_type == 'timestamp') ? 'date' : col.col_type)
    output << "' },\n"
    
    # ref_type이 Entity이고 ref_name이 _id로 끝나면 ....
    if(col.ref_type && col.ref_type == 'Entity' && col.ref_name)
      if(col.name == 'creator_id')
        output << "#{indent}{ name : 'creator', type : 'auto' },\n"
      elsif(col.name == 'updater_id')
        output << "#{indent}{ name : 'updater', type : 'auto' },\n"
      else
        output << "#{indent}{ name : '#{col.ref_name.underscore}', type : 'auto' },\n"
      end
    end

    unless(SKIP_COLUMN_NAME.include?(col.name))
      if(!col.nullable.nil? && !col.nullable)
        validations << "#{indent}{ type : 'presence', field : '#{col.name}' },\n"
      end
    
      if(col.col_type && (col.col_size && col.col_size > 0))
        validations << "#{indent}{ type : 'length', field : '#{col.name}', max : #{col.col_size} },\n"
      end
    end
  end

  output << "#{indent}{ name : 'properties_attributes', type : 'auto', defaultValue : [] },\n" if('y' == options.use_ext_prop)
  output << "#{indent}{ name : '_cud_flag_', type : 'string' }\n\t]"

  unless(validations.empty?)
    output << ",\n\n\tvalidations : [\n"
    validations.each { |val| output << val }
    output << "\t]"
  end

  output
end

.generate_search_items(domain, resourceName, columns, options, indent) ⇒ Object

columns 정보로 부터 search condition items를 생성



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/generators/hatio/util/resource_view_util.rb', line 247

def self.generate_search_items(domain, resourceName, columns, options, indent)
  output = "items : [\n"
  indent = "\t\t" unless indent
  search_columns = columns.select { |c| c.search_rank && c.search_rank > 0 }.sort_by(&:search_rank)
  
  search_columns.each do |col|
    if(!self.checkSkipColumn(col))
      if(col.ref_type && !col.ref_type.empty?)
        columnInfo = self.get_search_form_column_by_ref(domain, col)
      else
        columnInfo = self.get_search_form_column(domain, col)
      end
      output << "#{indent}\t#{columnInfo},\n" if columnInfo && !columnInfo.empty?
    end
  end
    
  output << "#{indent}]"
  output
end

.get_form_column(domain, col) ⇒ Object

column으로 부터 form column 정보를 얻는다.



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 'lib/generators/hatio/util/resource_view_util.rb', line 219

def self.get_form_column(domain, col)
  col_type = col.col_type
  if(col_type == 'string')
    allow_blank_str = (!col.nullable.nil? && !col.nullable) ? ", allowBlank : false" : ""
    col_size_str = (col.col_size && col.col_size > 0) ? ", maxLength : #{col.col_size}" : ""
    "{ name : '#{col.name}', fieldLabel : T('label.#{col.name}')#{allow_blank_str}#{col_size_str} }"
  elsif(col_type == 'text')
    allow_blank_str = (!col.nullable.nil? && !col.nullable) ? ", allowBlank : false" : ""
    col_size_str = (col.col_size && col.col_size > 0) ? ", maxLength : #{col.col_size}" : ""
    "{ name : '#{col.name}', fieldLabel : T('label.#{col.name}'), xtype : 'textareafield'#{allow_blank_str}#{col_size_str} }"
  elsif(col_type == 'boolean')
    "{ name : '#{col.name}', fieldLabel : T('label.#{col.name}'), xtype : 'checkboxfield', inputValue : true }"
  elsif(col_type == 'date')
    "{ name : '#{col.name}', fieldLabel : T('label.#{col.name}'), xtype : 'datefield', format : T('format.date'), submitFormat : T('format.submitDate') }"
  elsif(col_type == 'datetime' || col_type == 'timestamp')
    "{ name : '#{col.name}', fieldLabel : T('label.#{col.name}'), xtype : 'datefield', format : T('format.datetime') }"
  elsif(checkNumberColumn(col_type))
    min_val_str = col.min.nil? ? "" : ", minValue : #{col.min}"
    max_val_str = col.max.nil? ? "" : ", maxValue : #{col.max}"
    "{ name : '#{col.name}', fieldLabel : T('label.#{col.name}'), xtype : 'numberfield'#{min_val_str}#{max_val_str} }"
  else
    ""
  end
end

.get_form_column_by_ref(domain, col) ⇒ Object

column의 reference type정보로 부터 form column 정보를 얻는다.



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/generators/hatio/util/resource_view_util.rb', line 204

def self.get_form_column_by_ref(domain, col)
  if(col.ref_type == 'CommonCode')
    return "{ name : '#{col.name}', fieldLabel : T('label.#{col.name}'), xtype : 'codefield', commonCode : '#{col.ref_name}' }"
  elsif(col.ref_type == 'Entity')
    entity_name = col.name[0 .. col.name.rindex('_') - 1]
    storeName = find_store_name(domain, col, entity_name)
    return "{ name : '#{entity_name}', fieldLabel : #{storeName[0]}, xtype : 'entityfield', storeClass : '#{storeName[1]}' }"
  else
    return ""
  end
end

.get_grid_column(domain, col) ⇒ Object

column으로 부터 grid column 정보를 얻는다.



113
114
115
116
117
118
119
120
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
# File 'lib/generators/hatio/util/resource_view_util.rb', line 113

def self.get_grid_column(domain, col)
  output, col_type = "", col.col_type
  
  if(col_type == 'string')
    output = "{ header : T('label.#{col.name}'), dataIndex : '#{col.name}' "
    output << grid_column_editor(col) if(col.editable)
    output << " }"
  elsif(col_type == 'text')
    output = "{ header : T('label.#{col.name}'), dataIndex : '#{col.name}' "
    output << grid_column_editor(col) if(col.editable)
    output << " }"
  elsif(col_type == 'boolean')
    output = "{ header : T('label.#{col.name}'), dataIndex : '#{col.name}' "
    output << ", xtype : 'checkcolumn' "
    output << " }"
  elsif(col_type == 'date')
    if(col.editable)
      output = grid_column_editor(col) if(col.editable)
    else
      output = "{ header : T('label.#{col.name}'), dataIndex : '#{col.name}', xtype : 'datecolumn', format : T('format.date') }"
    end
  elsif(col_type == 'datetime' || col_type == 'timestamp')
    if(col.editable)
      output = grid_column_editor(col) if(col.editable)
    else
      output = "{ header : T('label.#{col.name}'), dataIndex : '#{col.name}', xtype : 'datecolumn', format : T('format.datetime') }"
    end
  elsif(checkNumberColumn(col_type))
    output = "{ header : T('label.#{col.name}'), dataIndex : '#{col.name}', align : 'right' "
    output << grid_column_editor(col) if(col.editable)
    output << " }"
  end
  
  return output
end

.get_grid_column_by_ref(domain, column) ⇒ Object

column의 reference type정보로 부터 grid column 정보를 얻는다.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/generators/hatio/util/resource_view_util.rb', line 73

def self.get_grid_column_by_ref(domain, column)
  columnInfo = ""
  if(column.ref_type == 'CommonCode')
    columnInfo << "{ header : T('label.#{column.name}'), dataIndex : '#{column.name}'"
    columnInfo << ", editor : { xtype : 'codecombo', commonCode : '#{column.ref_name}' }" if(column.editable)
    columnInfo << " }"
  elsif(column.ref_type == 'Entity')
    if(column.name.end_with?('_id') && column.ref_name)
      data_index = column.name[0 .. column.name.rindex('_') - 1]
      storeName = find_store_name(domain, column, data_index)
      columnInfo << "{ header : #{storeName[0]}, dataIndex : '#{data_index}', xtype : 'entitycolumn'"
      columnInfo << ", editor : { xtype : 'entitycolumneditor', storeClass : '#{storeName[1]}' }"  if(column.editable)
      columnInfo << " }"
    else
      columnInfo << get_grid_column(domain, column)
    end
  else
    columnInfo << get_grid_column(domain, column)
  end
  columnInfo
end

.get_search_form_column(domain, col) ⇒ Object

column의 정보로 부터 search form column 정보를 얻는다.



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/generators/hatio/util/resource_view_util.rb', line 285

def self.get_search_form_column(domain, col)
  col_type = col.col_type
  if(col.name.end_with?('_yn'))
    return "{ name : '#{col.name}-eq', fieldLabel : T('label.#{col.name}'), xtype : 'yesnopicker' }"
  elsif(checkTextColumn(col_type))
    return "{ name : '#{col.name}-like', fieldLabel : T('label.#{col.name}')}"
  elsif(col_type == 'boolean')
    return "{ name : '#{col.name}-eq', fieldLabel : T('label.#{col.name}'), xtype : 'checkboxfield', inputValue : true }"
  elsif(col_type == 'date')
    return "{ name : '#{col.name}-eq', fieldLabel : T('label.date'), xtype : 'datefield', format : T('format.date'), submitFormat : T('format.submitDate') }"
  elsif(col_type == 'datetime' || col_type == 'timestamp')
    output = "{ name : '#{col.name}-gte', fieldLabel : T('label.#{col.name}_from'), xtype : 'datefield', format : T('format.datetime') },\n"
    output << "{ name : '#{col.name}-lte', fieldLabel : T('label.#{col.name}_to'), xtype : 'datefield', format : T('format.datetime') }"
    return output
  elsif(checkNumberColumn(col_type))
    return "{ name : '#{col.name}-eq', fieldLabel : T('label.#{col.name}'), xtype : 'numberfield' }"
  else
    return ""
  end
end

.get_search_form_column_by_ref(domain, col) ⇒ Object

column의 reference type정보로 부터 search form column 정보를 얻는다.



270
271
272
273
274
275
276
277
278
279
280
# File 'lib/generators/hatio/util/resource_view_util.rb', line 270

def self.get_search_form_column_by_ref(domain, col)
  if(col.ref_type == 'CommonCode')
    return "{ name : '#{col.name}-eq', fieldLabel : T('label.#{col.name}'), xtype : 'codesearchcombo', commonCode : '#{col.ref_name}', valueField : 'name', displayField : 'name' }"
  elsif(col.ref_type == 'Entity')
    data_label = col.name[0 .. col.name.rindex('_') - 1]
    storeName = find_store_name(domain, col, data_label)
    return "{ name : '#{data_label}.name-eq', fieldLabel : #{storeName[0]}, xtype : 'entitysearchcombo', storeClass : '#{storeName[1]}', valueField : 'name' }"
  else
    return ""
  end
end

.grid_column_editor(col) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/generators/hatio/util/resource_view_util.rb', line 149

def self.grid_column_editor(col)
  col_type, output = col.col_type, ""
  
  if(col_type == 'string' || col_type == 'text')
    output << ", editor : { xtype : 'textfield' "
  elsif(col_type == 'date')
    output = "{ header : T('label.#{col.name}'), dataIndex : '#{col.name}', xtype : 'datecolumn', format : T('format.date'), editor : { xtype : 'datefield', format : T('format.date') } }"
  elsif(col_type == 'datetime' || col_type == 'timestamp')
    output = "{ header : T('label.#{col.name}'), dataIndex : '#{col.name}', editor : { xtype : 'datefield', format : T('format.datetime') } }"
  elsif(checkNumberColumn(col_type))
    output << ", editor : { xtype : 'numberfield' "
  end
  
  if(col_type == 'string' || col_type == 'text')
    output << ", maxLength : #{col.col_size} " if(col.col_size > 0)
  end
  
  if(checkNumberColumn(col_type))
    output << ", minValue : #{col.min} " if(!col.min.nil?)
    output << ", maxValue : #{col.max} " if(!col.max.nil?)
  end
  
  output << "}" if(col_type == 'string' || col_type == 'text')
  output << "}" if(checkNumberColumn(col_type))
  output
end

.grid_sort_option(columns, indent) ⇒ Object

grid column 정보에 sort option을 추가한다.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/generators/hatio/util/resource_view_util.rb', line 54

def self.grid_sort_option(columns, indent)
  sort_columns = columns.select{ |f| f.sort_rank && f.sort_rank > 0 }.sort_by(&:sort_rank)
  column_info = ""
  
  if(!sort_columns.empty?)
    column_info << "#{indent}sorters : [\n"
    sort_columns.each do |scol| 
      dir = scol.reverse_sort ? 'desc' : 'asc'
      column_info << "#{indent}\t{ property : '#{scol.name}', direction : '#{dir}' },\n" 
    end
    column_info << "#{indent}],"
  end
  
  column_info
end