Skip to content

Commit 7afd445

Browse files
committed
Add context to Dashboard and Field
1 parent d62db4e commit 7afd445

File tree

14 files changed

+61
-21
lines changed

14 files changed

+61
-21
lines changed

app/controllers/administrate/application_controller.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ def sorting_params
195195
end
196196

197197
def dashboard
198-
@dashboard ||= dashboard_class.new
198+
@dashboard ||= dashboard_class.new.tap do |d|
199+
d.context = self
200+
end
199201
end
200202

201203
def requested_resource
@@ -225,7 +227,7 @@ def apply_collection_includes(relation)
225227

226228
def resource_params
227229
params.require(resource_class.model_name.param_key)
228-
.permit(dashboard.permitted_attributes(action_name, self))
230+
.permit(dashboard.permitted_attributes(action_name))
229231
.transform_values { |v| read_param_value(v) }
230232
end
231233

lib/administrate/base_dashboard.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def all_attributes
5151
attribute_types.keys
5252
end
5353

54-
def form_attributes(action = nil, _context = nil)
54+
def form_attributes(action = nil)
5555
action =
5656
case action
5757
when "update" then "edit"
@@ -69,8 +69,8 @@ def specific_form_attributes_for(action)
6969
self.class.const_get(cname) if self.class.const_defined?(cname)
7070
end
7171

72-
def permitted_attributes(action = nil, context = nil)
73-
attributes = form_attributes(action, context)
72+
def permitted_attributes(action = nil)
73+
attributes = form_attributes(action)
7474

7575
if attributes.is_a? Hash
7676
attributes = attributes.values.flatten
@@ -126,6 +126,8 @@ def item_associations
126126
attribute_associated attributes
127127
end
128128

129+
attr_accessor :context
130+
129131
private
130132

131133
def attribute_not_found_message(attr)

lib/administrate/field/associative.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ def html_controller
5353
private
5454

5555
def associated_dashboard
56-
"#{associated_class_name}Dashboard".constantize.new
56+
"#{associated_class_name}Dashboard".constantize.new.tap do |d|
57+
d.context = context
58+
end
5759
end
5860

5961
def primary_key

lib/administrate/field/base.rb

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def required?
9797
end
9898

9999
attr_reader :attribute, :data, :options, :page, :resource
100+
attr_accessor :context
100101
end
101102
end
102103
end

lib/administrate/field/has_many.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ def associated_collection(order = self.order)
2626
associated_dashboard,
2727
order: order,
2828
collection_attributes: options[:collection_attributes]
29-
)
29+
).tap do |page|
30+
page.context = context
31+
page.dashboard_context = context
32+
end
3033
end
3134

3235
def attribute_key

lib/administrate/field/has_one.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,20 @@ def nested_form
3434
@nested_form ||= Administrate::Page::Form.new(
3535
resolver.dashboard_class.new,
3636
data || resolver.resource_class.new
37-
)
37+
).tap do |page|
38+
page.context = context
39+
page.dashboard_context = context
40+
end
3841
end
3942

4043
def nested_show
4144
@nested_show ||= Administrate::Page::Show.new(
4245
resolver.dashboard_class.new,
4346
data || resolver.resource_class.new
44-
)
47+
) do |page|
48+
page.context = context
49+
page.dashboard_context = context
50+
end
4551
end
4652

4753
def linkable?

lib/administrate/field/polymorphic.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def selected_global_id
2626
private
2727

2828
def associated_dashboard(klass = data.class)
29-
"#{klass.name}Dashboard".constantize.new
29+
"#{klass.name}Dashboard".constantize.new.tap do |d|
30+
d.context = context
31+
end
3032
end
3133

3234
def classes

lib/administrate/page/base.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,21 @@ def item_associations
2929

3030
attr_accessor :context
3131

32+
def dashboard_context=(context)
33+
dashboard.context = context
34+
end
35+
3236
private
3337

3438
def attribute_field(dashboard, resource, attribute_name, page)
3539
field = dashboard.attribute_type_for(attribute_name)
36-
field.new(attribute_name, nil, page, resource: resource)
40+
field.new(attribute_name, nil, page, resource: resource).tap do |f|
41+
f.context = context
42+
end
43+
end
44+
45+
def get_attribute_value(resource, attribute_name)
46+
resource.public_send(attribute_name)
3747
end
3848

3949
attr_reader :dashboard, :options

lib/administrate/page/form.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def initialize(dashboard, resource)
1111
attr_reader :resource
1212

1313
def attributes(action = nil)
14-
attributes = dashboard.form_attributes(action, context)
14+
attributes = dashboard.form_attributes(action)
1515

1616
if attributes.is_a? Array
1717
attributes = {"" => attributes}

spec/dashboards/order_dashboard_spec.rb

+12-8
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,25 @@
1919
context "when the user is not an admin" do
2020
it "not returns attributes with customer_id" do
2121
dashboard = OrderDashboard.new
22+
dashboard.context = ctx_with_non_admin_user
2223
expect(
23-
dashboard.permitted_attributes("new", ctx_with_non_admin_user)
24+
dashboard.permitted_attributes("new")
2425
).not_to include("customer_id")
2526
expect(
26-
dashboard.permitted_attributes("create", ctx_with_non_admin_user)
27+
dashboard.permitted_attributes("create")
2728
).not_to include("customer_id")
2829
end
2930
end
3031

3132
context "when the user is an admin" do
3233
it "returns attributes with customer_id" do
3334
dashboard = OrderDashboard.new
35+
dashboard.context = ctx_with_admin_user
3436
expect(
35-
dashboard.permitted_attributes("new", ctx_with_admin_user)
37+
dashboard.permitted_attributes("new")
3638
).to include("customer_id")
3739
expect(
38-
dashboard.permitted_attributes("create", ctx_with_admin_user)
40+
dashboard.permitted_attributes("create")
3941
).to include("customer_id")
4042
end
4143
end
@@ -52,23 +54,25 @@
5254
context "when the user is not an admin" do
5355
it "not returns attributes with customer_id" do
5456
dashboard = OrderDashboard.new
57+
dashboard.context = ctx_with_non_admin_user
5558
expect(
56-
dashboard.permitted_attributes("edit", ctx_with_non_admin_user)
59+
dashboard.permitted_attributes("edit")
5760
).not_to include("customer_id")
5861
expect(
59-
dashboard.permitted_attributes("update", ctx_with_non_admin_user)
62+
dashboard.permitted_attributes("update")
6063
).not_to include("customer_id")
6164
end
6265
end
6366

6467
context "when the user is an admin" do
6568
it "also no returns attributes with customer_id" do
6669
dashboard = OrderDashboard.new
70+
dashboard.context = ctx_with_admin_user
6771
expect(
68-
dashboard.permitted_attributes("edit", ctx_with_admin_user)
72+
dashboard.permitted_attributes("edit")
6973
).not_to include("customer_id")
7074
expect(
71-
dashboard.permitted_attributes("update", ctx_with_admin_user)
75+
dashboard.permitted_attributes("update")
7276
).not_to include("customer_id")
7377
end
7478
end

spec/example_app/app/dashboards/order_dashboard.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class OrderDashboard < Administrate::BaseDashboard
7070
)
7171
.freeze
7272

73-
def form_attributes(action = nil, context = nil)
73+
def form_attributes(action = nil)
7474
if %w[new create].include?(action.to_s) && context.try(:pundit_user).try(:admin?)
7575
super
7676
else

spec/lib/fields/belongs_to_spec.rb

+3
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@
186186
allow_any_instance_of(FooDashboard).to(
187187
receive(:display_resource).and_return(uuid)
188188
)
189+
allow_any_instance_of(FooDashboard).to(
190+
receive(:context=).with(nil).and_return(nil)
191+
)
189192
end
190193

191194
it "is the associated table key that matches our foreign key" do

spec/lib/fields/has_many_spec.rb

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252

5353
stub_const("FooDashboard", Class.new)
5454
allow(FooDashboard).to receive(:new).and_return(dashboard_double)
55+
allow(dashboard_double).to receive(:context=).with(nil).and_return(nil)
5556
end
5657

5758
it "determines what dashboard is used to present the association" do
@@ -79,6 +80,9 @@
7980
allow_any_instance_of(FooDashboard).to(
8081
receive(:display_resource).and_return(uuid)
8182
)
83+
allow_any_instance_of(FooDashboard).to(
84+
receive(:context=).with(nil).and_return(nil)
85+
)
8286
end
8387

8488
it "is the key matching the associated foreign key" do

spec/lib/fields/polymorphic_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
def display_resource(*)
4545
:success
4646
end
47+
attr_accessor :context
4748
end
4849

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

0 commit comments

Comments
 (0)