Skip to content

Commit

Permalink
add (Flash) notice helper
Browse files Browse the repository at this point in the history
  • Loading branch information
rubyforbusiness committed Aug 1, 2024
1 parent 9eced5c commit 29024e8
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/phlex/rails/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ module Phlex::Rails::Helpers
autoload :MailTo, "phlex/rails/helpers/mail_to"
autoload :MonthField, "phlex/rails/helpers/month_field"
autoload :MonthFieldTag, "phlex/rails/helpers/month_field_tag"
autoload :Notice, "phlex/rails/helpers/notice"
autoload :NumberField, "phlex/rails/helpers/number_field"
autoload :NumberFieldTag, "phlex/rails/helpers/number_field_tag"
autoload :NumberToCurrency, "phlex/rails/helpers/number_to_currency"
Expand Down
8 changes: 8 additions & 0 deletions lib/phlex/rails/helpers/notice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

module Phlex::Rails::Helpers::Notice
extend Phlex::Rails::HelperMacros

# @!method notice(...)
register_value_helper :notice
end
5 changes: 5 additions & 0 deletions test/dummy/app/controllers/helpers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ def tag
def missing_helper
render Helpers::MissingHelperView.new
end

def notice_test # can't use 'notice' as the method name because it interferes with https://api.rubyonrails.org/v7.1.3.4/classes/ActionController/Flash/ClassMethods.html#method-i-add_flash_types
flash.now.notice = "My Flash Notice"
render Helpers::NoticeView.new
end
end
10 changes: 10 additions & 0 deletions test/dummy/app/views/helpers/notice_view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class Helpers::NoticeView < ApplicationView
include Phlex::Rails::Helpers::Notice

def view_template
# Comes from https://api.rubyonrails.org/v7.1.3.4/classes/ActionDispatch/Flash/FlashHash.html#method-i-notice
p { notice }
end
end
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
get "/helpers/form_with", to: "helpers#form_with"
get "/helpers/tag", to: "helpers#tag"
get "/helpers/missing_helper", to: "helpers#missing_helper"
get "/helpers/notice", to: "helpers#notice_test"

get "/rendering/partial_from_phlex", to: "rendering#partial_from_phlex"
get "/rendering/view_component_from_phlex", to: "rendering#view_component_from_phlex"
Expand Down
7 changes: 7 additions & 0 deletions test/phlex/helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class HelpersTest < ActionDispatch::IntegrationTest
assert_select "form > h1 + input[type='text'] + h1"
end

test "notice" do
get "/helpers/notice"

assert_response :success
assert_select "p", "My Flash Notice"
end

test "tag" do
get "/helpers/tag"

Expand Down

0 comments on commit 29024e8

Please sign in to comment.