From 95d3aaf7f12bff18740d6e1445c584deb3eb5080 Mon Sep 17 00:00:00 2001 From: Sergei Smagin Date: Sat, 23 Feb 2019 20:17:36 +0300 Subject: [PATCH] Fix generator for non-association/columnar attrs. Rails allows for custom attributes, and these are often used with gems such as `attr_encrypted`. Previously, the Dashboard generator would fail with: > undefined method `has_one?' for nil:NilClass (NoMethodError) Instead of failing with an error, this ensures we rely on column names instead of attributes names. --- .../dashboard/dashboard_generator.rb | 4 +++- spec/generators/dashboard_generator_spec.rb | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/generators/administrate/dashboard/dashboard_generator.rb b/lib/generators/administrate/dashboard/dashboard_generator.rb index e6389ffd42..1b4cb70a82 100644 --- a/lib/generators/administrate/dashboard/dashboard_generator.rb +++ b/lib/generators/administrate/dashboard/dashboard_generator.rb @@ -50,7 +50,9 @@ def namespace end def attributes - klass.reflections.keys + klass.attribute_names - redundant_attributes + klass.reflections.keys + + klass.columns.map(&:name) - + redundant_attributes end def form_attributes diff --git a/spec/generators/dashboard_generator_spec.rb b/spec/generators/dashboard_generator_spec.rb index d04127ba47..8c65757de8 100644 --- a/spec/generators/dashboard_generator_spec.rb +++ b/spec/generators/dashboard_generator_spec.rb @@ -349,6 +349,29 @@ class Ticket < ActiveRecord::Base remove_constants :Account, :Ticket end end + + if ActiveRecord.version >= Gem::Version.new(5) + it "skips temporary attributes" do + begin + ActiveRecord::Schema.define do + create_table :accounts + end + + class Account < ActiveRecord::Base + reset_column_information + attribute :tmp_attribute, :boolean + end + + dashboard = file("app/dashboards/account_dashboard.rb") + + run_generator ["account"] + + expect(dashboard).not_to contain("tmp_attribute") + ensure + remove_constants :Account + end + end + end end describe "COLLECTION_ATTRIBUTES" do