Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom fields without corresponding model attributes #2569

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/administrate/page/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def attribute_field(dashboard, resource, attribute_name, page)
end

def get_attribute_value(resource, attribute_name)
resource.public_send(attribute_name)
resource.try(attribute_name)
end

attr_reader :dashboard, :options
Expand Down
3 changes: 2 additions & 1 deletion spec/example_app/app/dashboards/customer_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class CustomerDashboard < Administrate::BaseDashboard
searchable_fields: ["name"],
include_blank: true
),
password: Field::Password
password: Field::Password,
non_attribute_field: NonAttributeField
}

COLLECTION_ATTRIBUTES = ATTRIBUTE_TYPES.keys - %i[created_at updated_at]
Expand Down
5 changes: 5 additions & 0 deletions spec/example_app/app/fields/non_attribute_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class NonAttributeField < Administrate::Field::Base
def to_s
"Hello, Non Attribute Field"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>NonAttributeField is not supposed to be used in form.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= field.to_s %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= field.to_s %>
8 changes: 8 additions & 0 deletions spec/features/show_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@
end
end

it "displays non-attribute field" do
customer = create(:customer)

visit admin_customer_path(customer)

expect(page).to have_text("Hello, Non Attribute Field")
end

def ids_in_table
all("tr td:first-child").map(&:text).map(&:to_i)
end
Expand Down