Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gryphon committed May 2, 2024
1 parent 62881bd commit 1af7026
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Options available are:
* ```tree``` set to true if you have parent-child relation and you want to show records grouped by parent. Be sure to disable pagination as it will only group current page records
* ```group_by``` set to helper name to group records by result of it. Be sure to disable pagination as it will only group current page records
* ```expanded``` expand grouped or trees by default
* ```sortable``` set to true to allow to sort models. Model needs to have ```position``` attribute and use ```acts_as_list``` gem to be sorted. Gem uses ```stimulus-sortable``` JS package for sorting

## Rendering search panel

Expand Down
14 changes: 11 additions & 3 deletions app/helpers/custom_table/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def field_value_for item, field, definitions: nil, variant: nil
end
end

if !defs.nil? && defs[:amount]
if !defs.nil? && defs[:amount] == true
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 @@ -97,9 +97,15 @@ def field_value_for item, field, definitions: nil, variant: nil
return (item.send(field).blank? ? not_set : l(item.send(field), format: :short)) 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
return (item.send(field).presence || not_set).to_s rescue ""
# 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
end
end

Expand Down Expand Up @@ -330,7 +336,9 @@ def custom_table_fields_definition_for(model, variant = nil)

# Base definition for model
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
Expand All @@ -340,6 +348,7 @@ def custom_table_fields_definition_for_field(model, field, variant = nil)
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
Expand All @@ -358,7 +367,6 @@ def custom_table_data collection, variant=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
Expand Down
9 changes: 5 additions & 4 deletions app/helpers/custom_table/fieldset_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def has_editable?

def field column, **params, &block

defs = custom_table_fields_definition_for_field(@object.class, column) rescue nil
defs = @template.custom_table_fields_definition_for_field(@object.class, column) rescue nil

params = {} if params.nil?
params = params.deep_merge(@params)
Expand All @@ -25,12 +25,13 @@ def field column, **params, &block

if params[:label].nil?
if !defs.nil? && !defs[:label].blank?
params[:label] = defs[:label].nil?
params[:label] = defs[:label]
else
params[:label] = @object.class.human_attribute_name(column)
end
end


params[:template] = "field" if params[:template].nil?
params[:column] = column
params[:object] = @object
Expand All @@ -48,14 +49,14 @@ def field column, **params, &block
if block_given?
yield
else
@template.field_value_for(@object, column)
@template.field_value_for(@object, column, definitions: defs)
end
end
else
if block_given?
yield
else
@template.field_value_for(@object, column)
@template.field_value_for(@object, column, definitions: defs)
end
end

Expand Down
15 changes: 13 additions & 2 deletions app/views/custom_table/_table.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
- if local_assigns[:tree]
%th{style: "width: 15px;"}

- if local_assigns[:sortable]
%th{style: "width: 15px;"}

- if local_assigns[:with_select]
%th.checkbox-col
= check_box_tag "check-all", "", true, data: {"toggle-target": "toggler", "action": "toggle#toggle batch-actions#refresh"}
Expand All @@ -63,7 +66,12 @@
- if !local_assigns[:skip_actions]
%th.text-end

%tbody
- table_data_tag = {}
- table_data_tag["controller"] = "sortable"
- table_data_tag["sortable-resource-name-value"] = model_class.model_name.singular
- table_data_tag["sortable-handle-value"] = ".sort-handler"

%tbody{data: table_data_tag}

- position = 0

Expand Down Expand Up @@ -120,7 +128,7 @@
%th{class: [field.to_s, "text-end"]}
= amount_color group_fields_totals[field]
- else
%th{class: [field.to_s, "text-start"]}
%th.nowrap{class: [field.to_s, "text-start"]}
- if !shown
= tree_opener(group_id, true, local_assigns[:expanded])
= group
Expand Down Expand Up @@ -223,6 +231,9 @@
- if local_assigns[:with_index]
%th
- if local_assigns[:sortable]
%td
- if local_assigns[:with_select]
%th
Expand Down
17 changes: 14 additions & 3 deletions app/views/custom_table/_table_row.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
- row_class += ["child-of-#{item.parent_id}", (local_assigns[:expanded] ? "" : "d-none")] if local_assigns[:tree] && !item.parent_id.nil?
- row_class += ["child-of-#{local_assigns[:grouped_by_id]}", (local_assigns[:expanded] ? "" : "d-none")] if local_assigns[:grouped_by_id]

%tr{id: dom_id(item, "row"), :class => row_class}
- tr_data = {}
- if local_assigns[:sortable]
- tr_data["sortable-update-url"] = url_for(item)
%tr{id: dom_id(item, "row"), :class => row_class, data: tr_data }

- if local_assigns[:tree]
%td
Expand All @@ -17,6 +20,10 @@
- else
= custom_table_icon(custom_table_tree_child_icon_class)

- if local_assigns[:sortable]
%td
%i.fa.fa-grip-vertical.sort-handler.me-2

- if local_assigns[:with_select]
%td.checkbox-col
- if local_assigns[:with_select].to_s == "true"
Expand All @@ -40,7 +47,10 @@
%td{class: td_classes, id: dom_id(item, field)}
- v = field_value_for(item, field, definitions: defs, variant: variant)

- if defs[:editable]
- editable_enabled = defs[:editable]
- editable_enabled = editable_enabled.call(item) if editable_enabled.respond_to?(:call)

- if editable_enabled

= editable item, field do
- if defs[:link_to_show] == true && custom_table_has_show_route?(item) && can?(:show, item)
Expand All @@ -65,7 +75,8 @@
- elsif self.class.method_defined?("#{model_name}_custom_table_actions")
= self.send("#{model_name}_custom_table_actions", item)
- if local_assigns[:skip_default_actions].nil?
- if (!custom_table_has_show_route?(item) || local_assigns[:force_edit_button]) && can?(:update, item)
- local_assigns[:force_edit_button] = true if local_assigns[:force_edit_button].nil? && !custom_table_has_show_route?(item)
- if (local_assigns[:force_edit_button]) && can?(:update, item)
= custom_table_edit_button [:edit, namespace, local_assigns[:parent], item], id: "edit_#{item.model_name.singular}_#{item.id}", modal: local_assigns[:modal_edit]
- if can? :destroy, item
- if (Rails.application.routes.recognize_path(url_for([namespace, local_assigns[:parent], item]), :method => :delete) rescue false)
Expand Down

0 comments on commit 1af7026

Please sign in to comment.