Skip to content

Commit c720254

Browse files
committed
feat(views): pass context to widgets
1 parent d846cfe commit c720254

6 files changed

Lines changed: 29 additions & 7 deletions

File tree

lib/tiny_admin/views/actions/index.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ def view_template
4848
end
4949
}
5050

51-
render TinyAdmin::Views::Components::Widgets.new(widgets)
51+
context = { slug: slug, records: records, params: params }
52+
render TinyAdmin::Views::Components::Widgets.new(widgets, context: context)
5253
}
5354
end
5455
end

lib/tiny_admin/views/actions/show.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def view_template
3535
}
3636
end
3737

38-
render TinyAdmin::Views::Components::Widgets.new(widgets)
38+
context = { slug: slug, record: record, reference: reference, params: params }
39+
render TinyAdmin::Views::Components::Widgets.new(widgets, context: context)
3940
}
4041
end
4142
end

lib/tiny_admin/views/components/widgets.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ module TinyAdmin
44
module Views
55
module Components
66
class Widgets < BasicComponent
7-
def initialize(widgets)
7+
def initialize(widgets, context: {})
88
@widgets = widgets
9+
@context = context
910
end
1011

1112
def view_template
@@ -20,7 +21,7 @@ def view_template
2021
div(class: "col") {
2122
div(class: "card") {
2223
div(class: "card-body") {
23-
render widget.new
24+
render build_widget(widget)
2425
}
2526
}
2627
}
@@ -29,6 +30,20 @@ def view_template
2930
end
3031
}
3132
end
33+
34+
private
35+
36+
def build_widget(widget)
37+
key_params = [:key, :keyreq]
38+
if widget.instance_method(:initialize).arity != 0 ||
39+
widget.instance_method(:initialize).parameters.any? { |type, _| key_params.include?(type) }
40+
widget.new(context: @context)
41+
else
42+
widget.new
43+
end
44+
rescue ArgumentError
45+
widget.new
46+
end
3247
end
3348
end
3449
end

lib/tiny_admin/views/pages/content.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def view_template
1111
unsafe_raw(content)
1212
}
1313

14-
render TinyAdmin::Views::Components::Widgets.new(widgets)
14+
render TinyAdmin::Views::Components::Widgets.new(widgets, context: { params: params })
1515
}
1616
end
1717
end

lib/tiny_admin/views/pages/root.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Root < DefaultLayout
77
def view_template
88
super do
99
div(class: "root") {
10-
render TinyAdmin::Views::Components::Widgets.new(widgets)
10+
render TinyAdmin::Views::Components::Widgets.new(widgets, context: { params: params })
1111
}
1212
end
1313
end

sig/tiny_admin/views/components/widgets.rbs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ module TinyAdmin
33
module Components
44
class Widgets
55
@widgets: Array[untyped]?
6+
@context: Hash[Symbol, untyped]
67

7-
def initialize: (Array[untyped]?) -> void
8+
def initialize: (Array[untyped]?, ?context: Hash[Symbol, untyped]) -> void
89

910
def view_template: () ?{ (untyped) -> void } -> void
11+
12+
private
13+
14+
def build_widget: (Class) -> untyped
1015
end
1116
end
1217
end

0 commit comments

Comments
 (0)