Module: CustomTable::ApplicationHelper
- Defined in:
- app/helpers/custom_table/application_helper.rb
Instance Method Summary collapse
-
#amount(c) ⇒ Object
Base.
-
#amount_color(c) ⇒ Object
Colored.
-
#amount_round(c) ⇒ Object
Rounded.
-
#amount_round_color(c) ⇒ Object
Colored rounded.
-
#amount_value(c, p = 2) ⇒ Object
Abstract.
-
#current_user_has_customizable_fields_for?(model, variant = nil) ⇒ Boolean
Returns true if model can be customized by current user at least by one field.
- #custom_table_batch_actions(model) ⇒ Object
- #custom_table_batch_fields(model) ⇒ Object
- #custom_table_batch_ids(items) ⇒ Object
- #custom_table_batch_selector_check_box(item, **params) ⇒ Object
- #custom_table_customizable_fields_for(model, variant = nil) ⇒ Object
- #custom_table_data(collection, variant = nil, **params) ⇒ Object
- #custom_table_download_button(collection, **p) ⇒ Object
-
#custom_table_fields_definition_for(model, variant = nil) ⇒ Object
Base definition for model.
-
#custom_table_fields_definition_for_field(model, field, variant = nil) ⇒ Object
Base definition for model.
-
#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.
- #custom_table_fields_key(model, variant = nil) ⇒ Object
-
#custom_table_fields_list_to_definitions(model, fields) ⇒ Object
Gets array of fields and results in object of available fields and definitions from fields from list.
-
#custom_table_fields_settings_for(model, variant: nil) ⇒ Object
Returns list of fields for customization form.
- #custom_table_fields_totals(fields:, totals:, item:, fields_totals:, variant:) ⇒ Object
- #custom_table_filter(search_model, variant = nil, **params, &block) ⇒ Object
-
#custom_table_form_for(record, options = {}, &block) ⇒ Object
MOVE TO GENERATED HELPER def boolean_icon(value, true_value = nil, false_value = nil) capture do concat content_tag(:i, “”, class: (value ? “bi bi-check-lg text-success” : “bi bi-x-lg text-danger”), data: value) concat content_tag(:span, value ? true_value : false_value, class: “ms-1”) unless true_value.nil? end end.
-
#custom_table_row_data(item, variant = nil, **params) ⇒ Object
Data for updating values via turbo according object id and field name.
- #custom_table_settings(search_model, variant = nil, **params) ⇒ Object
- #custom_table_settings_button(search_model, variant = nil, size: "sm") ⇒ Object
- #custom_table_totals(collection, fields, totals = {}, fields_totals = {}) ⇒ Object
-
#custom_table_user_customization_for(model, variant = nil) ⇒ Object
Prepares object of user search customization.
-
#custom_table_user_customized_fields_for(model, variant = nil) ⇒ Object
Prepares object of user fields customization.
- #custom_table_variants_for(model) ⇒ Object
-
#field_value_for(item, field, definitions: nil, variant: nil) ⇒ Object
Tries to fetch attribute value by all possible ways (by priority): Helper singular_model_name_field_field Helper singular_model_name_field Helper singular_model_name_field_raw Attribute of model.
- #fields_table_for(collection, options = {}, &block) ⇒ Object
-
#raw_field_value_for(item, field, definitions: nil, variant: nil) ⇒ Object
Same as above but for Export only.
- #tree_opener(item_id, has_children = false, expanded = false) ⇒ Object
Instance Method Details
#amount(c) ⇒ Object
Base
416 417 418 |
# File 'app/helpers/custom_table/application_helper.rb', line 416 def amount(c) content_tag(:span, amount_value(c), "data-raw": (c.blank? ? nil : c.round(2))) end |
#amount_color(c) ⇒ Object
Colored
426 427 428 |
# File 'app/helpers/custom_table/application_helper.rb', line 426 def amount_color(c) content_tag(:span, amount(c), class: ["amount", (c.to_f >= 0 ? "positive" : "negative")]) end |
#amount_round(c) ⇒ Object
Rounded
411 412 413 |
# File 'app/helpers/custom_table/application_helper.rb', line 411 def amount_round(c) content_tag(:span, amount_value(c, 0), "data-raw": (c.blank? ? nil : c.round)) end |
#amount_round_color(c) ⇒ Object
Colored rounded
431 432 433 |
# File 'app/helpers/custom_table/application_helper.rb', line 431 def amount_round_color(c) content_tag(:span, amount_round(c), class: ["amount", (c.to_f >= 0 ? "positive" : "negative")]) end |
#amount_value(c, p = 2) ⇒ Object
Abstract
421 422 423 |
# File 'app/helpers/custom_table/application_helper.rb', line 421 def amount_value(c, p = 2) number_to_currency(c, precision: p, locale: :en, unit: "") end |
#current_user_has_customizable_fields_for?(model, variant = nil) ⇒ Boolean
Returns true if model can be customized by current user at least by one field
368 369 370 371 |
# File 'app/helpers/custom_table/application_helper.rb', line 368 def current_user_has_customizable_fields_for?(model, variant=nil) return false if current_user.nil? custom_table_customizable_fields_for(model, variant).count.positive? end |
#custom_table_batch_actions(model) ⇒ Object
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'app/helpers/custom_table/application_helper.rb', line 516 def custom_table_batch_actions model item = model.new forms = [] forms.push (form_for(item, url: [:batch_edit, item.model_name.plural.to_sym], params: "ids[]", html: {class: "d-inline me-1"}, method: :post, "target": "remote-modal", data: {"turbo-frame": "remote-modal", "batch-actions-target": "form", "action": "batch-actions#submit"}) do |f| concat(f.submit t("custom_table.batch_update"), class: "btn btn-success btn-sm my-1") end) forms.push (form_for(item, url: [:batch_destroy, item.model_name.plural.to_sym], params: "ids[]", html: {class: "d-inline"}, method: :delete, data: {"turbo-confirm": t("are_you_sure"), "batch-actions-target": "form", "action": "batch-actions#submit"}) do |f| concat(f.submit t("custom_table.batch_destroy"), class: "btn btn-danger btn-sm my-1") end) forms.join("").html_safe end |
#custom_table_batch_fields(model) ⇒ Object
512 513 514 |
# File 'app/helpers/custom_table/application_helper.rb', line 512 def custom_table_batch_fields model custom_table_fields_definition_for(model).select{|f, d| d[:batch] == true}.keys end |
#custom_table_batch_ids(items) ⇒ Object
501 502 503 504 505 506 507 508 509 510 |
# File 'app/helpers/custom_table/application_helper.rb', line 501 def custom_table_batch_ids items return if items.nil? || items.length == 0 capture do items.each do |item| concat hidden_field_tag("#{item.model_name.plural}[]", item.id) end concat content_tag(:p, t("custom_table.batch_selected_items_count", count: items.length)) concat content_tag(:p, t("custom_table.batch_action_description")) end end |
#custom_table_batch_selector_check_box(item, **params) ⇒ Object
480 481 482 483 484 485 486 487 488 |
# File 'app/helpers/custom_table/application_helper.rb', line 480 def custom_table_batch_selector_check_box item, **params # abort params.inspect params[:param] = "#{item.model_name.plural}[]" if params[:param].nil? params[:data] = {"toggle-target": "checkbox", "batch-actions-target": "checkbox", "action": "toggle#recalculateToggler batch-actions#refresh"} check_box_tag params[:param], item.id, (!params[item.model_name.plural.to_sym].blank?) && (params[item.model_name.plural.to_sym].include?(item.id.to_s)), params end |
#custom_table_customizable_fields_for(model, variant = nil) ⇒ Object
320 321 322 323 |
# File 'app/helpers/custom_table/application_helper.rb', line 320 def custom_table_customizable_fields_for(model, variant = nil) model_fields = custom_table_fields_definition_for(model, variant) model_fields.reject {|k,v| [:always, :export, :never].include?(v[:appear]) } end |
#custom_table_data(collection, variant = nil, **params) ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'app/helpers/custom_table/application_helper.rb', line 373 def custom_table_data collection, variant=nil, **params params[:collection] = collection params[:variant] = variant params[:paginate] = true if params[:paginate]!=false params[:last_page] = true if params[:last_page]!=false params[:namespace] = (controller.class.module_parent == Object) ? nil : controller.class.module_parent.to_s.underscore.to_sym params[:modal_edit] = true if params[:modal_edit].nil? params[:with_select] = true if params[:with_select].nil? && params[:batch_actions] params[:batch_activator] = true if params[:batch_activator].nil? && params[:batch_actions] === true render "custom_table/table", params do yield end end |
#custom_table_download_button(collection, **p) ⇒ Object
435 436 437 438 439 440 |
# File 'app/helpers/custom_table/application_helper.rb', line 435 def collection, **p p[:collection] = collection p[:downloads] ||= {} p[:downloads].unshift({title: t("custom_table.download_as_csv"), href: params.permit!.merge({:format => :csv})}) if p[:csv] render "custom_table/download", p end |
#custom_table_fields_definition_for(model, variant = nil) ⇒ Object
Base definition for model
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'app/helpers/custom_table/application_helper.rb', line 326 def custom_table_fields_definition_for(model, variant = nil) helper_name = "#{model.model_name.singular}_custom_table_fields" if (! self.class.method_defined?(helper_name)) helper_name = "#{model.model_name.element}_custom_table_fields" # Removing namespace from model if (! self.class.method_defined?(helper_name)) raise "#{helper_name} helper is not defined so we do not know how to render custom_table for #{model}" end end if variant.nil? || method(helper_name).parameters.empty? defs = self.send("#{helper_name}") else defs = self.send("#{helper_name}", variant) end return defs.each{|x,y| y[:label] = model.human_attribute_name(x) if y[:label].nil? } end |
#custom_table_fields_definition_for_field(model, field, variant = nil) ⇒ Object
Base definition for model
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'app/helpers/custom_table/application_helper.rb', line 348 def custom_table_fields_definition_for_field(model, field, variant = nil) helper_name = "#{model.model_name.singular}_custom_table_fields" if (! self.class.method_defined?(helper_name)) raise "#{helper_name} helper is not defined so we do not know how to render custom_table for #{model}" end if variant.nil? || method(helper_name).parameters.empty? defs = self.send("#{helper_name}") else defs = self.send("#{helper_name}", variant) end return nil if defs[field].nil? defs = defs[field] defs[:label] = model.human_attribute_name(field) if defs[:label].nil? return defs end |
#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 |
#custom_table_fields_key(model, variant = nil) ⇒ Object
314 315 316 317 318 |
# File 'app/helpers/custom_table/application_helper.rb', line 314 def custom_table_fields_key(model, variant = nil) fields_key = model.model_name.to_s fields_key += "-#{variant}" unless variant.nil? return fields_key end |
#custom_table_fields_list_to_definitions(model, fields) ⇒ Object
Gets array of fields and results in object of available fields and definitions from fields from list
491 492 493 494 495 496 497 498 499 |
# File 'app/helpers/custom_table/application_helper.rb', line 491 def custom_table_fields_list_to_definitions model, fields d = custom_table_fields_definition_for(model) out = {} fields.each do |f| found = d.find {|k, v| k == f } out[f] = found[1] if !found.nil? end return out end |
#custom_table_fields_settings_for(model, variant: nil) ⇒ Object
Returns list of fields for customization form
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'app/helpers/custom_table/application_helper.rb', line 267 def custom_table_fields_settings_for(model, variant: nil) model_fields = custom_table_fields_definition_for(model, variant) model_fields = model_fields.reject {|k,v| [:export, :never].include?(v[:appear]) } fields_key = model.model_name.to_s fields_key += "-#{variant}" unless variant.nil? user_customization = custom_table_user_customized_fields_for(model, variant) if !user_customization.nil? # Add new fields at the top to user customization if not present model_fields.each do |f, v| next if !user_customization[f].nil? selected = [:always, :default].include?(v[:appear]) new_field = {"#{f.to_s}": selected} if selected user_customization = new_field.merge(user_customization) else user_customization = user_customization.merge(new_field) end end return user_customization.reject{|f,v| model_fields[f].nil?}.collect{|f,v| [f, {selected: v}.merge(model_fields[f])]}.to_h else # Use default model settings # abort model_fields.transform_values!{|f| f[:selected] = true}.inspect return model_fields.each{|k,f| f[:selected] = [:always, :default].include?(f[:appear])} end end |
#custom_table_fields_totals(fields:, totals:, item:, fields_totals:, variant:) ⇒ Object
470 471 472 473 474 475 476 477 478 |
# File 'app/helpers/custom_table/application_helper.rb', line 470 def custom_table_fields_totals fields:, totals:, item:, fields_totals:, variant: fields.each do |field, defs| if !totals.nil? && totals.has_key?(field) && totals[field].nil? # Auto-counting fields_totals[field] = 0 if fields_totals[field].nil? fields_totals[field] += raw_field_value_for(item, field, definitions: defs, variant: variant).to_f rescue 0 end end return fields_totals end |
#custom_table_filter(search_model, variant = nil, **params, &block) ⇒ Object
401 402 403 404 405 406 407 408 |
# File 'app/helpers/custom_table/application_helper.rb', line 401 def custom_table_filter search_model, variant=nil, **params, &block params[:search_model] = search_model params[:variant] = variant render "custom_table/filter", params do |f| yield if !block.nil? end end |
#custom_table_form_for(record, options = {}, &block) ⇒ Object
MOVE TO GENERATED HELPER def boolean_icon(value, true_value = nil, false_value = nil)
capture do
concat content_tag(:i, "", class: (value ? "bi bi-check-lg text-success" : "bi bi-x-lg text-danger"), data: {raw: value})
concat content_tag(:span, value ? true_value : false_value, class: "ms-1") unless true_value.nil?
end
end
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/helpers/custom_table/application_helper.rb', line 12 def custom_table_form_for(record, = {}, &block) [:url] = request.path if [:url].nil? [:method] = :get [:html] ||= {} [:html][:class] = "row row-cols-sm-auto g-3 align-items-center custom-table-filter" [:wrapper] = [:wrapper] || :ct_inline_form [:wrapper_mappings] = { boolean: :ct_inline_boolean, check_boxes: :ct_vertical_collection, radio_buttons: :ct_vertical_collection, date: :ct_inline_element, select: :ct_inline_select } simple_form_for(record, , &block) end |
#custom_table_row_data(item, variant = nil, **params) ⇒ Object
Data for updating values via turbo according object id and field name
390 391 392 393 394 395 396 397 398 399 |
# File 'app/helpers/custom_table/application_helper.rb', line 390 def custom_table_row_data item, variant = nil, **params params[:item] = item params[:variant] = variant params[:namespace] = (controller.class.module_parent == Object) ? nil : controller.class.module_parent.to_s.underscore.to_sym render "custom_table/table_row_data", params do yield end end |
#custom_table_settings(search_model, variant = nil, **params) ⇒ Object
442 443 444 445 446 447 448 449 |
# File 'app/helpers/custom_table/application_helper.rb', line 442 def custom_table_settings search_model, variant=nil, **params params[:search_model] = search_model params[:variant] = variant render "custom_table/settings", params end |
#custom_table_settings_button(search_model, variant = nil, size: "sm") ⇒ Object
451 452 453 454 455 |
# File 'app/helpers/custom_table/application_helper.rb', line 451 def search_model, variant=nil, size: "sm" link_to custom_table.edit_setting_path(search_model.model_name, variant: variant), :class => "btn btn-outline-primary btn-#{size}", data: {"turbo-frame": "remote-modal"} do custom_table_settings_icon end end |
#custom_table_totals(collection, fields, totals = {}, fields_totals = {}) ⇒ Object
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'app/helpers/custom_table/application_helper.rb', line 119 def custom_table_totals collection, fields, totals = {}, fields_totals = {} out = {} totals = {} if totals.nil? fields_totals = {} if fields_totals.nil? fields.keys.each do |field| # We only work if field total is enabled explicitly with definition or totals next if !fields[field][:total] && !totals.has_key?(field) # Taking pre-defined value if available if !totals[field].nil? out[field] = totals[field] else model_class = collection.model # Trying to find helper first model_name = model_class.model_name.singular helper = "#{model_name}_#{field}_total" if self.class.method_defined?(helper) # Better use helper! out[field] = self.send(helper, collection.except(:limit, :offset, :order, :group)) else if collection.respond_to?(:total_pages) && (!model_class.columns_hash[field.to_s].nil? || !fields[field][:total_scope].nil?) # We can try to sum value from database if fields[field][:total_scope].nil? out[field] = collection.except(:limit, :offset, :order, :group).distinct(false).sum(field) else out[field] = collection.except(:limit, :offset, :order, :group).distinct(false).send(fields[field][:total_scope]) end else # Taking simple summed value because all data is shown out[field] = fields_totals[field] end end end end out end |
#custom_table_user_customization_for(model, variant = nil) ⇒ Object
Prepares object of user search customization
308 309 310 311 312 |
# File 'app/helpers/custom_table/application_helper.rb', line 308 def custom_table_user_customization_for(model, variant = nil) fields_key = custom_table_fields_key(model, variant) return nil if current_user.nil? || current_user.custom_table.nil? return current_user.custom_table[fields_key]&.symbolize_keys end |
#custom_table_user_customized_fields_for(model, variant = nil) ⇒ Object
Prepares object of user fields customization
299 300 301 302 303 304 305 |
# File 'app/helpers/custom_table/application_helper.rb', line 299 def custom_table_user_customized_fields_for(model, variant = nil) fields_key = custom_table_fields_key(model, variant) defs = custom_table_fields_definition_for(model, variant) return nil if current_user.nil? || current_user.custom_table.nil? || current_user.custom_table.dig(fields_key, :fields).nil? return current_user.custom_table.dig(fields_key, :fields).symbolize_keys.reject{|k,v| defs[k.to_sym].nil?} end |
#custom_table_variants_for(model) ⇒ Object
457 458 459 460 461 |
# File 'app/helpers/custom_table/application_helper.rb', line 457 def custom_table_variants_for model helper_name = "#{model.model_name.singular}_custom_table_variants" return self.send(helper_name) if self.class.method_defined?(helper_name) return [] end |
#field_value_for(item, field, definitions: nil, variant: nil) ⇒ Object
Tries to fetch attribute value by all possible ways (by priority): Helper singular_model_name_field_field Helper singular_model_name_field Helper singular_model_name_field_raw Attribute of model
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/helpers/custom_table/application_helper.rb', line 38 def field_value_for item, field, definitions: nil, variant: nil defs = definitions model_name = item.model_name.singular global_model_name = item.class.model_name.singular # non-model helpers = [] helpers += [defs[:helper]] if !defs.nil? helpers += [ "#{model_name}_#{variant}_#{field}_field", "#{model_name}_#{variant}_#{field}", ] if !variant.nil? helpers += [ "#{model_name}_#{field}_field", "#{model_name}_#{field}", "#{model_name}_#{field}_raw", "#{global_model_name}_#{field}" ] if item.class.superclass.to_s != "ApplicationRecord" super_model_name = item.class.superclass.model_name.singular helpers += [ "#{super_model_name}_#{field}_field", "#{super_model_name}_#{field}", "#{super_model_name}_#{field}_raw", ] end helpers = helpers.flatten.compact helpers.each do |helper| if self.class.method_defined?(helper) if self.method(helper).arity == 1 || self.method(helper).arity == -2 return self.send(helper, item) || not_set end if self.method(helper).arity == 2 return self.send(helper, item, field) || not_set end end end if !defs.nil? && defs[:amount] val = item.send(field) rescue nil return not_set if val.nil? if !item.class.columns_hash[field.to_s].nil? && item.class.columns_hash[field.to_s].type == :integer # Integer only return amount_value(val, 0) rescue "" else return amount(val) rescue "" end else if item.class.reflect_on_association(field) return not_set if (item.send(field) rescue nil).nil? return render(item.send(field)) rescue item.send(field).to_s rescue "" elsif item.class.columns_hash[field.to_s] && item.class.columns_hash[field.to_s].type == :boolean return boolean_icon(item.send(field)) rescue "" elsif item.class.defined_enums.has_key?(field.to_s) return (item.send("human_#{field}") rescue (item.send(field).presence || not_set)).to_s rescue "" elsif item.class.columns_hash[field.to_s] && [:date].include?(item.class.columns_hash[field.to_s].type) return (item.send(field).blank? ? not_set : l(item.send(field), format: CustomTable.configuration.date_format)) rescue "" elsif item.class.columns_hash[field.to_s] && [:datetime].include?(item.class.columns_hash[field.to_s].type) return (item.send(field).blank? ? not_set : l(item.send(field), format: CustomTable.configuration.datetime_format)) rescue "" elsif item.class.columns_hash[field.to_s] && [:integer, :float, :decimal].include?(item.class.columns_hash[field.to_s].type) return not_set if (item.send(field) rescue nil).nil? return item.send(field) if !defs.nil? && defs[:amount] === false # Showing simple output if amount is false return amount(item.send(field)) rescue "" else # Non-column attribute v = (item.send(field).presence || not_set) rescue nil if !defs.nil? && !defs[:type].nil? return date_value(v) if [:date, :datetime].include?(defs[:type]) end return v.to_s.presence || not_set end end end |
#fields_table_for(collection, options = {}, &block) ⇒ Object
31 |
# File 'app/helpers/custom_table/application_helper.rb', line 31 def fields_table_for(collection, = {}, &block); end |
#raw_field_value_for(item, field, definitions: nil, variant: nil) ⇒ Object
Same as above but for Export only
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 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/custom_table/application_helper.rb', line 166 def raw_field_value_for item, field, definitions: nil, variant: nil defs = definitions model_name = item.model_name.singular global_model_name = item.class.model_name.singular helpers = [] helpers += [ "#{model_name}_#{variant}_#{field}_field_raw", "#{model_name}_#{variant}_#{field}_raw", ] if !variant.nil? helpers += [ "#{model_name}_#{field}_field_raw", "#{model_name}_#{field}_raw", "#{global_model_name}_#{field}_raw" ] if item.class.superclass.to_s != "ApplicationRecord" super_model_name = item.class.superclass.model_name.singular helpers += [ "#{super_model_name}_#{field}_field_raw", "#{super_model_name}_#{field}_raw", ] end helpers = helpers.flatten.compact helpers.each do |helper| return self.send(helper, item) if self.class.method_defined?(helper) end if !defs.nil? && defs[:amount] return item.send(field) rescue nil else if item.class.reflect_on_association(field) return item.send(field).to_s rescue nil elsif item.class.columns_hash[field.to_s] && item.class.columns_hash[field.to_s].type == :boolean return item.send(field) rescue nil elsif item.class.defined_enums.has_key?(field.to_s) return (item.send(field).presence) rescue nil elsif item.class.columns_hash[field.to_s] && [:date, :datetime].include?(item.class.columns_hash[field.to_s].type) return (item.send(field).presence) rescue nil elsif item.class.columns_hash[field.to_s] && [:float, :decimal].include?(item.class.columns_hash[field.to_s].type) return item.send(field).round(2) rescue nil elsif item.class.columns_hash[field.to_s] && [:integer].include?(item.class.columns_hash[field.to_s].type) return item.send(field) rescue nil else return (item.send(field).presence) rescue nil end end end |
#tree_opener(item_id, has_children = false, expanded = false) ⇒ Object
463 464 465 466 467 468 |
# File 'app/helpers/custom_table/application_helper.rb', line 463 def tree_opener item_id, has_children=false, =false content_tag :span, class: "tree-opener #{ ? 'opened' : ''}", data: {action: (has_children ? "click->table#toggle" : ""), "table-css-param": ".child-of-#{item_id}"} do concat content_tag(:span, (has_children ? "▶" : "▷"), class: "closed") concat content_tag(:span, "▼", class: "opened") end end |