Skip to content

Commit a8b9cb6

Browse files
committed
Simplify the name of the object....
1 parent 1b6e7a6 commit a8b9cb6

22 files changed

+136
-166
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
module Admin
4+
class AnnouncementsController < Admin::AdminController
5+
skip_before_action :check_for_announcement
6+
def index
7+
@announcements = Announcement.all
8+
end
9+
10+
def new
11+
@announcement = Announcement.new
12+
@announcement.text = ''
13+
end
14+
15+
def edit
16+
@announcement = Announcement.find(params[:id])
17+
end
18+
19+
def create
20+
@announcement = Announcement.new(announcement_params)
21+
@announcement.author = current_user
22+
23+
if @announcement.save
24+
redirect_to admin_announcements_path
25+
else
26+
render 'new'
27+
end
28+
end
29+
30+
def update
31+
@announcement = Announcement.find(params[:id])
32+
if @announcement.update(announcement_params)
33+
redirect_to admin_announcements_path
34+
else
35+
render 'edit'
36+
end
37+
end
38+
39+
def destroy
40+
@announcement = Announcement.find(params[:id])
41+
@announcement.destroy
42+
redirect_to admin_announcements_path
43+
end
44+
45+
private
46+
47+
def announcement_params
48+
params.require(:announcement).permit(:text, :author_id)
49+
end
50+
end
51+
end

app/controllers/admin/app_announcements_controller.rb

-54
This file was deleted.

app/controllers/app_announcements_viewed_controller.rb

-10
This file was deleted.

app/controllers/application_controller.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def signup_enabled?
2828
end
2929

3030
def check_for_announcement
31-
@announcement = AppAnnouncement.latest_unseen_for_user(@current_user).first if @current_user
32-
AppAnnouncementViewed.create(user: @current_user, app_announcement: @announcement) if @announcement
31+
@announcement = Announcement.latest_unseen_for_user(@current_user).first if @current_user
32+
AnnouncementViewed.create(user: @current_user, announcement: @announcement) if @announcement
3333
end
3434
end

app/models/app_announcement.rb app/models/announcement.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22

33
# == Schema Information
44
#
5-
# Table name: app_announcements
5+
# Table name: announcements
66
#
77
# id :bigint not null, primary key
88
# text :string(255)
99
# created_at :datetime not null
1010
# updated_at :datetime not null
1111
# author_id :integer
1212
#
13-
class AppAnnouncement < ApplicationRecord
13+
class Announcement < ApplicationRecord
1414
belongs_to :author, class_name: 'User'
15-
has_many :app_announcement_viewed, dependent: :destroy
16-
has_many :viewers, through: :app_announcement_viewed, source: :user
15+
has_many :announcement_viewed, dependent: :destroy
16+
has_many :viewers, through: :announcement_viewed, source: :user
1717

1818
scope :latest_unseen_for_user, ->(user) {
1919
join_condition = "
20-
LEFT OUTER JOIN `app_announcement_vieweds`
21-
ON `app_announcements`.`id` = `app_announcement_vieweds`.`app_announcement_id`
22-
AND `app_announcement_vieweds`.`user_id` = ?
20+
LEFT OUTER JOIN `announcement_vieweds`
21+
ON `announcements`.`id` = `announcement_vieweds`.`announcement_id`
22+
AND `announcement_vieweds`.`user_id` = ?
2323
"
2424
joins(sanitize_sql_array([ join_condition, user.id ]))
25-
.where('`app_announcement_vieweds`.`user_id` IS NULL')
25+
.where('`announcement_vieweds`.`user_id` IS NULL')
2626
.order(created_at: :desc)
2727
}
2828
end

app/models/announcement_viewed.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
# == Schema Information
4+
#
5+
# Table name: announcement_vieweds
6+
#
7+
# id :bigint not null, primary key
8+
# created_at :datetime not null
9+
# updated_at :datetime not null
10+
# announcement_id :integer
11+
# user_id :integer
12+
#
13+
class AnnouncementViewed < ApplicationRecord
14+
belongs_to :user
15+
belongs_to :announcement
16+
end

app/models/app_announcement_viewed.rb

-16
This file was deleted.

app/models/user.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ class User < ApplicationRecord
114114
class_name: 'BookMetadatum',
115115
dependent: :destroy
116116

117-
has_many :app_announcements, foreign_key: 'author_id', dependent: :destroy, inverse_of: :author
118-
has_many :viewed_app_announcements, through: :app_announcements_viewed, source: :app_announcement
117+
has_many :announcements, foreign_key: 'author_id', dependent: :destroy, inverse_of: :author
118+
has_many :viewed_announcements, through: :announcements_viewed, source: :announcement
119119

120120
# Validations
121121

@@ -251,8 +251,8 @@ def pending_invite?
251251
end
252252

253253
def unseen_app_notifications
254-
AppAnnouncement
255-
.left_outer_joins(:app_announcement_viewed)
254+
Announcement
255+
.left_outer_joins(:announcement_viewed)
256256
.where('user_id != ? OR user_id IS NULL', id)
257257
.order(:created_at)
258258
end

app/views/admin/app_announcements/edit.html.erb app/views/admin/announcements/edit.html.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
</div>
44

55

6-
<%= form_for [:admin, @app_announcement] do |form| %>
6+
<%= form_for [:admin, @announcement] do |form| %>
77

88
<div class="mb-3">
99
<%= form.label :text, class: 'form-label' %>
@@ -21,5 +21,5 @@
2121

2222

2323
<br>
24-
<%= button_to 'Back to Quepid Announcements', admin_app_announcements_path, method: :get %>
24+
<%= button_to 'Back to Quepid Announcements', admin_announcements_path, method: :get %>
2525
<br>

app/views/admin/app_announcements/index.html.erb app/views/admin/announcements/index.html.erb

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
2-
<h1 class="h2">Quepid Announcements</h1>
2+
<h1 class="h2">Quepid Announcements to Users</h1>
33
<div class="btn-toolbar mb-2 mb-md-0">
44
<div class="btn-group me-2">
5-
<%= link_to 'New Quepid Announcement', new_admin_app_announcement_path, class: "btn btn-sm btn-outline-secondary" %>
5+
<%= link_to 'New Quepid Announcement', new_admin_announcement_path, class: "btn btn-sm btn-outline-secondary" %>
66
</div>
77
</div>
88
</div>
@@ -21,15 +21,15 @@
2121
</thead>
2222

2323
<tbody>
24-
<% @app_announcements.each do |announcement| %>
24+
<% @announcements.each do |announcement| %>
2525
<tr>
2626
<td><%= announcement.text.html_safe %></td>
2727
<td><%= announcement.author.name %> </td>
2828
<td><%= announcement.created_at.strftime("%B %d %Y, %l:%M%P") %> </td>
29-
<td><%= announcement.app_announcement_viewed.size %> </td>
29+
<td><%= announcement.announcement_viewed.size %> </td>
3030
<td>
31-
<%= link_to 'Edit', edit_admin_app_announcement_path(announcement) %>
32-
<%= link_to 'Delete', admin_app_announcement_path(announcement), method: :delete, data: { confirm: 'Are you sure?' } %>
31+
<%= link_to 'Edit', edit_admin_announcement_path(announcement) %>
32+
<%= link_to 'Delete', admin_announcement_path(announcement), method: :delete, data: { confirm: 'Are you sure?' } %>
3333
</td>
3434
</tr>
3535
<% end %>

app/views/admin/app_announcements/new.html.erb app/views/admin/announcements/new.html.erb

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
</div>
44

55

6-
<%= form_for [:admin, @app_announcement] do |form| %>
6+
<%= form_for [:admin, @announcement] do |form| %>
77

88
<div class="mb-3">
99
<%= form.label :text, class: 'form-label' %>
1010
<%= form.text_field :text, required: true, class: 'form-control' %>
11-
<div class="form-text">The announcement.</div>
11+
<div class="form-text">You can use HTML and emojis.</div>
1212
</div>
1313

1414
<br>
@@ -21,5 +21,5 @@
2121

2222

2323
<br>
24-
<%= button_to 'Back to Quepid Announcements', admin_app_announcements_path, method: :get %>
24+
<%= button_to 'Back to Quepid Announcements', admin_announcements_path, method: :get %>
2525
<br>

app/views/admin/app_announcements/show.html.erb

-23
This file was deleted.

app/views/admin/home/index.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<ul>
77
<li><%= link_to 'Users', admin_users_path %>
88
<li><%= link_to 'Quepid Communal Scorers', admin_communal_scorers_path %></li>
9-
<li><%= link_to 'Announcements', admin_app_announcements_path %>
9+
<li><%= link_to 'Announcements', admin_announcements_path %>
1010
<li><%= link_to 'Job Manager', sidekiq_web_path %></li>
1111
</ul>
1212
</div>

app/views/layouts/_header.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<li><%= link_to 'Admin Home', admin_path, class: 'dropdown-item', target: '_self' %></li>
8181
<li><%= link_to 'Users', admin_users_path, class: 'dropdown-item', target: '_self' %></li>
8282
<li><%= link_to 'Quepid Scorers', admin_communal_scorers_path, class: 'dropdown-item', target: '_self' %></li>
83-
<li><%= link_to 'Announcements', admin_app_announcements_path, class: 'dropdown-item', target: '_self' %></li>
83+
<li><%= link_to 'Announcements', admin_announcements_path, class: 'dropdown-item', target: '_self' %></li>
8484
<li><%= link_to 'Job Manager', sidekiq_web_path, class: 'dropdown-item', target: '_self' %></li>
8585
<% end %>
8686

app/views/layouts/_header_core_app.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<li><%= link_to 'Admin Home', admin_path, class: 'dropdown-link', target: '_self' %></li>
6666
<li><%= link_to 'Users', admin_users_path, class: 'dropdown-link', target: '_self' %></li>
6767
<li><%= link_to 'Quepid Scorers', admin_communal_scorers_path, class: 'dropdown-link', target: '_self' %></li>
68-
<li><%= link_to 'Announcements', admin_app_announcements_path, class: 'dropdown-item', target: '_self' %></li>
68+
<li><%= link_to 'Announcements', admin_announcements_path, class: 'dropdown-item', target: '_self' %></li>
6969
<li><%= link_to 'Job Manager', sidekiq_web_path, class: 'dropdown-link', target: '_self' %></li>
7070
<% end %>
7171
</ul>

config/routes.rb

+1-5
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@
4848
resources :ratings, only: [ :index ]
4949
end
5050

51-
resources :app_announcements, only: [] do
52-
resources :app_announcements_viewed, only: [ :create ]
53-
end
54-
5551
resources :books do
5652
resources :judgements
5753
resources :query_doc_pairs do
@@ -105,7 +101,7 @@
105101
resource :pulse, only: [ :show ], module: :users
106102
end
107103
resources :communal_scorers
108-
resources :app_announcements
104+
resources :announcements
109105
end
110106

111107
# preview routes for mailers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateAnnouncements < ActiveRecord::Migration[7.0]
2+
def change
3+
create_table :announcements do |t|
4+
t.string :text
5+
t.integer :author_id
6+
7+
t.timestamps
8+
end
9+
end
10+
end

db/migrate/20231207104058_create_app_announcements.rb

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateAnnouncementVieweds < ActiveRecord::Migration[7.0]
2+
def change
3+
create_table :announcement_vieweds do |t|
4+
t.integer :announcement_id
5+
t.integer :user_id
6+
7+
t.timestamps
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)