Add diary entry subscribe/unsubscribe GET pages

This commit is contained in:
Anton Khorev 2024-02-17 05:43:13 +03:00
parent d22e851557
commit 3a873b1668
8 changed files with 110 additions and 30 deletions

View file

@ -157,21 +157,25 @@ class DiaryEntriesController < ApplicationController
end end
def subscribe def subscribe
diary_entry = DiaryEntry.find(params[:id]) @diary_entry = DiaryEntry.find(params[:id])
diary_entry.subscriptions.create(:user => current_user) unless diary_entry.subscribers.exists?(current_user.id) if request.post?
@diary_entry.subscriptions.create(:user => current_user) unless @diary_entry.subscribers.exists?(current_user.id)
redirect_to diary_entry_path(diary_entry.user, diary_entry) redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
end
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
render :action => "no_such_entry", :status => :not_found render :action => "no_such_entry", :status => :not_found
end end
def unsubscribe def unsubscribe
diary_entry = DiaryEntry.find(params[:id]) @diary_entry = DiaryEntry.find(params[:id])
diary_entry.subscriptions.where(:user => current_user).delete_all if diary_entry.subscribers.exists?(current_user.id) if request.post?
@diary_entry.subscriptions.where(:user => current_user).delete_all if @diary_entry.subscribers.exists?(current_user.id)
redirect_to diary_entry_path(diary_entry.user, diary_entry) redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
end
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
render :action => "no_such_entry", :status => :not_found render :action => "no_such_entry", :status => :not_found
end end

View file

@ -1,26 +1,5 @@
<article class='diary_post border-top border-grey py-3<%= " text-muted px-3 bg-danger bg-opacity-10" unless diary_entry.visible %> user_<%= diary_entry.user.id %>'> <article class='diary_post border-top border-grey py-3<%= " text-muted px-3 bg-danger bg-opacity-10" unless diary_entry.visible %> user_<%= diary_entry.user.id %>'>
<div class='mb-3'> <%= render :partial => "diary_entry_heading", :object => diary_entry, :as => "diary_entry" %>
<% if @user %>
<h2><%= link_to diary_entry.title, diary_entry_path(diary_entry.user, diary_entry) %></h2>
<% else %>
<div class="row">
<div class="col-auto">
<%= user_thumbnail diary_entry.user %>
</div>
<div class="col">
<h2><%= link_to diary_entry.title, diary_entry_path(diary_entry.user, diary_entry) %></h2>
</div>
</div>
<% end %>
<small class='text-muted'>
<%= t(".posted_by_html", :link_user => (link_to diary_entry.user.display_name, user_path(diary_entry.user)), :created => l(diary_entry.created_at, :format => :blog), :language_link => (link_to diary_entry.language.name, :controller => "diary_entries", :action => "index", :display_name => nil, :language => diary_entry.language_code)) %>
<% if (l(diary_entry.updated_at, :format => :blog) != l(diary_entry.created_at, :format => :blog)) %>
<%= t(".updated_at_html", :updated => l(diary_entry.updated_at, :format => :blog)) %>
<% end %>
</small>
</div>
<div class="richtext text-break" xml:lang="<%= diary_entry.language_code %>" lang="<%= diary_entry.language_code %>"> <div class="richtext text-break" xml:lang="<%= diary_entry.language_code %>" lang="<%= diary_entry.language_code %>">
<%= diary_entry.body.to_html %> <%= diary_entry.body.to_html %>

View file

@ -0,0 +1,21 @@
<div class='mb-3'>
<% if @user %>
<h2><%= link_to diary_entry.title, diary_entry_path(diary_entry.user, diary_entry) %></h2>
<% else %>
<div class="row">
<div class="col-auto">
<%= user_thumbnail diary_entry.user %>
</div>
<div class="col">
<h2><%= link_to diary_entry.title, diary_entry_path(diary_entry.user, diary_entry) %></h2>
</div>
</div>
<% end %>
<small class='text-muted'>
<%= t("diary_entries.diary_entry.posted_by_html", :link_user => (link_to diary_entry.user.display_name, user_path(diary_entry.user)), :created => l(diary_entry.created_at, :format => :blog), :language_link => (link_to diary_entry.language.name, :controller => "diary_entries", :action => "index", :display_name => nil, :language => diary_entry.language_code)) %>
<% if (l(diary_entry.updated_at, :format => :blog) != l(diary_entry.created_at, :format => :blog)) %>
<%= t("diary_entries.diary_entry.updated_at_html", :updated => l(diary_entry.updated_at, :format => :blog)) %>
<% end %>
</small>
</div>

View file

@ -0,0 +1,12 @@
<% content_for :heading do %>
<h1><%= t ".heading", :user => @diary_entry.user.display_name %></h1>
<% end %>
<%= render :partial => "diary_entry_heading", :object => @diary_entry, :as => "diary_entry" %>
<%= bootstrap_form_tag do |f| %>
<% if params[:referer] -%>
<%= f.hidden_field :referer, :value => params[:referer] %>
<% end -%>
<%= f.primary t(".button") %>
<% end %>

View file

@ -0,0 +1,12 @@
<% content_for :heading do %>
<h1><%= t ".heading", :user => @diary_entry.user.display_name %></h1>
<% end %>
<%= render :partial => "diary_entry_heading", :object => @diary_entry, :as => "diary_entry" %>
<%= bootstrap_form_tag do |f| %>
<% if params[:referer] -%>
<%= f.hidden_field :referer, :value => params[:referer] %>
<% end -%>
<%= f.primary t(".button") %>
<% end %>

View file

@ -576,6 +576,12 @@ en:
comment: Comment comment: Comment
newer_comments: "Newer Comments" newer_comments: "Newer Comments"
older_comments: "Older Comments" older_comments: "Older Comments"
subscribe:
heading: Subscribe to the following diary entry discussion?
button: Subscribe to discussion
unsubscribe:
heading: Unsubscribe from the following diary entry discussion?
button: Unsubscribe from discussion
doorkeeper: doorkeeper:
errors: errors:
messages: messages:

View file

@ -244,8 +244,8 @@ OpenStreetMap::Application.routes.draw do
post "/user/:display_name/diary/:id/unhide" => "diary_entries#unhide", :id => /\d+/, :as => :unhide_diary_entry post "/user/:display_name/diary/:id/unhide" => "diary_entries#unhide", :id => /\d+/, :as => :unhide_diary_entry
post "/user/:display_name/diary/:id/hidecomment/:comment" => "diary_entries#hidecomment", :id => /\d+/, :comment => /\d+/, :as => :hide_diary_comment post "/user/:display_name/diary/:id/hidecomment/:comment" => "diary_entries#hidecomment", :id => /\d+/, :comment => /\d+/, :as => :hide_diary_comment
post "/user/:display_name/diary/:id/unhidecomment/:comment" => "diary_entries#unhidecomment", :id => /\d+/, :comment => /\d+/, :as => :unhide_diary_comment post "/user/:display_name/diary/:id/unhidecomment/:comment" => "diary_entries#unhidecomment", :id => /\d+/, :comment => /\d+/, :as => :unhide_diary_comment
post "/user/:display_name/diary/:id/subscribe" => "diary_entries#subscribe", :as => :diary_entry_subscribe, :id => /\d+/ match "/user/:display_name/diary/:id/subscribe" => "diary_entries#subscribe", :via => [:get, :post], :as => :diary_entry_subscribe, :id => /\d+/
post "/user/:display_name/diary/:id/unsubscribe" => "diary_entries#unsubscribe", :as => :diary_entry_unsubscribe, :id => /\d+/ match "/user/:display_name/diary/:id/unsubscribe" => "diary_entries#unsubscribe", :via => [:get, :post], :as => :diary_entry_unsubscribe, :id => /\d+/
# user pages # user pages
resources :users, :path => "user", :param => :display_name, :only => [:show, :destroy] resources :users, :path => "user", :param => :display_name, :only => [:show, :destroy]

View file

@ -98,10 +98,18 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest
{ :path => "/user/username/diary/1/unhidecomment/2", :method => :post }, { :path => "/user/username/diary/1/unhidecomment/2", :method => :post },
{ :controller => "diary_entries", :action => "unhidecomment", :display_name => "username", :id => "1", :comment => "2" } { :controller => "diary_entries", :action => "unhidecomment", :display_name => "username", :id => "1", :comment => "2" }
) )
assert_routing(
{ :path => "/user/username/diary/1/subscribe", :method => :get },
{ :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
)
assert_routing( assert_routing(
{ :path => "/user/username/diary/1/subscribe", :method => :post }, { :path => "/user/username/diary/1/subscribe", :method => :post },
{ :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" } { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" }
) )
assert_routing(
{ :path => "/user/username/diary/1/unsubscribe", :method => :get },
{ :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
)
assert_routing( assert_routing(
{ :path => "/user/username/diary/1/unsubscribe", :method => :post }, { :path => "/user/username/diary/1/unsubscribe", :method => :post },
{ :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" } { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" }
@ -916,6 +924,25 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest
assert_response :not_found assert_response :not_found
end end
def test_subscribe_page
user = create(:user)
other_user = create(:user)
diary_entry = create(:diary_entry, :user => user)
path = diary_entry_subscribe_path(:id => diary_entry, :display_name => user.display_name)
get path
assert_response :redirect
assert_redirected_to login_path(:referer => path)
session_for(other_user)
get path
assert_response :success
assert_dom ".content-body" do
assert_dom "a[href='#{diary_entry_path(:id => diary_entry, :display_name => user.display_name)}']", :text => diary_entry.title
assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
end
end
def test_subscribe_success def test_subscribe_success
user = create(:user) user = create(:user)
other_user = create(:user) other_user = create(:user)
@ -953,6 +980,25 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest
end end
end end
def test_unsubscribe_page
user = create(:user)
other_user = create(:user)
diary_entry = create(:diary_entry, :user => user)
path = diary_entry_unsubscribe_path(:id => diary_entry, :display_name => user.display_name)
get path
assert_response :redirect
assert_redirected_to login_path(:referer => path)
session_for(other_user)
get path
assert_response :success
assert_dom ".content-body" do
assert_dom "a[href='#{diary_entry_path(:id => diary_entry, :display_name => user.display_name)}']", :text => diary_entry.title
assert_dom "a[href='#{user_path(user)}']", :text => user.display_name
end
end
def test_unsubscribe_success def test_unsubscribe_success
user = create(:user) user = create(:user)
other_user = create(:user) other_user = create(:user)