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
11 changes: 11 additions & 0 deletions lib/tiny_admin/raw_html.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module TinyAdmin
class RawHtml
attr_reader :to_s

def initialize(value)
@to_s = value.to_s
end
end
end
4 changes: 4 additions & 0 deletions lib/tiny_admin/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
module TinyAdmin
class Support
class << self
def raw_html(value)
TinyAdmin::RawHtml.new(value)
end

def call(value, options: [])
options.inject(value) { |result, message| result&.send(message) } if value && options&.any?
end
Expand Down
14 changes: 12 additions & 2 deletions lib/tiny_admin/views/components/field_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,25 @@ def view_template
if field.options[:link_to]
a(href: TinyAdmin.route_for(field.options[:link_to], reference: translated_value)) {
span(class: value_class) {
field.apply_call_option(record) || translated_value
render_value(field.apply_call_option(record) || translated_value)
}
}
else
span(class: value_class) {
translated_value
render_value(translated_value)
}
end
end

private

def render_value(val)
if val.is_a?(TinyAdmin::RawHtml)
unsafe_raw(val.to_s)
else
val
end
end
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/dummy_rails/app/models/author.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ class Author < ApplicationRecord
def to_s
"#{name} (#{age})"
end

def stats
[
"Posts: <b>#{posts.count}</b>",
"Published: <b>#{published_posts.count}</b>",
"Recent: <b>#{recent_posts.count}</b>"
]
end
end
2 changes: 1 addition & 1 deletion spec/dummy_rails/app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Post < ApplicationRecord
validates :title, allow_blank: false, presence: true

scope :published, -> { where(published: true) }
scope :recents, -> { where('created_at > ?', Date.current - 8.months) }
scope :recents, -> { where('created_at > ?', Time.current - 1.week) }

# # override a field - can be dangerous
# def title
Expand Down
5 changes: 5 additions & 0 deletions spec/dummy_rails/app/tiny_admin/admin_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
class AdminHelper < TinyAdmin::Support
class << self
def multiline(array, options: [])
raw_html array.join("<br/>")
end
end
end
17 changes: 17 additions & 0 deletions spec/dummy_rails/config/tiny_admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ sections:
- id
- name
- email
- field: stats
header: Some stats
method: multiline
links:
- show
- sample_mem
show:
attributes:
- id
- name
- email
- age
- field: stats
header: Some stats
method: multiline
- created_at
- updated_at
links:
- show
- sample_mem
Expand Down
33 changes: 33 additions & 0 deletions spec/lib/tiny_admin/views/components/field_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,39 @@
end
end

describe "with a RawHtml value" do
let(:field) { TinyAdmin::Field.new(name: "items", type: :string, title: "Items", options: { method: "multiline" }) }
let(:record) { double("record", id: 1) } # rubocop:disable RSpec/VerifiedDoubles

before do
allow(TinyAdmin.settings.helper_class).to receive(:multiline)
.and_return(TinyAdmin::RawHtml.new("1<br/>2<br/>3"))
end

it "renders the value as raw HTML without escaping", :aggregate_failures do
html = described_class.new(field, [1, 2, 3], record: record).call
expect(html).to include("<span>")
expect(html).to include("1<br/>2<br/>3")
expect(html).not_to include("&lt;br/&gt;")
end
end

describe "with a RawHtml value inside a link" do
let(:field) { TinyAdmin::Field.new(name: "items", type: :string, title: "Items", options: { method: "multiline", link_to: "posts" }) }
let(:record) { double("record", id: 1) } # rubocop:disable RSpec/VerifiedDoubles

before do
allow(TinyAdmin.settings.helper_class).to receive(:multiline)
.and_return(TinyAdmin::RawHtml.new("1<br/>2<br/>3"))
end

it "renders the value as raw HTML inside a link without escaping", :aggregate_failures do
html = described_class.new(field, [1, 2, 3], record: record).call
expect(html).to include("<a")
expect(html).to include("<span>1<br/>2<br/>3</span>")
end
end

describe "with value_class option" do
let(:field) { TinyAdmin::Field.new(name: "status", type: :string, title: "Status", options: { options: ["value_class"] }) }
let(:record) { double("record", id: 1) } # rubocop:disable RSpec/VerifiedDoubles
Expand Down
Loading