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/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 diff --git a/lib/trestle/table/automatic.rb b/lib/trestle/table/automatic.rb index 9dabed0a..14d29f67 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 @@ -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