diff --git a/app/assets/stylesheets/admin.scss b/app/assets/stylesheets/admin.scss index 5b2a19228..261cbe0c5 100644 --- a/app/assets/stylesheets/admin.scss +++ b/app/assets/stylesheets/admin.scss @@ -31,14 +31,6 @@ $table-header: #ecf1f6; .admin { @include admin-layout; - > header { - border-bottom: 1px solid #eee; - - > * { - @include full-width-background($adjust-margin: false, $adjust-padding: true); - } - } - h2 { font-weight: 100; margin-bottom: $line-height; @@ -60,86 +52,6 @@ $table-header: #ecf1f6; float: none; } - .top-links { - - a { - line-height: rem-calc($line-height * 1.5); - } - } - - .top-bar { - height: auto; - padding-top: $line-height / 2; - - @include breakpoint(small only) { - - .top-bar-left ul { - display: inline-block; - } - - .top-bar-right { - - > ul { - border-bottom: 0; - padding-bottom: 0; - margin-bottom: 0; - } - - .submenu { - position: initial; - } - - a { - font-weight: normal; - } - } - - [class^="icon-"] { - display: none; - } - } - - [class^="icon-"] { - font-size: $base-font-size; - } - - h1 { - margin-top: $line-height / 2; - margin-bottom: 0; - - @include breakpoint(medium) { - margin-top: 0; - } - - small { - color: inherit; - text-transform: uppercase; - } - - a { - color: inherit; - display: inline-block; - font-family: "Lato" !important; - font-size: rem-calc(24); - font-weight: lighter; - line-height: 1; - } - } - } - - .top-bar .menu > li { - - @include breakpoint(medium) { - height: auto !important; - } - } - - .title-bar { - color: inherit; - position: absolute; - right: 12px; - } - .notifications.unread-notifications::after { color: $admin-color; } diff --git a/app/assets/stylesheets/layout/admin_header.scss b/app/assets/stylesheets/layout/admin_header.scss new file mode 100644 index 000000000..758f1782a --- /dev/null +++ b/app/assets/stylesheets/layout/admin_header.scss @@ -0,0 +1,89 @@ +.admin { + > header { + border-bottom: 1px solid #eee; + + > * { + @include full-width-background($adjust-margin: false, $adjust-padding: true); + } + } + + .top-links { + + a { + line-height: rem-calc($line-height * 1.5); + } + } + + .top-bar { + height: auto; + padding-top: $line-height / 2; + + @include breakpoint(small only) { + + .top-bar-left ul { + display: inline-block; + } + + .top-bar-right { + + > ul { + border-bottom: 0; + padding-bottom: 0; + margin-bottom: 0; + } + + .submenu { + position: initial; + } + + a { + font-weight: normal; + } + } + + [class^="icon-"] { + display: none; + } + } + + [class^="icon-"] { + font-size: $base-font-size; + } + + h1 { + margin-top: $line-height / 2; + margin-bottom: 0; + + @include breakpoint(medium) { + margin-top: 0; + } + + small { + color: inherit; + text-transform: uppercase; + } + + a { + color: inherit; + display: inline-block; + font-family: "Lato" !important; + font-size: rem-calc(24); + font-weight: lighter; + line-height: 1; + } + } + } + + .top-bar .menu > li { + + @include breakpoint(medium) { + height: auto !important; + } + } + + .title-bar { + color: inherit; + position: absolute; + right: 12px; + } +} diff --git a/app/components/layout/account_menu_component.html.erb b/app/components/layout/account_menu_component.html.erb new file mode 100644 index 000000000..7c6875c84 --- /dev/null +++ b/app/components/layout/account_menu_component.html.erb @@ -0,0 +1,5 @@ + diff --git a/app/components/layout/account_menu_component.rb b/app/components/layout/account_menu_component.rb new file mode 100644 index 000000000..b8918c695 --- /dev/null +++ b/app/components/layout/account_menu_component.rb @@ -0,0 +1,7 @@ +class Layout::AccountMenuComponent < ApplicationComponent + attr_reader :user + + def initialize(user) + @user = user + end +end diff --git a/app/components/layout/admin_header_component.html.erb b/app/components/layout/admin_header_component.html.erb new file mode 100644 index 000000000..f1be1d2f6 --- /dev/null +++ b/app/components/layout/admin_header_component.html.erb @@ -0,0 +1,39 @@ +
+ + +
+ <% if show_account_menu? %> +
+ +
+ <% end %> + +
+
+

+ <%= link_to namespace_path do %> + <%= setting["org_name"] %> +
<%= namespaced_header_title %> + <% end %> +

+
+ + <% if show_account_menu? %> +
+
+ <%= render Layout::AccountMenuComponent.new(user) %> +
+
+ <% end %> +
+
+
diff --git a/app/components/layout/admin_header_component.rb b/app/components/layout/admin_header_component.rb new file mode 100644 index 000000000..3f97f9656 --- /dev/null +++ b/app/components/layout/admin_header_component.rb @@ -0,0 +1,32 @@ +class Layout::AdminHeaderComponent < ApplicationComponent + attr_reader :user + delegate :namespace, :namespaced_root_path, :show_admin_menu?, to: :helpers + + def initialize(user) + @user = user + end + + private + + def namespaced_header_title + if namespace == "moderation/budgets" + t("moderation.header.title") + elsif namespace == "management" + t("management.dashboard.index.title") + else + t("#{namespace}.header.title") + end + end + + def namespace_path + if namespace == "officing" + "#" + else + namespaced_root_path + end + end + + def show_account_menu? + show_admin_menu?(user) || namespace != "management" + end +end diff --git a/app/components/layout/admin_login_items_component.html.erb b/app/components/layout/admin_login_items_component.html.erb new file mode 100644 index 000000000..a5200f80d --- /dev/null +++ b/app/components/layout/admin_login_items_component.html.erb @@ -0,0 +1,4 @@ +
  • + <%= link_to t("layouts.header.administration_menu"), "#", rel: "nofollow", class: "hide-for-small-only" %> + <%= link_list(*admin_links, class: "submenu menu", data: { submenu: true }) %> +
  • diff --git a/app/components/layout/admin_login_items_component.rb b/app/components/layout/admin_login_items_component.rb new file mode 100644 index 000000000..44dd2d429 --- /dev/null +++ b/app/components/layout/admin_login_items_component.rb @@ -0,0 +1,49 @@ +class Layout::AdminLoginItemsComponent < ApplicationComponent + attr_reader :user + delegate :link_list, :show_admin_menu?, to: :helpers + + def initialize(user) + @user = user + end + + def render? + show_admin_menu?(user) + end + + private + + def admin_links + [ + (admin_link if user.administrator?), + (moderation_link if user.administrator? || user.moderator?), + (valuation_link if feature?(:budgets) && (user.administrator? || user.valuator?)), + (management_link if user.administrator? || user.manager?), + (officing_link if user.poll_officer? && Poll.current.any?), + (sdg_management_link if feature?(:sdg) && (user.administrator? || user.sdg_manager?)) + ] + end + + def admin_link + [t("layouts.header.administration"), admin_root_path] + end + + def moderation_link + [t("layouts.header.moderation"), moderation_root_path] + end + + def valuation_link + [t("layouts.header.valuation"), valuation_root_path] + end + + def management_link + [t("layouts.header.management"), management_sign_in_path] + end + + def officing_link + [t("layouts.header.officing"), officing_root_path] + end + + def sdg_management_link + [t("sdg_management.header.title"), sdg_management_root_path] + end +end diff --git a/app/views/layouts/_footer.html.erb b/app/components/layout/footer_component.html.erb similarity index 100% rename from app/views/layouts/_footer.html.erb rename to app/components/layout/footer_component.html.erb diff --git a/app/components/layout/footer_component.rb b/app/components/layout/footer_component.rb new file mode 100644 index 000000000..b9e0fe5de --- /dev/null +++ b/app/components/layout/footer_component.rb @@ -0,0 +1,2 @@ +class Layout::FooterComponent < ApplicationComponent +end diff --git a/app/views/devise/menu/_login_items.html.erb b/app/components/layout/login_items_component.html.erb similarity index 93% rename from app/views/devise/menu/_login_items.html.erb rename to app/components/layout/login_items_component.html.erb index 0aed7e3a4..b2acca46e 100644 --- a/app/views/devise/menu/_login_items.html.erb +++ b/app/components/layout/login_items_component.html.erb @@ -1,7 +1,7 @@ -<% if user_signed_in? %> +<% if user %>
  • <%= layout_menu_link_to t("layouts.header.my_activity_link"), - user_path(current_user), + user_path(user), controller_name == "users", rel: "nofollow", title: t("shared.go_to_page") + diff --git a/app/components/layout/login_items_component.rb b/app/components/layout/login_items_component.rb new file mode 100644 index 000000000..3ee4e6ee0 --- /dev/null +++ b/app/components/layout/login_items_component.rb @@ -0,0 +1,8 @@ +class Layout::LoginItemsComponent < ApplicationComponent + attr_reader :user + delegate :layout_menu_link_to, to: :helpers + + def initialize(user) + @user = user + end +end diff --git a/app/views/shared/_subnavigation.html.erb b/app/components/layout/subnavigation_component.html.erb similarity index 100% rename from app/views/shared/_subnavigation.html.erb rename to app/components/layout/subnavigation_component.html.erb diff --git a/app/components/layout/subnavigation_component.rb b/app/components/layout/subnavigation_component.rb new file mode 100644 index 000000000..57f12a683 --- /dev/null +++ b/app/components/layout/subnavigation_component.rb @@ -0,0 +1,3 @@ +class Layout::SubnavigationComponent < ApplicationComponent + delegate :content_block, :layout_menu_link_to, to: :helpers +end diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb index 1ec467ead..b89b4533a 100644 --- a/app/helpers/admin_helper.rb +++ b/app/helpers/admin_helper.rb @@ -3,16 +3,6 @@ module AdminHelper "/#{namespace}" end - def namespaced_header_title - if namespace == "moderation/budgets" - t("moderation.header.title") - elsif namespace == "management" - t("management.dashboard.index.title") - else - t("#{namespace}.header.title") - end - end - def official_level_options options = [["", 0]] (1..5).each do |i| diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 90bcc7f30..49db9cb5c 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -35,34 +35,10 @@ module UsersHelper end end - def current_administrator? - current_user&.administrator? - end - - def current_moderator? - current_user&.moderator? - end - - def current_valuator? - current_user&.valuator? - end - - def current_manager? - current_user&.manager? - end - - def current_sdg_manager? - current_user&.sdg_manager? - end - - def current_poll_officer? - current_user&.poll_officer? - end - - def show_admin_menu?(user = nil) + def show_admin_menu?(user) unless namespace == "officing" - current_administrator? || current_moderator? || current_valuator? || current_manager? || - user&.administrator? || current_poll_officer? || current_sdg_manager? + user&.administrator? || user&.moderator? || user&.valuator? || + (user&.manager? && namespace != "management") || user&.poll_officer? || user&.sdg_manager? end end diff --git a/app/views/layouts/_admin_header.html.erb b/app/views/layouts/_admin_header.html.erb deleted file mode 100644 index f8cd11365..000000000 --- a/app/views/layouts/_admin_header.html.erb +++ /dev/null @@ -1,50 +0,0 @@ -
    - - -
    -
    - -
    - -
    -
    - <% if namespace == "officing" %> -

    - <%= link_to "#" do %> - <%= setting["org_name"] %> -
    <%= namespaced_header_title %> - <% end %> -

    - <% else %> -

    - <%= link_to namespaced_root_path do %> - <%= setting["org_name"] %> -
    <%= namespaced_header_title %> - <% end %> -

    - <% end %> -
    - - <% if show_admin_menu?(current_user) || namespace != "management" %> -
    -
    - -
    -
    - <% end %> -
    -
    -
    diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 1e101fa36..bf1884f93 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -26,15 +26,11 @@
    - + <%= render Layout::AccountMenuComponent.new(current_user) %>
    @@ -44,7 +40,7 @@ diff --git a/app/views/layouts/management.html.erb b/app/views/layouts/management.html.erb index 5bb9f525a..921d91b2b 100644 --- a/app/views/layouts/management.html.erb +++ b/app/views/layouts/management.html.erb @@ -7,7 +7,7 @@ - <%= render "layouts/admin_header", current_user: manager_logged_in %> + <%= render Layout::AdminHeaderComponent.new(manager_logged_in) %>
  • - <%= link_to t("layouts.header.administration_menu"), "#", rel: "nofollow", class: "hide-for-small-only" %> - -
  • -<% end %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 708b63275..d10b71bbc 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -17,7 +17,7 @@

    <%= avatar_image(@user, seed: @user.id, size: 60) %> <%= @user.name %> - <% if current_administrator? %> + <% if current_user&.administrator? %> <%= @user.email %> <% end %>

    diff --git a/spec/components/layout/admin_header_component_spec.rb b/spec/components/layout/admin_header_component_spec.rb new file mode 100644 index 000000000..62a938369 --- /dev/null +++ b/spec/components/layout/admin_header_component_spec.rb @@ -0,0 +1,38 @@ +require "rails_helper" + +describe Layout::AdminHeaderComponent do + let(:user) { create(:user) } + before { Setting["org_name"] = "CONSUL" } + + around do |example| + with_request_url("/") { example.run } + end + + context "management section", controller: Management::BaseController do + it "shows the menu button and menu for administrators" do + create(:administrator, user: user) + + render_inline Layout::AdminHeaderComponent.new(user) + + expect(page).to have_link "Go back to CONSUL" + expect(page).to have_link "You don't have new notifications" + expect(page).to have_link "My content" + expect(page).to have_link "My account" + expect(page).to have_link "Sign out" + expect(page).to have_css "[data-toggle]" + end + + it "does not show the menu button and menu for managers" do + create(:manager, user: user) + + render_inline Layout::AdminHeaderComponent.new(user) + + expect(page).to have_link "Go back to CONSUL" + expect(page).not_to have_content "You don't have new notifications" + expect(page).not_to have_content "My content" + expect(page).not_to have_content "My account" + expect(page).not_to have_content "Sign out" + expect(page).not_to have_css "[data-toggle]" + end + end +end diff --git a/spec/system/management_spec.rb b/spec/system/management_spec.rb index 78ddc11b6..a1ac75e5a 100644 --- a/spec/system/management_spec.rb +++ b/spec/system/management_spec.rb @@ -2,34 +2,18 @@ require "rails_helper" describe "Management" do let(:user) { create(:user) } - before { Setting["org_name"] = "CONSUL" } - scenario "Should show admin menu if logged user is admin" do - create(:administrator, user: user) - login_as(user) - - visit root_path - click_link "Menu" - click_link "Management" - - expect(page).to have_link "Go back to CONSUL" - - expect(page).to have_link "You don't have new notifications" - expect(page).to have_link "My content" - expect(page).to have_link "My account" - expect(page).to have_link "Sign out" - end - - scenario "Should not show admin menu if logged user is manager" do + scenario "Does not show the admin menu when managing users having the admin menu" do create(:manager, user: user) + create(:moderator, user: create(:user, :in_census, document_number: "12345678M")) + login_as(user) - visit root_path - - click_link "Menu" - click_link "Management" - - expect(page).to have_link "Go back to CONSUL" + visit management_sign_in_path + click_link "Select user" + fill_in "Document number", with: "12345678M" + click_button "Check document" + expect(page).to have_content "This user account is already verified" expect(page).not_to have_content "You don't have new notifications" expect(page).not_to have_content "My content" expect(page).not_to have_content "My account"