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

Allow different form attributes for new/update actions #1991

Merged
merged 5 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/views/administrate/application/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ and renders all form fields for a resource's editable attributes.
</div>
<% end %>

<% page.attributes.each do |attribute| -%>
<% page.attributes(controller.action_name).each do |attribute| -%>
<div class="field-unit field-unit--<%= attribute.html_class %> field-unit--<%= requireness(attribute) %>">
<%= render_field attribute, f: f %>
</div>
Expand Down
22 changes: 20 additions & 2 deletions docs/customizing_dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ edit the dashboard file generated by the installation generator.
By default, the file will look something like this:

```ruby
require "administrate/dashboard/base"
require "administrate/base_dashboard"

class CustomerDashboard < Administrate::Dashboard::Base
class CustomerDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
id: Field::Number,
name: Field::String,
Expand Down Expand Up @@ -326,3 +326,21 @@ COLLECTION_FILTERS = {
inactive: ->(resources) { resources.inactive }
}
```

## Form Attributes

You can define different attributes for new/create or edit/update actions:

```ruby
FORM_ATTRIBUTES_NEW = [
:name,
:email
]
FORM_ATTRIBUTES_EDIT = [
:name,
:email,
:orders
]
```

Or for custom action with constant name `"FORM_ATTRIBUTES_#{action.upcase}"`
12 changes: 10 additions & 2 deletions lib/administrate/base_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,16 @@ def all_attributes
attribute_types.keys
end

def form_attributes
self.class::FORM_ATTRIBUTES
def form_attributes(action = nil)
specific_form_attributes_for(action) || self.class::FORM_ATTRIBUTES
end

def specific_form_attributes_for(action)
return unless action

cname = "FORM_ATTRIBUTES_#{action.upcase}"

self.class.const_get(cname) if self.class.const_defined?(cname)
end

def permitted_attributes
Expand Down
11 changes: 9 additions & 2 deletions lib/administrate/page/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ def initialize(dashboard, resource)

attr_reader :resource

def attributes
dashboard.form_attributes.map do |attribute|
def attributes(action = nil)
action =
case action
when "update" then "edit"
when "create" then "new"
else action
end

dashboard.form_attributes(action).map do |attribute|
attribute_field(dashboard, resource, attribute, :form)
end
end
Expand Down
66 changes: 66 additions & 0 deletions spec/dashboards/line_item_dashboard_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require "rails_helper"

describe LineItemDashboard do
let(:dashboard) { described_class.new }

describe "#form_attributes" do
let(:attributes) { dashboard.form_attributes(action) }

context "for nil action" do
let(:action) { nil }

it "returns the attributes from FORM_ATTRIBUTES" do
expect(attributes).to match(
%i[
order
product
quantity
unit_price
],
)
end
end

context "for a new action" do
let(:action) { "new" }

it "returns the attributes from FORM_ATTRIBUTES_NEW" do
expect(attributes).to match(
%i[
order
product
],
)
end
end

context "for an edit action" do
let(:action) { "edit" }

it "returns the attributes from FORM_ATTRIBUTES_EDIT" do
expect(attributes).to match(
%i[
order
product
quantity
],
)
end
end

context "for an invalid action" do
let(:action) { "invalid" }

it "returns the attributes from FORM_ATTRIBUTES" do
expect(attributes).to match(
%i[
order
product
quantity
unit_price
],
)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/dashboards/order_dashboard_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

describe CustomerDashboard do
describe OrderDashboard do
describe "#permitted_attributes" do
it "returns the attribute name by default" do
dashboard = OrderDashboard.new
Expand Down
2 changes: 2 additions & 0 deletions spec/example_app/app/dashboards/line_item_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class LineItemDashboard < Administrate::BaseDashboard
COLLECTION_ATTRIBUTES = ATTRIBUTES + [:total_price]
SHOW_PAGE_ATTRIBUTES = ATTRIBUTES + [:total_price]
FORM_ATTRIBUTES = ATTRIBUTES
FORM_ATTRIBUTES_NEW = ATTRIBUTES - %i[quantity unit_price]
FORM_ATTRIBUTES_EDIT = ATTRIBUTES - [:unit_price]

def display_resource(line_item)
"Line Item #%04d" % line_item.id
Expand Down
46 changes: 46 additions & 0 deletions spec/lib/pages/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,50 @@
expect(page.page_title).to eq("Worf")
end
end

describe "#attributes" do
let(:line_item) { build(:line_item) }
let(:page) { described_class.new(LineItemDashboard.new, line_item) }
let(:attributes) { page.attributes(action).map(&:attribute) }

context "for a new action" do
let(:action) { "new" }

it "returns the attributes from FORM_ATTRIBUTES_NEW" do
expect(attributes).to match(
%i[
order
product
],
)
end
end

context "for a create action" do
let(:action) { "create" }

it "returns the attributes from FORM_ATTRIBUTES_NEW" do
expect(attributes).to match(
%i[
order
product
],
)
end
end

context "for an update action" do
let(:action) { "update" }

it "returns the attributes from FORM_ATTRIBUTES_EDIT" do
expect(attributes).to match(
%i[
order
product
quantity
],
)
end
end
end
end