Skip to content

Commit 97aabff

Browse files
committed
fix(views): guard against nil values and fix flash CSS classes
Fix params_to_s pushing [string] instead of string and add URL encoding via CGI.escape. Fix Flash component using "notices" CSS class for all message types instead of "warnings"/"errors". Add nil guard on Head#style_links and Store#prepare_sections navbar. Add rescue in Support.to_date for values that do not respond to to_date.
1 parent 4dad7be commit 97aabff

6 files changed

Lines changed: 33 additions & 27 deletions

File tree

lib/tiny_admin/store.rb

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,26 @@ def prepare_sections(sections, logout:)
2222
end
2323

2424
slug = section[:slug].to_s
25-
case section[:type]&.to_sym
26-
when :content
27-
list << add_content_section(slug, section)
28-
when :page
29-
list << add_page_section(slug, section)
30-
when :resource
31-
list << add_resource_section(slug, section)
32-
when :url
33-
list << add_url_section(slug, section)
25+
item = case section[:type]&.to_sym
26+
when :content then add_content_section(slug, section)
27+
when :page then add_page_section(slug, section)
28+
when :resource then add_resource_section(slug, section)
29+
when :url then add_url_section(slug, section)
3430
end
31+
list << item if item
3532
end
3633
navbar << logout if logout
3734
end
3835

3936
private
4037

4138
def add_content_section(slug, section)
42-
pages[slug] = { class: settings.content_page, content: section[:content], widgets: section[:widgets] }
39+
pages[slug] = {class: settings.content_page, content: section[:content], widgets: section[:widgets]}
4340
TinyAdmin::Section.new(name: section[:name], slug: slug)
4441
end
4542

4643
def add_page_section(slug, section)
47-
pages[slug] = { class: to_class(section[:page]) }
44+
pages[slug] = {class: to_class(section[:page])}
4845
TinyAdmin::Section.new(name: section[:name], slug: slug)
4946
end
5047

@@ -56,7 +53,7 @@ def add_resource_section(slug, section)
5653
repository: to_class(section[:repository] || settings.repository)
5754
)
5855

59-
hidden = section[:options] && (section[:options].include?(:hidden) || section[:options].include?('hidden'))
56+
hidden = section[:options] && (section[:options].include?(:hidden) || section[:options].include?("hidden"))
6057
TinyAdmin::Section.new(name: section[:name], slug: slug) unless hidden
6158
end
6259

lib/tiny_admin/support.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ def round(value, options: [])
2424
end
2525

2626
def strftime(value, options: [])
27-
value&.strftime(options&.first || '%Y-%m-%d %H:%M')
27+
value&.strftime(options&.first || "%Y-%m-%d %H:%M")
2828
end
2929

3030
def to_date(value, options: [])
31-
value.to_date.to_s if value
31+
value&.to_date&.to_s
32+
rescue NoMethodError, ArgumentError
33+
value&.to_s
3234
end
3335

3436
def upcase(value, options: [])

lib/tiny_admin/utils.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# frozen_string_literal: true
22

3+
require "cgi"
4+
35
module TinyAdmin
46
module Utils
57
def params_to_s(params)
68
list = params.each_with_object([]) do |(param, value), result|
79
if value.is_a?(Hash)
8-
values = value.map { |key, val| "#{param}[#{key}]=#{val}" }
10+
values = value.map { |key, val| "#{CGI.escape(param.to_s)}[#{CGI.escape(key.to_s)}]=#{CGI.escape(val.to_s)}" }
911
result.concat(values)
1012
else
11-
result.push(["#{param}=#{value}"])
13+
result.push("#{CGI.escape(param.to_s)}=#{CGI.escape(value.to_s)}")
1214
end
1315
end
14-
list.join('&')
16+
list.join("&")
1517
end
1618

1719
def prepare_page(page_class, slug: nil, attributes: nil, options: nil, params: nil)
@@ -28,7 +30,7 @@ def prepare_page(page_class, slug: nil, attributes: nil, options: nil, params: n
2830
)
2931
attrs = attributes || {}
3032
attrs[:params] = params if params
31-
attrs[:widgets] = attrs[:widgets].map { to_class(_1) } if attrs[:widgets]
33+
attrs[:widgets] = attrs[:widgets].map { to_class(it) } if attrs[:widgets]
3234
page.update_attributes(attrs) unless attrs.empty?
3335
yield(page) if block_given?
3436
page.setup if page.respond_to?(:setup)
@@ -40,9 +42,9 @@ def to_class(klass)
4042
end
4143

4244
def humanize(string)
43-
return '' unless string
45+
return "" unless string
4446

45-
string.respond_to?(:humanize) ? string.humanize : string.tr('_', ' ').capitalize
47+
string.respond_to?(:humanize) ? string.humanize : string.tr("_", " ").capitalize
4648
end
4749
end
4850
end

lib/tiny_admin/views/components/flash.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ def view_template
1212
warnings = messages[:warnings]
1313
errors = messages[:errors]
1414

15-
div(class: 'flash') {
16-
div(class: 'notices alert alert-success', role: 'alert') { notices.join(', ') } if notices&.any?
17-
div(class: 'notices alert alert-warning', role: 'alert') { warnings.join(', ') } if warnings&.any?
18-
div(class: 'notices alert alert-danger', role: 'alert') { errors.join(', ') } if errors&.any?
15+
div(class: "flash") {
16+
div(class: "notices alert alert-success", role: "alert") { notices.join(", ") } if notices&.any?
17+
div(class: "warnings alert alert-warning", role: "alert") { warnings.join(", ") } if warnings&.any?
18+
div(class: "errors alert alert-danger", role: "alert") { errors.join(", ") } if errors&.any?
1919
}
2020
end
2121
end

lib/tiny_admin/views/components/head.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ class Head < BasicComponent
88

99
def view_template
1010
head {
11-
meta charset: 'utf-8'
12-
meta name: 'viewport', content: 'width=device-width, initial-scale=1'
11+
meta charset: "utf-8"
12+
meta name: "viewport", content: "width=device-width, initial-scale=1"
1313
title {
1414
page_title
1515
}
16-
style_links.each do |style_link|
16+
(style_links || []).each do |style_link|
1717
link(**style_link)
1818
end
1919
style { extra_styles } if extra_styles

spec/lib/tiny_admin/support_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@
8888
it "returns nil when value is nil" do
8989
expect(described_class.to_date(nil)).to be_nil
9090
end
91+
92+
it "falls back to to_s when to_date raises an error" do
93+
value = "not-a-date"
94+
expect(described_class.to_date(value)).to eq("not-a-date")
95+
end
9196
end
9297

9398
describe ".label_for" do

0 commit comments

Comments
 (0)