Method: CustomTable::ApplicationHelper#custom_table_fields_for

Defined in:
app/helpers/custom_table/application_helper.rb

#custom_table_fields_for(model, variant: nil, current_search: {}, predefined_fields: nil, use_all_fields: false) ⇒ Object

Returns list of fields to show in table according user settings



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'app/helpers/custom_table/application_helper.rb', line 224

def custom_table_fields_for(model, variant: nil, current_search: {}, predefined_fields: nil, use_all_fields: false)
  
  fields = []
  current_search = {} if current_search.nil?
  
  model_fields = custom_table_fields_definition_for(model, variant)
  
  if (!predefined_fields.nil?)
    out = {}
    predefined_fields.each do |f|
      out[f] = model_fields[f]
    end
    return out.compact
    # return model_fields.select {|k,v| predefined_fields.include?(k) }
  end
  
  fields_key = model.model_name.to_s
  fields_key += "-#{variant}" unless variant.nil?
  
  s = custom_table_user_customized_fields_for(model, variant)
 
  use_all_fields = true if params[:custom_table_use_all_fields]

  if use_all_fields
    selected_fields = model_fields.keys
  else
    if !s.nil?
      # Use saved user settings for the model
      always_selected_fields = model_fields.select { |x, y| [:always].include?(y[:appear]) || current_search.keys.include?(x.to_s) }.keys
      always_selected_fields.each {|f| s = {"#{f.to_s}": true}.merge(s) if s[f].nil? }
      selected_fields = s.select{|f,v| v }.keys
    else
      # Use default model settings
      selected_fields = model_fields.select { |x, y| [:always, :default].include?(y[:appear]) || current_search.keys.include?(x.to_s) }.keys
    end
  end
  
  # Filter selection again with model settings for sure
  return selected_fields.collect{|f| [f, model_fields[f]]}.to_h
  
end