diff --git a/lib/generators/administrate/dashboard/dashboard_generator.rb b/lib/generators/administrate/dashboard/dashboard_generator.rb index dbb5a8df31..3db86e6134 100644 --- a/lib/generators/administrate/dashboard/dashboard_generator.rb +++ b/lib/generators/administrate/dashboard/dashboard_generator.rb @@ -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 diff --git a/spec/generators/dashboard_generator_spec.rb b/spec/generators/dashboard_generator_spec.rb index 85fb61451e..f9cc1933bc 100644 --- a/spec/generators/dashboard_generator_spec.rb +++ b/spec/generators/dashboard_generator_spec.rb @@ -61,6 +61,31 @@ class Foo < ApplicationRecord end end + it "sorts the attributes" do + begin + 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 @@ -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