Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
Add option to select target users to show global banner message.
Browse files Browse the repository at this point in the history
  • Loading branch information
akiko-pusu committed Feb 11, 2020
1 parent a18aae5 commit 0945293
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/models/global_banner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class GlobalBanner < Setting
GLOBAL_BANNER_DEFAULT_SETTING = {
enable: 'false',
banner_description: 'exp. Information about upcoming Service Interruption.',
only_authenticated: nil,
display_for: 'all',
display_only_login_page: nil,
type: 'info',
display_part: 'both',
Expand Down
13 changes: 9 additions & 4 deletions app/views/global_banner/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
</p>

<p>
<%= content_tag(:label, l(:label_display_authenticated_user_only))%>
<%= hidden_field_tag('setting[only_authenticated]', false).html_safe %>
<%= check_box_tag 'setting[only_authenticated]', true, setting['only_authenticated'] == 'true' %>
<%= content_tag(:label, l(:setting_banner_display_for, default: 'Display for'))%>
<%= select_tag('setting[display_for]',
options_for_select([[l(:label_anonymous_only, default: 'Anonymous'),'anonymous'],
[l(:label_authenticated_only, default: 'Authenticated'),'authenticated'],
[l(:label_display_all, default: 'All'), 'all']],
{ selected: setting['display_for'] })) %>
</p>

<p>
Expand All @@ -29,7 +32,9 @@
<%= content_tag(:label, l(:setting_banner_display_part))%>
<%= select_tag('setting[display_part]',
options_for_select([[l(:label_header_only),'header'],
[l(:label_footer_only),'footer'],[l(:label_both),'both']],setting['display_part'])) %>
[l(:label_footer_only),'footer'],
[l(:label_both),'both']],
setting['display_part'])) %>
<%= hidden_field_tag('setting[display_only_login_page]', false).html_safe %>
<%= check_box_tag 'setting[display_only_login_page]', true, setting['display_only_login_page'] == 'true' %>
<%= l(:label_check_if_banner_show_only_login_page) %>
Expand Down
12 changes: 10 additions & 2 deletions lib/banners/application_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,17 @@ def should_display_on_current_page?(context, setting)
(context[:controller].action_name != 'login')) &&
(setting['display_only_login_page'] == 'true')

return false if !User.current.logged? && setting['only_authenticated'] == 'true'
return should_display_for?(setting)
end

def should_display_for?(setting)
target = setting['display_for'] || 'all'
return true if target == 'all'

return true if target == 'authenticated' && User.current.logged?
return true if target == 'anonymous' && User.current.anonymous?

true
false
end
end
end
8 changes: 4 additions & 4 deletions test/integration/layout_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ def test_display_only_for_login_page
enable: 'true', type: 'warn', display_part: 'both',
use_timer: 'false',
banner_description: 'h1. Test data.',
display_only_login_page: 'true',
only_authenticated: 'true'
display_for: 'authenticated'
} }

# Session is cleared
Expand All @@ -100,11 +99,12 @@ def test_display_more_link
use_timer: 'false',
banner_description: 'h1. Test data.',
display_only_login_page: 'false',
only_authenticated: 'false',
display_for: 'authenticated',
related_link: ''
} }

get '/'
assert_select 'div.banner_area', 1
assert_select 'div.banner_more_info', 0

# Update setting.
Expand All @@ -114,7 +114,7 @@ def test_display_more_link
use_timer: 'false',
banner_description: 'h1. Test data.',
display_only_login_page: 'false',
only_authenticated: 'false',
display_for: 'all',
related_link: 'http://www.redmine.org/'
} }

Expand Down

0 comments on commit 0945293

Please sign in to comment.