Skip to content

Commit

Permalink
Add context to Dashboard and Field
Browse files Browse the repository at this point in the history
  • Loading branch information
goosys committed Nov 5, 2024
1 parent d62db4e commit 7afd445
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 21 deletions.
6 changes: 4 additions & 2 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ def sorting_params
end

def dashboard
@dashboard ||= dashboard_class.new
@dashboard ||= dashboard_class.new.tap do |d|
d.context = self
end
end

def requested_resource
Expand Down Expand Up @@ -225,7 +227,7 @@ def apply_collection_includes(relation)

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

Expand Down
8 changes: 5 additions & 3 deletions lib/administrate/base_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def all_attributes
attribute_types.keys
end

def form_attributes(action = nil, _context = nil)
def form_attributes(action = nil)
action =
case action
when "update" then "edit"
Expand All @@ -69,8 +69,8 @@ def specific_form_attributes_for(action)
self.class.const_get(cname) if self.class.const_defined?(cname)
end

def permitted_attributes(action = nil, context = nil)
attributes = form_attributes(action, context)
def permitted_attributes(action = nil)
attributes = form_attributes(action)

if attributes.is_a? Hash
attributes = attributes.values.flatten
Expand Down Expand Up @@ -126,6 +126,8 @@ def item_associations
attribute_associated attributes
end

attr_accessor :context

private

def attribute_not_found_message(attr)
Expand Down
4 changes: 3 additions & 1 deletion lib/administrate/field/associative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def html_controller
private

def associated_dashboard
"#{associated_class_name}Dashboard".constantize.new
"#{associated_class_name}Dashboard".constantize.new.tap do |d|
d.context = context
end
end

def primary_key
Expand Down
1 change: 1 addition & 0 deletions lib/administrate/field/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def required?
end

attr_reader :attribute, :data, :options, :page, :resource
attr_accessor :context
end
end
end
5 changes: 4 additions & 1 deletion lib/administrate/field/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def associated_collection(order = self.order)
associated_dashboard,
order: order,
collection_attributes: options[:collection_attributes]
)
).tap do |page|
page.context = context
page.dashboard_context = context
end
end

def attribute_key
Expand Down
10 changes: 8 additions & 2 deletions lib/administrate/field/has_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@ def nested_form
@nested_form ||= Administrate::Page::Form.new(
resolver.dashboard_class.new,
data || resolver.resource_class.new
)
).tap do |page|
page.context = context
page.dashboard_context = context
end
end

def nested_show
@nested_show ||= Administrate::Page::Show.new(
resolver.dashboard_class.new,
data || resolver.resource_class.new
)
) do |page|
page.context = context
page.dashboard_context = context
end
end

def linkable?
Expand Down
4 changes: 3 additions & 1 deletion lib/administrate/field/polymorphic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def selected_global_id
private

def associated_dashboard(klass = data.class)
"#{klass.name}Dashboard".constantize.new
"#{klass.name}Dashboard".constantize.new.tap do |d|
d.context = context
end
end

def classes
Expand Down
12 changes: 11 additions & 1 deletion lib/administrate/page/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@ def item_associations

attr_accessor :context

def dashboard_context=(context)
dashboard.context = context
end

private

def attribute_field(dashboard, resource, attribute_name, page)
field = dashboard.attribute_type_for(attribute_name)
field.new(attribute_name, nil, page, resource: resource)
field.new(attribute_name, nil, page, resource: resource).tap do |f|
f.context = context
end
end

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

attr_reader :dashboard, :options
Expand Down
2 changes: 1 addition & 1 deletion lib/administrate/page/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(dashboard, resource)
attr_reader :resource

def attributes(action = nil)
attributes = dashboard.form_attributes(action, context)
attributes = dashboard.form_attributes(action)

if attributes.is_a? Array
attributes = {"" => attributes}
Expand Down
20 changes: 12 additions & 8 deletions spec/dashboards/order_dashboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,25 @@
context "when the user is not an admin" do
it "not returns attributes with customer_id" do
dashboard = OrderDashboard.new
dashboard.context = ctx_with_non_admin_user
expect(
dashboard.permitted_attributes("new", ctx_with_non_admin_user)
dashboard.permitted_attributes("new")
).not_to include("customer_id")
expect(
dashboard.permitted_attributes("create", ctx_with_non_admin_user)
dashboard.permitted_attributes("create")
).not_to include("customer_id")
end
end

context "when the user is an admin" do
it "returns attributes with customer_id" do
dashboard = OrderDashboard.new
dashboard.context = ctx_with_admin_user
expect(
dashboard.permitted_attributes("new", ctx_with_admin_user)
dashboard.permitted_attributes("new")
).to include("customer_id")
expect(
dashboard.permitted_attributes("create", ctx_with_admin_user)
dashboard.permitted_attributes("create")
).to include("customer_id")
end
end
Expand All @@ -52,23 +54,25 @@
context "when the user is not an admin" do
it "not returns attributes with customer_id" do
dashboard = OrderDashboard.new
dashboard.context = ctx_with_non_admin_user
expect(
dashboard.permitted_attributes("edit", ctx_with_non_admin_user)
dashboard.permitted_attributes("edit")
).not_to include("customer_id")
expect(
dashboard.permitted_attributes("update", ctx_with_non_admin_user)
dashboard.permitted_attributes("update")
).not_to include("customer_id")
end
end

context "when the user is an admin" do
it "also no returns attributes with customer_id" do
dashboard = OrderDashboard.new
dashboard.context = ctx_with_admin_user
expect(
dashboard.permitted_attributes("edit", ctx_with_admin_user)
dashboard.permitted_attributes("edit")
).not_to include("customer_id")
expect(
dashboard.permitted_attributes("update", ctx_with_admin_user)
dashboard.permitted_attributes("update")
).not_to include("customer_id")
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/example_app/app/dashboards/order_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class OrderDashboard < Administrate::BaseDashboard
)
.freeze

def form_attributes(action = nil, context = nil)
def form_attributes(action = nil)
if %w[new create].include?(action.to_s) && context.try(:pundit_user).try(:admin?)
super
else
Expand Down
3 changes: 3 additions & 0 deletions spec/lib/fields/belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@
allow_any_instance_of(FooDashboard).to(
receive(:display_resource).and_return(uuid)
)
allow_any_instance_of(FooDashboard).to(
receive(:context=).with(nil).and_return(nil)
)
end

it "is the associated table key that matches our foreign key" do
Expand Down
4 changes: 4 additions & 0 deletions spec/lib/fields/has_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

stub_const("FooDashboard", Class.new)
allow(FooDashboard).to receive(:new).and_return(dashboard_double)
allow(dashboard_double).to receive(:context=).with(nil).and_return(nil)
end

it "determines what dashboard is used to present the association" do
Expand Down Expand Up @@ -79,6 +80,9 @@
allow_any_instance_of(FooDashboard).to(
receive(:display_resource).and_return(uuid)
)
allow_any_instance_of(FooDashboard).to(
receive(:context=).with(nil).and_return(nil)
)
end

it "is the key matching the associated foreign key" do
Expand Down
1 change: 1 addition & 0 deletions spec/lib/fields/polymorphic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
def display_resource(*)
:success
end
attr_accessor :context
end

field = Administrate::Field::Polymorphic.new(:foo, Thing.new, :show)
Expand Down

0 comments on commit 7afd445

Please sign in to comment.