Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gryphon committed Feb 21, 2024
1 parent 05de2e4 commit d5bb81c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
6 changes: 4 additions & 2 deletions app/controllers/concerns/custom_table_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def format_web
def custom_table collection, variant = nil, default_sorts: "created_at desc", default_search: {}

@q = collection.ransack(params[:q])
@q = collection.ransack((params[:q] || {}).merge(default_search)) if @q.conditions.empty?
@q = collection.ransack((params[:q] || {}).merge(default_search)) if params[:q].nil?

customization = helpers.custom_table_user_customization_for(collection.model, variant)
@q.sorts = customization&.dig(:sorts).presence || default_sorts if @q.sorts.empty?
Expand All @@ -28,7 +28,9 @@ def custom_table collection, variant = nil, default_sorts: "created_at desc", de

if !current_user.nil?
current_user.save_custom_table_settings(collection.model, variant, per_page: params[:per]) if !params[:per].nil? && params[:do_not_save_settings].nil?
current_user.save_custom_table_settings(collection.model, variant, sorts: "#{@q.sorts[0].name} #{@q.sorts[0].dir}") if !params[:q].nil? && !params[:q][:s].nil? && !@q.nil? && !@q.sorts[0].nil? && !@q.sorts[0].name.nil? && params[:do_not_save_settings].nil?
if !params[:q].nil? && !params[:q][:s].nil? && !@q.nil? && !@q.sorts[0].nil? && !@q.sorts[0].name.nil? && params[:do_not_save_settings].nil?
current_user.save_custom_table_settings(collection.model, variant, sorts: "#{@q.sorts[0].name} #{@q.sorts[0].dir}")
end
end

return collection
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/custom_table/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def update

if current_user.save_custom_table_settings(model, variant, fields: p[:fields])
flash[:notice] = t("custom_table.customization_saved")
respond_to do |format|
format.turbo_stream {render turbo_stream: turbo_stream.action(:refresh, nil)}
end
else
# optional_redirect_to main_app.root_path, alert: t("custom_table.cannot_save_customization")
end
Expand All @@ -62,6 +65,9 @@ def destroy

if current_user.destroy_custom_table_settings(model, variant)
flash[:notice] = t("custom_table.customization_saved")
respond_to do |format|
format.turbo_stream {render turbo_stream: turbo_stream.action(:refresh, nil)}
end
else
# optional_redirect_to main_app.root_path, alert: t("custom_table.cannot_save_customization")
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/custom_table/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def field_value_for item, field, definitions: nil, variant: nil

helpers.each do |helper|
if self.class.method_defined?(helper)
if self.method(helper).arity == 1
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
Expand Down
14 changes: 11 additions & 3 deletions app/views/custom_table/_table.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
- global_model_name = model_class.model_name.singular
- fields_totals = {}
- variant = local_assigns[:variant]
- if local_assigns[:tree] && local_assigns[:paginate]
%p.text-danger Tree mode is incompartible with pagination

- if collection.respond_to?(:model)
- customization = custom_table_user_customization_for(collection.model, variant)
Expand Down Expand Up @@ -97,12 +99,17 @@
-# - abort Hash[grouped_collection.sort_by{|k,v| k.to_s}].inspect
- group_id = 0

- Hash[grouped_collection.sort_by{|k,v| k.to_s}].each do |group, items|

- if !group.nil?

- group_id += 1

-# Calculating group totals
- group_fields_totals = {}

- items.each do |item|
- custom_table_fields_totals(fields: fields, item: item, totals: totals, variant: variant, fields_totals: group_fields_totals)
%tr
Expand All @@ -115,14 +122,15 @@
- else
%th{class: [field.to_s, "text-start"]}
- if !shown
= tree_opener(group, true, local_assigns[:expanded])
= tree_opener(group_id, true, local_assigns[:expanded])
= group
- shown = true
%th.text-end
- if !local_assigns[:skip_actions]
%th.text-end
- items.each do |item|
- position+=1
= render "custom_table/table_row", local_assigns.merge({item: item, fields: fields, position: position, fields_totals: fields_totals, grouped_by_id: group})
= render "custom_table/table_row", local_assigns.merge({item: item, fields: fields, position: position, fields_totals: fields_totals, grouped_by_id: group_id})
- else
Expand Down
9 changes: 9 additions & 0 deletions app/views/custom_table/_table_fe.xlsx.fast_excel
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ collection.except(:limit, :offset).send(iterator) do |item|
sheet.append_row(row, formats)
end

ef = workbook.add_format(top: :medium)
row = []
fields.each do |field, defs|
next if defs[:table] == false
row.push nil
end
sheet.append_row(row, ef)


if local_assigns[:autofilter]
sheet.autofilter(data_start_row, 0, sheet.last_row_number, fields.length-1)
end

0 comments on commit d5bb81c

Please sign in to comment.