Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions lib/tiny_admin/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ def prepare_sections(sections, logout:)
end

slug = section[:slug].to_s
case section[:type]&.to_sym
when :content
list << add_content_section(slug, section)
when :page
list << add_page_section(slug, section)
when :resource
list << add_resource_section(slug, section)
when :url
list << add_url_section(slug, section)
end
item = case section[:type]&.to_sym
when :content then add_content_section(slug, section)
when :page then add_page_section(slug, section)
when :resource then add_resource_section(slug, section)
when :url then add_url_section(slug, section)
end
list << item if item
end
navbar << logout if logout
end
Expand Down
4 changes: 3 additions & 1 deletion lib/tiny_admin/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def strftime(value, options: [])
end

def to_date(value, options: [])
value.to_date.to_s if value
value&.to_date&.to_s
rescue NoMethodError, ArgumentError
value&.to_s
end

def upcase(value, options: [])
Expand Down
6 changes: 4 additions & 2 deletions lib/tiny_admin/utils.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# frozen_string_literal: true

require "cgi"

module TinyAdmin
module Utils
def params_to_s(params)
list = params.each_with_object([]) do |(param, value), result|
if value.is_a?(Hash)
values = value.map { |key, val| "#{param}[#{key}]=#{val}" }
values = value.map { |key, val| "#{CGI.escape(param.to_s)}[#{CGI.escape(key.to_s)}]=#{CGI.escape(val.to_s)}" }
result.concat(values)
else
result.push(["#{param}=#{value}"])
result.push("#{CGI.escape(param.to_s)}=#{CGI.escape(value.to_s)}")
end
end
list.join("&")
Expand Down
15 changes: 5 additions & 10 deletions lib/tiny_admin/views/actions/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def table_body
attributes = prepare_record.call(record)
attributes.each do |key, value|
field = fields[key]
next unless field

td(class: "field-value-#{field.name} field-value-type-#{field.type}") {
render TinyAdmin.settings.components[:field_value].new(field, value, record: record)
}
Expand Down Expand Up @@ -110,16 +112,9 @@ def table_body
end

def actions_buttons
ul(class: "nav justify-content-end") {
(actions || {}).each do |action, action_class|
li(class: "nav-item mx-1") {
href = TinyAdmin.route_for(slug, action: action)
a(href: href, class: "nav-link btn btn-outline-secondary") {
action_class.respond_to?(:title) ? action_class.title : action
}
}
end
}
buttons = TinyAdmin::Views::Components::ActionsButtons.new
buttons.update_attributes(actions: actions, slug: slug)
render buttons
end
end
end
Expand Down
19 changes: 6 additions & 13 deletions lib/tiny_admin/views/actions/show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def view_template

prepare_record.call(record).each do |key, value|
field = fields[key]
next unless field

div(class: "field-#{field.name} row lh-lg") {
if field
div(class: "field-header col-2") { field.options[:header] || field.title }
end
div(class: "field-header col-2") { field.options[:header] || field.title }
div(class: "field-value col-10") {
render TinyAdmin.settings.components[:field_value].new(field, value, record: record)
}
Expand All @@ -43,16 +43,9 @@ def view_template
private

def actions_buttons
ul(class: "nav justify-content-end") {
(actions || {}).each do |action, action_class|
li(class: "nav-item mx-1") {
href = TinyAdmin.route_for(slug, reference: reference, action: action)
a(href: href, class: "nav-link btn btn-outline-secondary") {
action_class.respond_to?(:title) ? action_class.title : action
}
}
end
}
buttons = TinyAdmin::Views::Components::ActionsButtons.new
buttons.update_attributes(actions: actions, slug: slug, reference: reference)
render buttons
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions lib/tiny_admin/views/attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module TinyAdmin
module Views
module Attributes
def update_attributes(attributes)
attributes.each do |key, value|
send("#{key}=", value)
end
end
end
end
end
7 changes: 1 addition & 6 deletions lib/tiny_admin/views/basic_layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
module TinyAdmin
module Views
class BasicLayout < Phlex::HTML
include Attributes
include Utils

attr_accessor :content, :params, :widgets

def label_for(value, options: [])
TinyAdmin.settings.helper_class.label_for(value, options: options)
end

def update_attributes(attributes)
attributes.each do |key, value|
send("#{key}=", value)
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/tiny_admin/views/components/actions_buttons.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module TinyAdmin
module Views
module Components
class ActionsButtons < BasicComponent
attr_accessor :actions, :slug, :reference

def view_template
ul(class: "nav justify-content-end") {
(actions || {}).each do |action, action_class|
li(class: "nav-item mx-1") {
href = TinyAdmin.route_for(slug, reference: reference, action: action)
a(href: href, class: "nav-link btn btn-outline-secondary") {
action_class.respond_to?(:title) ? action_class.title : action
}
}
end
}
end
end
end
end
end
6 changes: 1 addition & 5 deletions lib/tiny_admin/views/components/basic_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ module TinyAdmin
module Views
module Components
class BasicComponent < Phlex::HTML
def update_attributes(attributes)
attributes.each do |key, value|
send("#{key}=", value)
end
end
include Attributes
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/tiny_admin/views/components/flash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def view_template

div(class: "flash") {
div(class: "notices alert alert-success", role: "alert") { notices.join(", ") } if notices&.any?
div(class: "notices alert alert-warning", role: "alert") { warnings.join(", ") } if warnings&.any?
div(class: "notices alert alert-danger", role: "alert") { errors.join(", ") } if errors&.any?
div(class: "warnings alert alert-warning", role: "alert") { warnings.join(", ") } if warnings&.any?
div(class: "errors alert alert-danger", role: "alert") { errors.join(", ") } if errors&.any?
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tiny_admin/views/components/head.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def view_template
title {
page_title
}
style_links.each do |style_link|
(style_links || []).each do |style_link|
link(**style_link)
end
style { extra_styles } if extra_styles
Expand Down
26 changes: 26 additions & 0 deletions lib/tiny_admin/views/pages/error_page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module TinyAdmin
module Views
module Pages
class ErrorPage < DefaultLayout
def view_template
super do
div(class: css_class) {
h1(class: "title") { title }
}
end
end

private

def css_class
self.class.name.split("::").last
.gsub(/([A-Z])/, '_\1')
.sub(/^_/, "")
.downcase
end
end
end
end
end
10 changes: 1 addition & 9 deletions lib/tiny_admin/views/pages/page_not_allowed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@
module TinyAdmin
module Views
module Pages
class PageNotAllowed < DefaultLayout
def view_template
super do
div(class: "page_not_allowed") {
h1(class: "title") { title }
}
end
end

class PageNotAllowed < ErrorPage
def title
"Page not allowed"
end
Expand Down
10 changes: 1 addition & 9 deletions lib/tiny_admin/views/pages/page_not_found.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@
module TinyAdmin
module Views
module Pages
class PageNotFound < DefaultLayout
def view_template
super do
div(class: "page_not_found") {
h1(class: "title") { title }
}
end
end

class PageNotFound < ErrorPage
def title
"Page not found"
end
Expand Down
10 changes: 1 addition & 9 deletions lib/tiny_admin/views/pages/record_not_found.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@
module TinyAdmin
module Views
module Pages
class RecordNotFound < DefaultLayout
def view_template
super do
div(class: "record_not_found") {
h1(class: "title") { title }
}
end
end

class RecordNotFound < ErrorPage
def title
"Record not found"
end
Expand Down
5 changes: 5 additions & 0 deletions spec/lib/tiny_admin/support_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@
it "returns nil when value is nil" do
expect(described_class.to_date(nil)).to be_nil
end

it "falls back to to_s when to_date raises an error" do
value = "not-a-date"
expect(described_class.to_date(value)).to eq("not-a-date")
end
end

describe ".label_for" do
Expand Down
Loading