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

Better defaults for the Show page #2138

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/active_admin/views/components/attributes_table.rb
Expand Up @@ -7,12 +7,12 @@ class AttributesTable < ActiveAdmin::Component
def build(obj, *attrs)
@collection = is_array?(obj) ? obj : [obj]
@resource_class = @collection.first.class
options = { }
options = attrs.extract_options!
options[:for] = @collection.first if single_record?
super(options)
@table = table
build_colgroups
rows(*attrs)
rows(*display_rows(*attrs, options))
end

def rows(*attrs)
Expand Down Expand Up @@ -48,6 +48,25 @@ def default_id_for_prefix
'attributes_table'
end

def default_rows
if @resource_class.respond_to? :attribute_names
@resource_class.attribute_names
elsif @resource_class.respond_to? :keys
@resource_class.keys
elsif @resource_class.respond_to? :each
@resource_class.first
else
raise "can't detect default_rows"
end
end

def display_rows(*attrs, options)
attrs = default_rows if attrs.empty?
attrs.map!(&:to_s)
attrs += [*options[:with]].map(&:to_s)
attrs -= [*options[:expect]].map(&:to_s)
end

# Build Colgroups
#
# Colgroups are only necessary for a collection of records; not
Expand Down
18 changes: 0 additions & 18 deletions spec/unit/views/components/attributes_table_spec.rb
Expand Up @@ -236,24 +236,6 @@
end # describe rendering rows
end # with a collection


context "when using a single Hash" do
let(:table) do
render_arbre_component nil, helpers do
attributes_table_for foo: 1, bar: 2 do
row :foo
row :bar
end
end
end
it "should render" do
expect(table.find_by_tag("th")[0].content).to eq "Foo"
expect(table.find_by_tag("th")[1].content).to eq "Bar"
expect(table.find_by_tag("td")[0].content).to eq "1"
expect(table.find_by_tag("td")[1].content).to eq "2"
end
end

context "when using an Array of Hashes" do
let(:table) do
render_arbre_component nil, helpers do
Expand Down