From 5c2efe01a3c9a336793f7b6f117059b5b916af5b Mon Sep 17 00:00:00 2001 From: Sam Pohlenz Date: Fri, 1 Nov 2024 08:10:46 +1030 Subject: [PATCH 1/3] Add ability to exclude attributes from automatic forms --- lib/trestle/form/automatic.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/trestle/form/automatic.rb b/lib/trestle/form/automatic.rb index 2b00db3e..fe9e5a06 100644 --- a/lib/trestle/form/automatic.rb +++ b/lib/trestle/form/automatic.rb @@ -2,8 +2,11 @@ module Trestle class Form class Automatic < Form def initialize(admin, options={}) + @admin = admin + form = self + super(options) do |instance| - admin.default_form_attributes.each do |attribute| + form.attributes.each do |attribute| if attribute.array? if [:string, :text].include?(attribute.type) select attribute.name, nil, {}, { multiple: true, data: { tags: true, select_on_close: true }} @@ -46,6 +49,17 @@ def initialize(admin, options={}) end end end + + def attributes + @admin.default_form_attributes.reject { |attribute| + exclude?(attribute.name) + } + end + + private + def exclude?(field) + Array(options[:exclude]).include?(field) + end end end end From bcd2286a4e5c8561b691f70e560a0d7cfa7ffef9 Mon Sep 17 00:00:00 2001 From: Sam Pohlenz Date: Fri, 1 Nov 2024 08:11:32 +1030 Subject: [PATCH 2/3] Support table builder method without block for automatic table options --- lib/trestle/admin/builder.rb | 7 ++++++- lib/trestle/table/automatic.rb | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/trestle/admin/builder.rb b/lib/trestle/admin/builder.rb index ce23f3ab..63009d81 100644 --- a/lib/trestle/admin/builder.rb +++ b/lib/trestle/admin/builder.rb @@ -49,7 +49,12 @@ def menu(*args, &block) def table(name_or_options={}, options={}, &block) name, options = normalize_table_options(name_or_options, options) - admin.tables[name] = Table::Builder.build(options, &block) + + if block_given? + admin.tables[name] = Table::Builder.build(options, &block) + else + admin.tables[name] = Table::Automatic.new(admin, options) + end end def form(options={}, &block) diff --git a/lib/trestle/table/automatic.rb b/lib/trestle/table/automatic.rb index 9dabed0a..03c04378 100644 --- a/lib/trestle/table/automatic.rb +++ b/lib/trestle/table/automatic.rb @@ -1,8 +1,8 @@ module Trestle class Table class Automatic < Table - def initialize(admin) - super(sortable: true, admin: admin) + def initialize(admin, options={}) + super(options.merge(sortable: true, admin: admin)) end def columns From bfeffe3a809c619674c074ca7952f71b6b558e96 Mon Sep 17 00:00:00 2001 From: Sam Pohlenz Date: Fri, 1 Nov 2024 08:11:44 +1030 Subject: [PATCH 3/3] Add ability to exclude attributes from automatic tables --- lib/trestle/table/automatic.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/trestle/table/automatic.rb b/lib/trestle/table/automatic.rb index 03c04378..14d29f67 100644 --- a/lib/trestle/table/automatic.rb +++ b/lib/trestle/table/automatic.rb @@ -10,7 +10,7 @@ def columns end def content_columns - admin.default_table_attributes.map.with_index do |attribute, index| + attributes.map.with_index do |attribute, index| case attribute.type when :association Column.new(attribute.association_name, sort: false) @@ -30,6 +30,17 @@ def content_columns def actions_column ActionsColumn.new end + + private + def attributes + admin.default_table_attributes.reject { |attribute| + exclude?(attribute.name) + } + end + + def exclude?(field) + Array(options[:exclude]).include?(field) + end end end end