From a3fb4da4fa9bab800941882dccf2938e520d12af Mon Sep 17 00:00:00 2001 From: Nic Hippenmeyer Date: Wed, 26 Jan 2022 12:10:57 -0500 Subject: [PATCH 1/4] Sort dashboard attributes --- .../administrate/dashboard/templates/dashboard.rb.erb | 2 +- spec/generators/dashboard_generator_spec.rb | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/generators/administrate/dashboard/templates/dashboard.rb.erb b/lib/generators/administrate/dashboard/templates/dashboard.rb.erb index 7c0a65c0a9..3e122d28e5 100644 --- a/lib/generators/administrate/dashboard/templates/dashboard.rb.erb +++ b/lib/generators/administrate/dashboard/templates/dashboard.rb.erb @@ -8,7 +8,7 @@ class <%= class_name %>Dashboard < Administrate::BaseDashboard # which determines how the attribute is displayed # on pages throughout the dashboard. ATTRIBUTE_TYPES = { -<% attributes.each do |attr| -%> +<% attributes.sort.each do |attr| -%> <%= attr %>: <%= field_type(attr) %>, <% end -%> }.freeze diff --git a/spec/generators/dashboard_generator_spec.rb b/spec/generators/dashboard_generator_spec.rb index 85fb61451e..ea701e0df2 100644 --- a/spec/generators/dashboard_generator_spec.rb +++ b/spec/generators/dashboard_generator_spec.rb @@ -33,8 +33,8 @@ class Foo < ApplicationRecord load file("app/dashboards/foo_dashboard.rb") attrs = FooDashboard::ATTRIBUTE_TYPES - expect(attrs[:id]).to eq(Administrate::Field::Number) expect(attrs[:created_at]).to eq(Administrate::Field::DateTime) + expect(attrs[:id]).to eq(Administrate::Field::Number) expect(attrs[:updated_at]).to eq(Administrate::Field::DateTime) ensure remove_constants :Foo, :FooDashboard @@ -323,10 +323,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 From 7d1083622ce4430b9f91d08b2aa13dc2b9e9a48f Mon Sep 17 00:00:00 2001 From: Nic Hippenmeyer Date: Fri, 11 Feb 2022 13:03:27 -0500 Subject: [PATCH 2/4] Sort id first and timestamps last --- .../dashboard/templates/dashboard.rb.erb | 11 +++++++- spec/generators/dashboard_generator_spec.rb | 25 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/generators/administrate/dashboard/templates/dashboard.rb.erb b/lib/generators/administrate/dashboard/templates/dashboard.rb.erb index 3e122d28e5..0533d74111 100644 --- a/lib/generators/administrate/dashboard/templates/dashboard.rb.erb +++ b/lib/generators/administrate/dashboard/templates/dashboard.rb.erb @@ -8,9 +8,18 @@ class <%= class_name %>Dashboard < Administrate::BaseDashboard # which determines how the attribute is displayed # on pages throughout the dashboard. ATTRIBUTE_TYPES = { -<% attributes.sort.each do |attr| -%> +<% if attributes.include?('id') %> + id: <%= field_type('id') %>, +<% end %> +<% attributes.sort.without(%w[id created_at updated_at]).each do |attr| -%> <%= attr %>: <%= field_type(attr) %>, <% end -%> +<% if attributes.include?('created_at') %> + created_at: <%= field_type('created_at') %>, +<% end %> +<% if attributes.include?('updated_at') %> + updated_at: <%= field_type('updated_at') %>, +<% end %> }.freeze # COLLECTION_ATTRIBUTES diff --git a/spec/generators/dashboard_generator_spec.rb b/spec/generators/dashboard_generator_spec.rb index ea701e0df2..68f1adb6df 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) 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[id 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 From c11bea15bec2da6729c8019279509add6e03de07 Mon Sep 17 00:00:00 2001 From: Nic Hippenmeyer Date: Fri, 11 Feb 2022 13:28:38 -0500 Subject: [PATCH 3/4] Sort all attributes --- .../administrate/dashboard/dashboard_generator.rb | 13 ++++++++++--- .../dashboard/templates/dashboard.rb.erb | 11 +---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/generators/administrate/dashboard/dashboard_generator.rb b/lib/generators/administrate/dashboard/dashboard_generator.rb index dbb5a8df31..6804e3b55f 100644 --- a/lib/generators/administrate/dashboard/dashboard_generator.rb +++ b/lib/generators/administrate/dashboard/dashboard_generator.rb @@ -53,9 +53,16 @@ def namespace end def attributes - klass.reflections.keys + - klass.columns.map(&:name) - - redundant_attributes + columns = klass.columns.map(&:name) + + (klass.reflections.keys + + columns - + redundant_attributes - + %w[id created_at updated_at]).sort.tap do |result| + result.unshift(klass.primary_key) if klass.primary_key.present? + result.push("created_at") if columns.include?("created_at") + result.push("updated_at") if columns.include?("updated_at") + end end def form_attributes diff --git a/lib/generators/administrate/dashboard/templates/dashboard.rb.erb b/lib/generators/administrate/dashboard/templates/dashboard.rb.erb index 0533d74111..7c0a65c0a9 100644 --- a/lib/generators/administrate/dashboard/templates/dashboard.rb.erb +++ b/lib/generators/administrate/dashboard/templates/dashboard.rb.erb @@ -8,18 +8,9 @@ class <%= class_name %>Dashboard < Administrate::BaseDashboard # which determines how the attribute is displayed # on pages throughout the dashboard. ATTRIBUTE_TYPES = { -<% if attributes.include?('id') %> - id: <%= field_type('id') %>, -<% end %> -<% attributes.sort.without(%w[id created_at updated_at]).each do |attr| -%> +<% attributes.each do |attr| -%> <%= attr %>: <%= field_type(attr) %>, <% end -%> -<% if attributes.include?('created_at') %> - created_at: <%= field_type('created_at') %>, -<% end %> -<% if attributes.include?('updated_at') %> - updated_at: <%= field_type('updated_at') %>, -<% end %> }.freeze # COLLECTION_ATTRIBUTES From ae67bc1abfe0a521f596bfefe5b191f4293dcd46 Mon Sep 17 00:00:00 2001 From: Nic Hippenmeyer Date: Mon, 7 Mar 2022 15:46:45 -0500 Subject: [PATCH 4/4] Cleanup and improve spec --- .../dashboard/dashboard_generator.rb | 26 ++++++++++++------- spec/generators/dashboard_generator_spec.rb | 6 ++--- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/generators/administrate/dashboard/dashboard_generator.rb b/lib/generators/administrate/dashboard/dashboard_generator.rb index 6804e3b55f..3db86e6134 100644 --- a/lib/generators/administrate/dashboard/dashboard_generator.rb +++ b/lib/generators/administrate/dashboard/dashboard_generator.rb @@ -53,16 +53,22 @@ def namespace end def attributes - columns = klass.columns.map(&:name) - - (klass.reflections.keys + - columns - - redundant_attributes - - %w[id created_at updated_at]).sort.tap do |result| - result.unshift(klass.primary_key) if klass.primary_key.present? - result.push("created_at") if columns.include?("created_at") - result.push("updated_at") if columns.include?("updated_at") - end + 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 68f1adb6df..f9cc1933bc 100644 --- a/spec/generators/dashboard_generator_spec.rb +++ b/spec/generators/dashboard_generator_spec.rb @@ -33,8 +33,8 @@ class Foo < ApplicationRecord load file("app/dashboards/foo_dashboard.rb") attrs = FooDashboard::ATTRIBUTE_TYPES - expect(attrs[:created_at]).to eq(Administrate::Field::DateTime) expect(attrs[:id]).to eq(Administrate::Field::Number) + expect(attrs[:created_at]).to eq(Administrate::Field::DateTime) expect(attrs[:updated_at]).to eq(Administrate::Field::DateTime) ensure remove_constants :Foo, :FooDashboard @@ -64,7 +64,7 @@ class Foo < ApplicationRecord it "sorts the attributes" do begin ActiveRecord::Schema.define do - create_table(:foos) do |t| + create_table(:foos, primary_key: :code) do |t| t.string :col_2 t.string :col_1 t.string :col_3 @@ -80,7 +80,7 @@ class Foo < ApplicationRecord load file("app/dashboards/foo_dashboard.rb") attrs = FooDashboard::ATTRIBUTE_TYPES.keys - expect(attrs).to eq(%i[id col_1 col_2 col_3 created_at updated_at]) + expect(attrs).to eq(%i[code col_1 col_2 col_3 created_at updated_at]) ensure remove_constants :Foo, :FooDashboard end