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

Sort dashboard attributes #2133

Merged
merged 5 commits into from
Mar 7, 2022
Merged
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
15 changes: 14 additions & 1 deletion lib/generators/administrate/dashboard/dashboard_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,22 @@ def namespace
end

def attributes
klass.reflections.keys +
attrs = (
klass.reflections.keys +
klass.columns.map(&:name) -
redundant_attributes
)

primary_key = attrs.delete(klass.primary_key)
created_at = attrs.delete("created_at")
updated_at = attrs.delete("updated_at")

[
primary_key,
*attrs.sort,
created_at,
updated_at,
].compact
end

def form_attributes
Expand Down
32 changes: 30 additions & 2 deletions spec/generators/dashboard_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,31 @@ class Foo < ApplicationRecord
end
end

it "sorts the attributes" do
begin
nhippenmeyer marked this conversation as resolved.
Show resolved Hide resolved
ActiveRecord::Schema.define do
create_table(:foos, primary_key: :code) do |t|
t.string :col_2
t.string :col_1
t.string :col_3
t.timestamps
end
end

class Foo < ApplicationRecord
reset_column_information
end

run_generator ["foo"]
load file("app/dashboards/foo_dashboard.rb")
attrs = FooDashboard::ATTRIBUTE_TYPES.keys

expect(attrs).to eq(%i[code col_1 col_2 col_3 created_at updated_at])
ensure
remove_constants :Foo, :FooDashboard
end
end

it "defaults to a string column that is not searchable" do
begin
ActiveRecord::Schema.define do
Expand Down Expand Up @@ -323,10 +348,13 @@ class Foo < ApplicationRecord

run_generator ["foo"]
load file("app/dashboards/foo_dashboard.rb")
all_attrs = FooDashboard::ATTRIBUTE_TYPES.keys
all_attrs = FooDashboard::ATTRIBUTE_TYPES.keys.sort
table_attrs = FooDashboard::COLLECTION_ATTRIBUTES

expect(table_attrs).to eq(all_attrs.first(table_attribute_limit))
expect(table_attrs).to contain_exactly(
:id,
*all_attrs.first(table_attribute_limit - 1),
)
expect(table_attrs).not_to eq(all_attrs)
ensure
remove_constants :Foo, :FooDashboard
Expand Down