Skip to content

Commit

Permalink
Better XLSX exports
Browse files Browse the repository at this point in the history
  • Loading branch information
gryphon committed Sep 1, 2023
1 parent 302e6cf commit fa1868e
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 151 deletions.
17 changes: 17 additions & 0 deletions app/assets/stylesheets/custom_table/table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ table {
padding: 2px;
text-align: right;
margin-right: 5px;
z-index: 1000;
}

.widget table.search-fields td {
Expand Down Expand Up @@ -74,3 +75,19 @@ table tr:hover, li.list-group-item:hover {
display: inherit;
}
}

table {
tr.sticky, thead.sticky {
position: sticky;
background: white;
z-index: 1;
}
th.sticky-left, td.sticky-left {
position: sticky;
left: 0px;
background: white;
}
.text-sm {
font-size: 11px;
}
}
107 changes: 91 additions & 16 deletions app/helpers/custom_table/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,39 @@ def field_value_for item, field, definitions: nil, representation: nil
defs = definitions

model_name = item.model_name.singular
global_model_name = item.class.model_name.singular
global_model_name = item.class.model_name.singular # non-model

helpers = []
helpers += [defs[:helper]] if !defs.nil?

helpers += [
"#{model_name}_#{representation}_#{field}_field",
"#{model_name}_#{representation}_#{field}",
] if !representation.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|
return self.send(helper, item) || not_set if self.class.method_defined?(helper)
end

if !defs.nil? && !defs[:helper].nil?
return self.send(defs[:helper], item, field).presence || not_set
elsif !representation.nil? && self.class.method_defined?("#{model_name}_#{representation}_#{field}_field")
return self.send("#{model_name}_#{representation}_#{field}_field", item).presence || not_set
elsif !representation.nil? && self.class.method_defined?("#{model_name}_#{representation}_#{field}")
return self.send("#{model_name}_#{representation}_#{field}", item).presence || not_set
elsif self.class.method_defined?("#{model_name}_#{field}_field")
return self.send("#{model_name}_#{field}_field", item).presence || not_set
elsif self.class.method_defined?("#{model_name}_#{field}")
return self.send("#{model_name}_#{field}", item).presence || not_set
elsif self.class.method_defined?("#{model_name}_#{field}_raw")
return self.send("#{model_name}_#{field}_raw", item).presence || not_set
elsif self.class.method_defined?("#{global_model_name}_#{field}")
return self.send("#{global_model_name}_#{field}", item).presence || not_set
elsif !defs.nil? && defs[:amount]
if !defs.nil? && defs[:amount]
if !item.class.columns_hash[field.to_s].nil? && item.class.columns_hash[field.to_s].type == :integer
return amount_value(item.send(field), 0) rescue ""
else
Expand All @@ -82,6 +98,63 @@ def field_value_for item, field, definitions: nil, representation: nil

end

# Same as above but for Export only
def raw_field_value_for item, field, definitions: nil, representation: nil

defs = definitions

model_name = item.model_name.singular
global_model_name = item.class.model_name.singular

helpers = []

helpers += [
"#{model_name}_#{representation}_#{field}_field_raw",
"#{model_name}_#{representation}_#{field}_raw",
] if !representation.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).blank? ? nil : field) rescue nil
elsif item.class.columns_hash[field.to_s] && [:integer, :float, :decimal].include?(item.class.columns_hash[field.to_s].type)
return nil if (item.send(field) rescue nil).nil?
return item.send(field) rescue nil
else
return (item.send(field).presence).to_s rescue nil
end
end

end


# Returns list of fields to show in table according user settings
def custom_table_fields_for(model, representation: nil, current_search: {}, predefined_fields: nil, use_all_fields: false)

Expand Down Expand Up @@ -224,6 +297,8 @@ def custom_table_data collection, representation=nil, **params
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[:force_edit_button] = false if params[:force_edit_button].nil?
params[:modal_edit] = true if params[:modal_edit].nil?

render "custom_table/table", params do
yield
Expand Down
6 changes: 4 additions & 2 deletions app/views/custom_table/_field.haml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
%dt{class: adaptive ? "col-lg-2 col-md-5" : "col-5"}= label
%dd{id: "#{column}_#{object.model_name.singular}_#{object.id}", class: adaptive ? "col-lg-4 col-md-7" : "col-7"}= yield
%div.d-flex.justify-content-between.mb-2
%b= label
%span.text-end.d-inline{id: "#{column}_#{object.model_name.singular}_#{object.id}"}= yield
-# %dt{class: adaptive ? "col-lg-2 col-md-5" : "col-5"}= label
2 changes: 1 addition & 1 deletion app/views/custom_table/_fieldset.haml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
%dl.row.mb-0
%div
= yield
19 changes: 10 additions & 9 deletions app/views/custom_table/_filter.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,22 @@
- defs[:search].each do |field|

- a[key].push field[:q]
- label = field[:label] || defs[:label]

- if field[:type] == :text

- ih = {class: "form-control-sm"}
- ih = ih.merge(defs[:input_html]) if !defs[:input_html].nil?

= f.input field[:q], input_html: ih, required: false, label: false, placeholder: defs[:label]
= f.input field[:q], input_html: ih, required: false, label: false, placeholder: label

- if field[:type] == :boolean

= f.input field[:q], as: :boolean, input_html: {}, required: false, label: field[:label] || defs[:label], unchecked_value: ""
= f.input field[:q], as: :boolean, input_html: {}, required: false, label: label, unchecked_value: ""

- if field[:type] == :switch

= f.input field[:q], as: :select, input_html: {:class => "form-select form-select-sm"}, required: false, label: false, :collection => [[t("yes")+" | #{defs[:label]}", "true"], [t("no")+" | #{defs[:label]}", "false"]], include_blank: defs[:label]
= f.input field[:q], as: :select, input_html: {:class => "form-select form-select-sm"}, required: false, label: false, :collection => [[t("yes")+" | #{label}", "true"], [t("no")+" | #{label}", "false"]], include_blank: label

- if field[:type] == :select

Expand All @@ -52,37 +53,37 @@
- collection = collection.accessible_by(current_ability)
- label_method = :to_s

= f.input field[:q], :as => :select, :input_html => {:class => "form-select form-select-sm"}, :required => false, :label => false, :collection => collection, label_method: label_method, include_blank: defs[:label]
= f.input field[:q], :as => :select, :input_html => {:class => "form-select form-select-sm"}, :required => false, :label => false, :collection => collection, label_method: label_method, include_blank: label

- if field[:type] == :grouped_select

- collection = field[:collection]

- collection = collection.accessible_by(current_ability) if collection.class.to_s =~ /ActiveRecord_Relation/i

= f.input field[:q], :as => :grouped_select, :input_html => {:class => "form-select form-select-sm"}, :required => false, :label => false, group_method: field[:group_method], :collection => collection, include_blank: defs[:label]
= f.input field[:q], :as => :grouped_select, :input_html => {:class => "form-select form-select-sm"}, :required => false, :label => false, group_method: field[:group_method], :collection => collection, include_blank: label

- if field[:type] == :autocomplete

- collection = field[:collection]

- collection = collection.accessible_by(current_ability) if collection.class.to_s =~ /ActiveRecord_Relation/i

= f.input field[:q], :as => :autocomplete, :input_html => {:class => "input-sm"}, :required => false, :label => false, :collection => collection, prompt: defs[:label]
= f.input field[:q], :as => :autocomplete, :input_html => {:class => "input-sm"}, :required => false, :label => false, :collection => collection, prompt: label

- if field[:type] == :enum

- collection = field[:collection]
- collection = [search_model, key.to_s.pluralize] if collection.nil?
- coll = collection[0].send(collection[1]).keys.map { |w| [(collection[0].method_defined?(:human_enum_name) ? collection[0].human_enum_name(collection[1].to_s.singularize, w) : w), w] }

= f.input field[:q], :as => :select, :input_html => {:class => "form-select form-select-sm"}, :required => false, :label => false, :collection => coll, include_blank: defs[:label]
= f.input field[:q], :as => :select, :input_html => {:class => "form-select form-select-sm"}, :required => false, :label => false, :collection => coll, include_blank: label

- if field[:type] == :dates
.col-12
.input-group.input-group-sm{"data-controller": "dates"}
= f.input_field field[:q][0], :as => :date_picker, :label => false, :class => "form-control", data: {"dates-target": "dateFrom"}, :placeholder => defs[:label]+" ("+t("custom_table.date_from")+")"
= f.input_field field[:q][1], :as => :date_picker, :label => false, :class => "form-control", data: {"dates-target": "dateTo"}, :placeholder => defs[:label]+" ("+t("custom_table.date_to")+")"
= f.input_field field[:q][0], :as => :date_picker, :label => false, :class => "form-control", data: {"dates-target": "dateFrom"}, :placeholder => label+" ("+t("custom_table.date_from")+")"
= f.input_field field[:q][1], :as => :date_picker, :label => false, :class => "form-control", data: {"dates-target": "dateTo"}, :placeholder => label+" ("+t("custom_table.date_to")+")"
%button.btn.btn-outline-secondary.dropdown-toggle(type="button" data-bs-toggle="dropdown" aria-expanded="false")
%ul.dropdown-menu.dropdown-menu-end
%button.dropdown-item{type: "button", "data-action": "dates#prevWeek"}= t("analytics.previos_week")
Expand Down

0 comments on commit fa1868e

Please sign in to comment.