Skip to content

Commit c47c5fa

Browse files
committed
Fixed routes and split file blog.js
1 parent 3f1edd1 commit c47c5fa

File tree

10 files changed

+102
-102
lines changed

10 files changed

+102
-102
lines changed

app/assets/javascripts/blog.js

+2-66
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,3 @@
11
//*= require "bootstrap"
2-
3-
// Comments
4-
const comment_form = document.getElementById('comment_form');
5-
const comment_message = document.getElementById('comment_message');
6-
7-
function addComment() {
8-
fetch(comment_form.action, {
9-
method: 'POST',
10-
body: new FormData(comment_form),
11-
}).then(response => {
12-
response.text().then(text => {
13-
14-
switch (response.status) {
15-
case 201:
16-
createCommentCreated(text);
17-
break;
18-
19-
case 403, 422:
20-
createError(text);
21-
break;
22-
}
23-
});
24-
});
25-
}
26-
27-
28-
function createError(body) {
29-
comment_message.innerHTML = '';
30-
comment_message.classList.remove('alert-success');
31-
32-
comment_message.classList.add('alert-danger');
33-
comment_message.innerHTML = body;
34-
}
35-
36-
function createCommentCreated(body) {
37-
comment_message.innerHTML = '';
38-
comment_message.classList.remove('alert-danger');
39-
40-
comment_message.classList.add('alert-success');
41-
comment_message.innerHTML = body;
42-
43-
comment_form.reset();
44-
}
45-
46-
// likes
47-
const likes = document.getElementById('likes');
48-
49-
function updateLikes(href_patch) {
50-
console.log(href_patch);
51-
fetch(href_patch, {
52-
method: 'PATCH',
53-
}).then(response => {
54-
response.text().then(text => {
55-
switch (response.status) {
56-
case 200:
57-
console.log(text)
58-
likes.innerHTML = text;
59-
break;
60-
61-
case 403, 422:
62-
likes.innerHTML = 'Error';
63-
break;
64-
}
65-
});
66-
});
67-
}
2+
//*= require "blog/likes"
3+
//*= require "blog/comments"
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Comments
2+
const comment_form = document.getElementById('comment_form');
3+
const comment_message = document.getElementById('comment_message');
4+
5+
function addComment() {
6+
fetch(comment_form.action, {
7+
method: 'POST',
8+
body: new FormData(comment_form),
9+
}).then(response => {
10+
response.text().then(text => {
11+
switch (response.status) {
12+
case 201:
13+
createComment(text);
14+
break;
15+
16+
case 403, 422:
17+
createError(text);
18+
break;
19+
}
20+
});
21+
});
22+
}
23+
24+
25+
function createError(body) {
26+
comment_message.innerHTML = '';
27+
comment_message.classList.remove('alert-success');
28+
29+
comment_message.classList.add('alert-danger');
30+
comment_message.innerHTML = body;
31+
}
32+
33+
function createComment(body) {
34+
comment_message.innerHTML = '';
35+
comment_message.classList.remove('alert-danger');
36+
37+
comment_message.classList.add('alert-success');
38+
comment_message.innerHTML = body;
39+
40+
comment_form.reset();
41+
}

app/assets/javascripts/blog/likes.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// likes
2+
const likes = document.getElementById('likes');
3+
4+
function updateLikes(link) {
5+
fetch(link, {
6+
method: 'PATCH',
7+
}).then(response => {
8+
response.text().then(text => {
9+
if (response.ok) {
10+
likes.innerHTML = text;
11+
}else {
12+
console.log("Likes not updated");
13+
}
14+
});
15+
});
16+
}
+1-25
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
11
module Blog
22
class SitemapController < BaseController
3-
def index
4-
@url_and_date = [hash_home].concat(hash_pages, hash_categories, hash_posts)
5-
end
6-
7-
private
8-
9-
def hash_url_and_updated(url, date_updated)
10-
{ url:, date_updated: }
11-
end
12-
13-
def hash_home
14-
hash_url_and_updated(home_url, DateTime.now.strftime('%d/%m/%Y'))
15-
end
16-
17-
def hash_pages
18-
Page.all.map { |page| hash_url_and_updated page_url(page), page.date_updated }
19-
end
20-
21-
def hash_categories
22-
Category.all.map { |category| hash_url_and_updated category_url(category), category.date_updated }
23-
end
24-
25-
def hash_posts
26-
Post.all.map { |post| hash_url_and_updated post_url(post), post.date_updated }
27-
end
3+
def index; end
284
end
295
end

app/helpers/application_helper.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
module ApplicationHelper
2+
def pages_navbar
3+
Page.where(published: true)
4+
end
25
end

app/helpers/blog/sitemap_helper.rb

+25
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
11
module Blog::SitemapHelper
2+
def urls_with_dates
3+
[hash_home].concat(hash_pages, hash_categories, hash_posts)
4+
end
5+
6+
private
7+
8+
def hash_url_and_updated(url, date_updated)
9+
{ url:, date_updated: }
10+
end
11+
12+
def hash_home
13+
hash_url_and_updated(home_url, DateTime.now.strftime('%d/%m/%Y'))
14+
end
15+
16+
def hash_pages
17+
Page.all.map { |page| hash_url_and_updated page_url(page), page.date_updated }
18+
end
19+
20+
def hash_categories
21+
Category.all.map { |category| hash_url_and_updated category_url(category), category.date_updated }
22+
end
23+
24+
def hash_posts
25+
Post.all.map { |post| hash_url_and_updated post_url(post), post.date_updated }
26+
end
227
end

app/views/blog/sitemap/index.xml.builder

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
xml.instruct!
22
xml.urlset xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9' do
3-
@url_and_date.each do |page|
3+
urls_with_dates.each do |page|
44
xml.url do
55
xml.loc page[:url]
66
xml.lastmod page[:date_updated]

app/views/layouts/blog/_header.html.erb

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<header class="container text-center">
22
<nav class="navbar navbar-expand-lg bg-body-tertiary">
33
<div class="container-fluid">
4-
<%= link_to t('site.name'), home_path, class: "navbar-brand", style: 'font-family: var(--bs-body-font-family); font-weight: bold;' %>
4+
<%= content_tag :div, t('site.name'), class: "navbar-brand", style: 'font-family: var(--bs-body-font-family); font-weight: bold;' %>
55
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
66
data-bs-target="#navbarSupportedContent"
77
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
@@ -10,17 +10,18 @@
1010
<div class="collapse navbar-collapse" id="navbarSupportedContent">
1111
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
1212
<li class="nav-item">
13-
<%= link_to t('navbar.home'), home_path, class: "nav-link" %>
13+
<%= link_to t('site.navbar.home'), home_path, class: "nav-link" %>
1414
</li>
15-
<li class="nav-item">
16-
<%= link_to t('navbar.search'), search_path, class: "nav-link" %>
17-
</li>
18-
<% Page.where(published: true).each do |page| %>
15+
<% pages_navbar.each do |page| %>
1916
<li class="nav-item">
2017
<%= link_to page.title, page_path(page), class: "nav-link" %>
2118
</li>
2219
<% end %>
2320
</ul>
21+
<%= form_with url:search_path, method: 'GET', role: 'search', class: 'd-flex' do |f| %>
22+
<%= f.search_field 'query[title_cont]', class: "form-control me-2" %>
23+
<%= f.submit t('site.navbar.search'), class: "btn btn-outline-success" %>
24+
<% end %>
2425
</div>
2526
</div>
2627
</nav>

config/routes.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
resources :authors, only: %i[show]
1818
get 'sitemap', to: 'sitemap#index', as: 'sitemap', format: :xml
1919
get 'search', to: 'search#index', as: 'search'
20-
get 'categories/:id', to: 'categories#show', as: 'category'
21-
get 'categories/:id/page/:page', to: 'categories#show', as: 'categories_page'
22-
20+
resources :categories, only: %i[show] do
21+
member do
22+
get '/page/:page', to: 'categories#show', as: 'page'
23+
end
24+
end
2325
resources :pages, only: %i[show]
2426
resources :posts, only: %i[show index] do
2527
match :likes, to: 'likes#update', via: %i[patch put]

db/seeds.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def production
103103
published: true,
104104
comments_enabled: true,
105105
admin_user: AdminUser.all.sample)
106-
106+
107107
puts 'Create a page'
108108
title = 'Page'
109109
Page.create!(title:,

0 commit comments

Comments
 (0)