Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PoC] Add ability to exclude fields from automatic forms and tables #519

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/trestle/admin/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion lib/trestle/form/automatic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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
17 changes: 14 additions & 3 deletions lib/trestle/table/automatic.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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
content_columns + [actions_column]
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)
Expand All @@ -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