Skip to content

Commit

Permalink
Fix generator for non-association/columnar attrs. (#1290)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
s-mage authored and nickcharlton committed Apr 16, 2019
1 parent 57f7d95 commit 4e36c31
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/generators/administrate/dashboard/dashboard_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions spec/generators/dashboard_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4e36c31

Please sign in to comment.