Skip to content

Commit

Permalink
Cleanup and improve spec
Browse files Browse the repository at this point in the history
  • Loading branch information
nhippenmeyer committed Mar 7, 2022
1 parent c11bea1 commit ae67bc1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
26 changes: 16 additions & 10 deletions lib/generators/administrate/dashboard/dashboard_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/generators/dashboard_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ae67bc1

Please sign in to comment.