Skip to content

Commit ba8559f

Browse files
committed
refactor(views): extract shared Attributes, ActionsButtons, and ErrorPage
DRY up duplicated update_attributes into a Views::Attributes module shared by BasicLayout and BasicComponent. Extract inline actions_buttons methods from Index and Show into a reusable ActionsButtons component. Consolidate three identical error page view_templates into an ErrorPage base class. Also adds nil guard on field access in Index and Show table/record iteration.
1 parent e9a6776 commit ba8559f

File tree

10 files changed

+79
-61
lines changed

10 files changed

+79
-61
lines changed

lib/tiny_admin/views/actions/index.rb

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def table_body
7575
attributes = prepare_record.call(record)
7676
attributes.each do |key, value|
7777
field = fields[key]
78+
next unless field
79+
7880
td(class: "field-value-#{field.name} field-value-type-#{field.type}") {
7981
render TinyAdmin.settings.components[:field_value].new(field, value, record: record)
8082
}
@@ -110,16 +112,9 @@ def table_body
110112
end
111113

112114
def actions_buttons
113-
ul(class: "nav justify-content-end") {
114-
(actions || {}).each do |action, action_class|
115-
li(class: "nav-item mx-1") {
116-
href = TinyAdmin.route_for(slug, action: action)
117-
a(href: href, class: "nav-link btn btn-outline-secondary") {
118-
action_class.respond_to?(:title) ? action_class.title : action
119-
}
120-
}
121-
end
122-
}
115+
buttons = TinyAdmin::Views::Components::ActionsButtons.new
116+
buttons.update_attributes(actions: actions, slug: slug)
117+
render buttons
123118
end
124119
end
125120
end

lib/tiny_admin/views/actions/show.rb

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ def view_template
2525

2626
prepare_record.call(record).each do |key, value|
2727
field = fields[key]
28+
next unless field
29+
2830
div(class: "field-#{field.name} row lh-lg") {
29-
if field
30-
div(class: "field-header col-2") { field.options[:header] || field.title }
31-
end
31+
div(class: "field-header col-2") { field.options[:header] || field.title }
3232
div(class: "field-value col-10") {
3333
render TinyAdmin.settings.components[:field_value].new(field, value, record: record)
3434
}
@@ -43,16 +43,9 @@ def view_template
4343
private
4444

4545
def actions_buttons
46-
ul(class: "nav justify-content-end") {
47-
(actions || {}).each do |action, action_class|
48-
li(class: "nav-item mx-1") {
49-
href = TinyAdmin.route_for(slug, reference: reference, action: action)
50-
a(href: href, class: "nav-link btn btn-outline-secondary") {
51-
action_class.respond_to?(:title) ? action_class.title : action
52-
}
53-
}
54-
end
55-
}
46+
buttons = TinyAdmin::Views::Components::ActionsButtons.new
47+
buttons.update_attributes(actions: actions, slug: slug, reference: reference)
48+
render buttons
5649
end
5750
end
5851
end

lib/tiny_admin/views/attributes.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module TinyAdmin
4+
module Views
5+
module Attributes
6+
def update_attributes(attributes)
7+
attributes.each do |key, value|
8+
send("#{key}=", value)
9+
end
10+
end
11+
end
12+
end
13+
end

lib/tiny_admin/views/basic_layout.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,14 @@
33
module TinyAdmin
44
module Views
55
class BasicLayout < Phlex::HTML
6+
include Attributes
67
include Utils
78

89
attr_accessor :content, :params, :widgets
910

1011
def label_for(value, options: [])
1112
TinyAdmin.settings.helper_class.label_for(value, options: options)
1213
end
13-
14-
def update_attributes(attributes)
15-
attributes.each do |key, value|
16-
send("#{key}=", value)
17-
end
18-
end
1914
end
2015
end
2116
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
module TinyAdmin
4+
module Views
5+
module Components
6+
class ActionsButtons < BasicComponent
7+
attr_accessor :actions, :slug, :reference
8+
9+
def view_template
10+
ul(class: "nav justify-content-end") {
11+
(actions || {}).each do |action, action_class|
12+
li(class: "nav-item mx-1") {
13+
href = TinyAdmin.route_for(slug, reference: reference, action: action)
14+
a(href: href, class: "nav-link btn btn-outline-secondary") {
15+
action_class.respond_to?(:title) ? action_class.title : action
16+
}
17+
}
18+
end
19+
}
20+
end
21+
end
22+
end
23+
end
24+
end

lib/tiny_admin/views/components/basic_component.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ module TinyAdmin
44
module Views
55
module Components
66
class BasicComponent < Phlex::HTML
7-
def update_attributes(attributes)
8-
attributes.each do |key, value|
9-
send("#{key}=", value)
10-
end
11-
end
7+
include Attributes
128
end
139
end
1410
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
module TinyAdmin
4+
module Views
5+
module Pages
6+
class ErrorPage < DefaultLayout
7+
def view_template
8+
super do
9+
div(class: css_class) {
10+
h1(class: "title") { title }
11+
}
12+
end
13+
end
14+
15+
private
16+
17+
def css_class
18+
self.class.name.split("::").last
19+
.gsub(/([A-Z])/, '_\1')
20+
.sub(/^_/, "")
21+
.downcase
22+
end
23+
end
24+
end
25+
end
26+
end

lib/tiny_admin/views/pages/page_not_allowed.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@
33
module TinyAdmin
44
module Views
55
module Pages
6-
class PageNotAllowed < DefaultLayout
7-
def view_template
8-
super do
9-
div(class: "page_not_allowed") {
10-
h1(class: "title") { title }
11-
}
12-
end
13-
end
14-
6+
class PageNotAllowed < ErrorPage
157
def title
168
"Page not allowed"
179
end

lib/tiny_admin/views/pages/page_not_found.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@
33
module TinyAdmin
44
module Views
55
module Pages
6-
class PageNotFound < DefaultLayout
7-
def view_template
8-
super do
9-
div(class: "page_not_found") {
10-
h1(class: "title") { title }
11-
}
12-
end
13-
end
14-
6+
class PageNotFound < ErrorPage
157
def title
168
"Page not found"
179
end

lib/tiny_admin/views/pages/record_not_found.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@
33
module TinyAdmin
44
module Views
55
module Pages
6-
class RecordNotFound < DefaultLayout
7-
def view_template
8-
super do
9-
div(class: "record_not_found") {
10-
h1(class: "title") { title }
11-
}
12-
end
13-
end
14-
6+
class RecordNotFound < ErrorPage
157
def title
168
"Record not found"
179
end

0 commit comments

Comments
 (0)