- | <%= simple_format(puzzle.question) %> |
+ <%= markdown(puzzle.question) %> |
<%= puzzle.answer %> |
- <%= puzzle.explanation %> |
+ <%= markdown(puzzle.explanation) %> |
<% if puzzle.link.present? %>
<%= link_to 'View', puzzle.link, target: '_blank' %>
diff --git a/app/views/puzzles/update.turbo_stream.erb b/app/views/puzzles/update.turbo_stream.erb
index 8f26218..e9dd213 100644
--- a/app/views/puzzles/update.turbo_stream.erb
+++ b/app/views/puzzles/update.turbo_stream.erb
@@ -1,9 +1,9 @@
- | <%= simple_format(@puzzle.question) %> |
+ <%= markdown(@puzzle.question) %> |
<%= @puzzle.answer %> |
- <%= @puzzle.explanation %> |
+ <%= markdown(@puzzle.explanation) %> |
<% if @puzzle.link.present? %>
<%= link_to 'View', @puzzle.link, target: '_blank' %>
diff --git a/db/seeds.rb b/db/seeds.rb
index 1df51e8..39a5bb6 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -6,32 +6,32 @@
# rates > 80 do not. Entries without `state` default to :pending.
puzzles = [
{
- question: "Ruby or Rails provided this method? Array.new(5) { |i| i * 2 }",
+ question: "Ruby or Rails provided this method? `Array.new(5) { |i| i * 2 }`",
answer: :ruby,
explanation: "`Array.new` is a core Ruby method that creates a new array with a specified size and optional block for initialization. This is part of Ruby’s core library."
},
{
- question: "Ruby or Rails provided this method? File.open('log.txt', 'w') { |file| file.write('Hello, World!') }",
+ question: "Ruby or Rails provided this method? `File.open('log.txt', 'w') { |file| file.write('Hello, World!') }`",
answer: :ruby,
explanation: "`File.open` is a core Ruby method for opening files. It’s part of Ruby's standard library for file handling, not part of Rails."
},
{
- question: "Ruby or Rails provided this method? render json: @user",
+ question: "Ruby or Rails provided this method? `render json: @user`",
answer: :rails,
explanation: "`render json:` is a Rails method used to render a JSON response from a controller action."
},
{
- question: "Ruby or Rails provided this method? link_to 'Home', root_path",
+ question: "Ruby or Rails provided this method? `link_to 'Home', root_path`",
answer: :rails,
explanation: "`link_to` is a Rails helper method that generates HTML links. `root_path` is a Rails path helper."
},
{
- question: "Ruby or Rails provided this method? params[:id]",
+ question: "Ruby or Rails provided this method? `params[:id]`",
answer: :rails,
explanation: "`params[:id]` is used in Rails to fetch query parameters or URL parameters in controller actions."
},
{
- question: "Ruby or Rails provided this method? before_action :authenticate_user!",
+ question: "Ruby or Rails provided this method? `before_action :authenticate_user!`",
answer: :rails,
explanation: "`before_action` is a Rails callback defined in `ActionController::Callbacks`. It runs specified methods before controller actions.",
state: :archived,
@@ -39,7 +39,7 @@
success_rate: 20
},
{
- question: "Ruby or Rails provided this method? 42.times { puts 'hello' }",
+ question: "Ruby or Rails provided this method? `42.times { puts 'hello' }`",
answer: :ruby,
explanation: "`Integer#times` is a core Ruby method that iterates a block a specified number of times.",
state: :archived,
@@ -47,7 +47,7 @@
success_rate: 40
},
{
- question: "Ruby or Rails provided this method? User.where(active: true).order(:name)",
+ question: "Ruby or Rails provided this method? `User.where(active: true).order(:name)`",
answer: :rails,
explanation: "`where` and `order` are ActiveRecord query methods provided by Rails to build SQL queries.",
state: :archived,
@@ -55,7 +55,7 @@
success_rate: 50
},
{
- question: "Ruby or Rails provided this method? 'hello world'.split(' ')",
+ question: "Ruby or Rails provided this method? `'hello world'.split(' ')`",
answer: :ruby,
explanation: "`String#split` is a core Ruby method that divides a string into an array based on a delimiter.",
state: :archived,
@@ -63,7 +63,7 @@
success_rate: 70
},
{
- question: "Ruby or Rails provided this method? flash[:notice] = 'Saved!'",
+ question: "Ruby or Rails provided this method? `flash[:notice] = 'Saved!'`",
answer: :rails,
explanation: "`flash` is a Rails feature provided by `ActionDispatch::Flash` for passing messages between requests.",
state: :archived,
@@ -71,7 +71,7 @@
success_rate: 80
},
{
- question: "Ruby or Rails provided this method? [1, 2, 3].reduce(:+)",
+ question: "Ruby or Rails provided this method? `[1, 2, 3].reduce(:+)`",
answer: :ruby,
explanation: "`Enumerable#reduce` (also `inject`) is a core Ruby method that combines elements using a binary operation.",
state: :archived,
@@ -79,12 +79,17 @@
success_rate: 90
},
{
- question: "Ruby or Rails provided this method? validates :email, presence: true, uniqueness: true",
+ question: "Ruby or Rails provided this method? `validates :email, presence: true, uniqueness: true`",
answer: :rails,
explanation: "`validates` is an ActiveModel/ActiveRecord method from Rails that adds validation rules to models.",
state: :archived,
sent_at: 1.day.ago,
success_rate: 100
+ },
+ {
+ question: "Ruby or Rails provided this method?\n\n```\nclass SomeClass\n def method\n whatever\n end\nend\n```",
+ answer: :rails,
+ explanation: "Not really important, just to show it renders blocks"
}
]
diff --git a/test/fixtures/puzzles.yml b/test/fixtures/puzzles.yml
index 45588a1..86e15e8 100644
--- a/test/fixtures/puzzles.yml
+++ b/test/fixtures/puzzles.yml
@@ -1,5 +1,5 @@
one:
- question: "Ruby or Rails provided this method? render json: @user"
+ question: "Ruby or Rails provided this method? `render json: @user`"
answer: "rails"
explanation: "This is a test puzzle"
link: "https://example.com"
@@ -7,7 +7,7 @@ one:
suggested_by: "test_user"
two:
- question: "Ruby or Rails provided this method? puts 'Hello, World!'"
+ question: "Ruby or Rails provided this method? `puts 'Hello, World!'`"
answer: "ruby"
explanation: "This is a test puzzle 2"
link: ""
diff --git a/test/helpers/markdown_helper_test.rb b/test/helpers/markdown_helper_test.rb
new file mode 100644
index 0000000..bc0f8cb
--- /dev/null
+++ b/test/helpers/markdown_helper_test.rb
@@ -0,0 +1,27 @@
+require "test_helper"
+
+class MarkdownHelperTest < ActionView::TestCase
+ include ApplicationHelper
+
+ test "renders `code` with code tag" do
+ puzzle_content = "Some content with `some code` in it"
+ as_html = markdown(puzzle_content)
+
+ assert_includes as_html, "some code"
+ end
+
+ test "renders ```code``` with code block" do
+ puzzle_content = <<~CONTENT
+ Some content with
+
+ ```
+ class ACodeBlock
+ end
+ ```
+ in it
+ CONTENT
+ as_html = markdown(puzzle_content)
+
+ assert_includes as_html, "class ACodeBlock"
+ end
+end
| |