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

make permitted_attributes support action #2356

Merged
merged 6 commits into from
Apr 19, 2023
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/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def apply_collection_includes(relation)

def resource_params
params.require(resource_class.model_name.param_key).
permit(dashboard.permitted_attributes).
permit(dashboard.permitted_attributes(action_name)).
transform_values { |v| read_param_value(v) }
end

Expand Down
11 changes: 9 additions & 2 deletions lib/administrate/base_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ def all_attributes
end

def form_attributes(action = nil)
action =
case action
when "update" then "edit"
when "create" then "new"
else action
end
specific_form_attributes_for(action) || self.class::FORM_ATTRIBUTES
end

Expand All @@ -62,11 +68,12 @@ def specific_form_attributes_for(action)
self.class.const_get(cname) if self.class.const_defined?(cname)
end

def permitted_attributes
form_attributes.map do |attr|
def permitted_attributes(action = nil)
form_attributes(action).map do |attr|
attribute_types[attr].permitted_attribute(
attr,
resource_class: self.class.model,
action: action,
)
end.uniq
end
Expand Down
7 changes: 0 additions & 7 deletions lib/administrate/page/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ def initialize(dashboard, resource)
attr_reader :resource

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module <%= namespace.to_s.camelize %>
#
# def resource_params
# params.require(resource_class.model_name.param_key).
# permit(dashboard.permitted_attributes).
# permit(dashboard.permitted_attributes(action_name)).
# transform_values { |value| value == "" ? nil : value }
# end

Expand Down
19 changes: 19 additions & 0 deletions spec/dashboards/line_item_dashboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,23 @@
end
end
end

describe "#permitted_attributes" do
it "returns the attribute name by default" do
attributes = ["order_id", "product_id", :quantity, :unit_price]
expect(dashboard.permitted_attributes).to eq(attributes)
end

it "returns the attribute name for new/create actions" do
attributes = ["order_id", "product_id"]
expect(dashboard.permitted_attributes("new")).to eq(attributes)
expect(dashboard.permitted_attributes("create")).to eq(attributes)
end

it "returns the attribute name for edit/update actions" do
attributes = ["order_id", "product_id", :quantity]
expect(dashboard.permitted_attributes("edit")).to eq(attributes)
expect(dashboard.permitted_attributes("update")).to eq(attributes)
end
end
end