diff --git a/app/components/application/navbar_component.html.erb b/app/components/application/navbar_component.html.erb
index 3bf7b4b..39298e1 100644
--- a/app/components/application/navbar_component.html.erb
+++ b/app/components/application/navbar_component.html.erb
@@ -1,5 +1,5 @@
<%= render Structure::NavbarComponent.new do |nav| %>
- <% nav.item(href: root_path, title: t("application.navbar_component.home"), active: true) do |i| %>
+ <% nav.item(href: root_path, title: t("application.navbar_component.home"), active: activate_home_item?) do |i| %>
<% i.icon do %>
<% end %>
@@ -11,13 +11,13 @@
<% end %>
<% end %>
- <% nav.item(href: new_post_path, title: t("application.navbar_component.new")) do |i| %>
+ <% nav.item(href: new_post_path, title: t("application.navbar_component.new"), active: activate_new_post_item?) do |i| %>
<% i.icon do %>
<% end %>
<% end %>
- <% nav.item(href: user_path(username: current_user.profile.username), title: t("application.navbar_component.profile")) do |i| %>
+ <% nav.item(href: user_path(username: current_user.profile.username), title: t("application.navbar_component.profile"), active: activate_profile_item?) do |i| %>
<% i.icon do %>
<% end %>
diff --git a/app/components/application/navbar_component.rb b/app/components/application/navbar_component.rb
index 078e5ba..46e3cbd 100644
--- a/app/components/application/navbar_component.rb
+++ b/app/components/application/navbar_component.rb
@@ -2,12 +2,29 @@
module Application
class NavbarComponent < ViewComponent::Base
- def initialize(current_user:)
+ def initialize(current_user:, current_controller:)
@current_user = current_user
+ @current_controller = current_controller
end
private
- attr_reader :current_user
+ attr_reader :current_user, :current_controller
+
+ def activate_home_item?
+ current_controller.is_a?(PostsController) && current_action_is_a?(:index)
+ end
+
+ def activate_new_post_item?
+ current_controller.is_a?(PostsController) && current_action_is_a?(:new)
+ end
+
+ def activate_profile_item?
+ current_controller.is_a?(UsersController) || current_controller.is_a?(ProfilesController)
+ end
+
+ def current_action_is_a?(action)
+ current_controller.action_name == action.to_s
+ end
end
end
diff --git a/app/pages/posts/index_page.html.erb b/app/pages/posts/index_page.html.erb
index 0d3ded4..44720b8 100644
--- a/app/pages/posts/index_page.html.erb
+++ b/app/pages/posts/index_page.html.erb
@@ -2,4 +2,4 @@
<%= render Structure::CardComponent.with_collection(posts, user: user) %>
-<%= render Application::NavbarComponent.new(current_user: user) %>
+<%= render Application::NavbarComponent.new(current_user: user, current_controller: controller) %>
diff --git a/app/pages/posts/new_page.html.erb b/app/pages/posts/new_page.html.erb
index ef0cff9..593ed2a 100644
--- a/app/pages/posts/new_page.html.erb
+++ b/app/pages/posts/new_page.html.erb
@@ -18,5 +18,5 @@
<% end %>
<% end %>
- <%= render Application::NavbarComponent.new(current_user: current_user) %>
+ <%= render Application::NavbarComponent.new(current_user: current_user, current_controller: controller) %>
diff --git a/app/pages/profiles/edit_page.html.erb b/app/pages/profiles/edit_page.html.erb
index cb7be9a..d2de280 100644
--- a/app/pages/profiles/edit_page.html.erb
+++ b/app/pages/profiles/edit_page.html.erb
@@ -24,5 +24,5 @@
<% end %>
<% end %>
- <%= render Application::NavbarComponent.new(current_user: user) %>
+ <%= render Application::NavbarComponent.new(current_user: user, current_controller: controller) %>
diff --git a/app/pages/users/show_page.html.erb b/app/pages/users/show_page.html.erb
index 85fc9a6..ec1450b 100644
--- a/app/pages/users/show_page.html.erb
+++ b/app/pages/users/show_page.html.erb
@@ -24,5 +24,5 @@
<% if current_user.present? %>
- <%= render Application::NavbarComponent.new(current_user: current_user) %>
+ <%= render Application::NavbarComponent.new(current_user: current_user, current_controller: controller) %>
<% end %>
diff --git a/spec/components/application/navbar_component_spec.rb b/spec/components/application/navbar_component_spec.rb
index ba75b61..867c163 100644
--- a/spec/components/application/navbar_component_spec.rb
+++ b/spec/components/application/navbar_component_spec.rb
@@ -6,7 +6,7 @@
it "renders the Home item" do
current_user = create(:user)
- rendered = render_inline(described_class.new(current_user: current_user))
+ rendered = render_inline(described_class.new(current_user: current_user, current_controller: "posts"))
item_text = I18n.t("application.navbar_component.home")
expect(rendered.to_html).to have_link(item_text, href: root_path)
@@ -15,7 +15,7 @@
it "renders the Search item" do
current_user = create(:user)
- rendered = render_inline(described_class.new(current_user: current_user))
+ rendered = render_inline(described_class.new(current_user: current_user, current_controller: "posts"))
item_text = I18n.t("application.navbar_component.search")
expect(rendered.to_html).to have_link(item_text, href: "#")
@@ -24,7 +24,7 @@
it "renders the New item" do
current_user = create(:user)
- rendered = render_inline(described_class.new(current_user: current_user))
+ rendered = render_inline(described_class.new(current_user: current_user, current_controller: "posts"))
item_text = I18n.t("application.navbar_component.new")
expect(rendered.to_html).to have_link(item_text, href: new_post_path)
@@ -33,7 +33,7 @@
it "renders the Profile item" do
current_user = create(:user)
- rendered = render_inline(described_class.new(current_user: current_user))
+ rendered = render_inline(described_class.new(current_user: current_user, current_controller: "posts"))
item_text = I18n.t("application.navbar_component.profile")
expect(rendered.to_html).to have_link(item_text, href: user_path(username: current_user.profile.username))
@@ -42,7 +42,7 @@
it "renders the Sign out item" do
current_user = create(:user)
- rendered = render_inline(described_class.new(current_user: current_user))
+ rendered = render_inline(described_class.new(current_user: current_user, current_controller: "posts"))
item_text = I18n.t("application.navbar_component.sign_out")
expect(rendered.to_html).to have_link(item_text, href: destroy_user_session_path)