-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allowing admins to create "app announcements" which are then shown to…
… users until they are marked as viewed
- Loading branch information
Showing
15 changed files
with
288 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class AppAnnouncementsController < Admin::AdminController | ||
def index | ||
@app_announcements = AppAnnouncement.all | ||
end | ||
|
||
def show | ||
@app_announcement = AppAnnouncement.find(params[:id]) | ||
end | ||
|
||
def new | ||
@app_announcement = AppAnnouncement.new | ||
end | ||
|
||
def edit | ||
@app_announcement = AppAnnouncement.find(params[:id]) | ||
end | ||
|
||
def create | ||
@app_announcement = AppAnnouncement.new(app_announcement_params) | ||
@app_announcement.author = current_user | ||
@save_result = @app_announcement.save | ||
|
||
if @save_result | ||
redirect_to admin_app_announcements_path | ||
else | ||
render 'new' | ||
end | ||
end | ||
|
||
def update | ||
@app_announcement = AppAnnouncement.find(params[:id]) | ||
if @app_announcement.update(app_announcement_params) | ||
redirect_to admin_app_announcements_path | ||
else | ||
render 'edit' | ||
end | ||
end | ||
|
||
def destroy | ||
@app_announcement = AppAnnouncement.find(params[:id]) | ||
@app_announcement.destroy | ||
redirect_to admin_app_announcements_path | ||
end | ||
|
||
private | ||
|
||
def app_announcement_params | ||
params.require(:app_announcement).permit(:text, :author_id) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
class AppAnnouncementsViewedController < ApplicationController | ||
def create | ||
@app_announcement = AppAnnouncement.find(params[:app_announcement_id]) | ||
@app_announcement_viewed = AppAnnouncementViewed.create(user: current_user, app_announcement: @app_announcement) | ||
|
||
redirect_to root_path | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: app_announcements | ||
# | ||
# id :bigint not null, primary key | ||
# text :string(255) | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# author_id :integer | ||
# | ||
class AppAnnouncement < ApplicationRecord | ||
belongs_to :author, class_name: 'User' | ||
has_many :app_announcement_viewed, dependent: :destroy | ||
has_many :viewers, through: :app_announcement_viewed, source: :user | ||
|
||
scope :latest_unseen_for_user, ->(user) { | ||
join_condition = " | ||
LEFT OUTER JOIN `app_announcement_vieweds` | ||
ON `app_announcements`.`id` = `app_announcement_vieweds`.`app_announcement_id` | ||
AND `app_announcement_vieweds`.`user_id` = ? | ||
" | ||
joins(sanitize_sql_array([ join_condition, user.id ])) | ||
.where('`app_announcement_vieweds`.`user_id` IS NULL') | ||
.order(created_at: :desc) | ||
} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: app_announcement_vieweds | ||
# | ||
# id :bigint not null, primary key | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# app_announcement_id :integer | ||
# user_id :integer | ||
# | ||
class AppAnnouncementViewed < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :app_announcement | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom"> | ||
<h1 class="h2">Edit Quepid Announcement</h1> | ||
</div> | ||
|
||
|
||
<%= form_for [:admin, @app_announcement] do |form| %> | ||
|
||
<div class="mb-3"> | ||
<%= form.label :text, class: 'form-label' %> | ||
<%= form.text_field :text, required: true, class: 'form-control' %> | ||
<div class="form-text">The announcement.</div> | ||
</div> | ||
|
||
<br> | ||
|
||
<div class="actions"> | ||
<%= form.submit %> | ||
</div> | ||
|
||
<% end %> | ||
|
||
|
||
<br> | ||
<%= button_to 'Back to Quepid Announcements', admin_app_announcements_path, method: :get %> | ||
<br> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom"> | ||
<h1 class="h2">Quepid Announcements</h1> | ||
<div class="btn-toolbar mb-2 mb-md-0"> | ||
<div class="btn-group me-2"> | ||
<%= link_to 'New Quepid Announcement', new_admin_app_announcement_path, class: "btn btn-sm btn-outline-secondary" %> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
|
||
<table class="table table-striped table-hover"> | ||
|
||
<thead> | ||
<tr> | ||
<th>Text</th> | ||
<th>Author</th> | ||
<th>Created</th> | ||
<th>Views</th> | ||
<th>Actions</th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% @app_announcements.each do |announcement| %> | ||
<tr> | ||
<td><%= announcement.text %></td> | ||
<td><%= announcement.author.name %> </td> | ||
<td><%= announcement.created_at.strftime("%B %d %Y, %l:%M%P") %> </td> | ||
<td><%= announcement.app_announcement_viewed.size %> </td> | ||
<td> | ||
<%= link_to 'Edit', edit_admin_app_announcement_path(announcement) %> | ||
<%= link_to 'Delete', admin_app_announcement_path(announcement), method: :delete, data: { confirm: 'Are you sure?' } %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom"> | ||
<h1 class="h2">New Quepid Announcement</h1> | ||
</div> | ||
|
||
|
||
<%= form_for [:admin, @app_announcement] do |form| %> | ||
|
||
<div class="mb-3"> | ||
<%= form.label :text, class: 'form-label' %> | ||
<%= form.text_field :text, required: true, class: 'form-control' %> | ||
<div class="form-text">The announcement.</div> | ||
</div> | ||
|
||
<br> | ||
|
||
<div class="actions"> | ||
<%= form.submit %> | ||
</div> | ||
|
||
<% end %> | ||
|
||
|
||
<br> | ||
<%= button_to 'Back to Quepid Announcements', admin_app_announcements_path, method: :get %> | ||
<br> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom"> | ||
<h1 class="h2">Quepid Announcements</h1> | ||
<div class="btn-toolbar mb-2 mb-md-0"> | ||
<div class="btn-group me-2"> | ||
<%= link_to 'New Quepid Announcement', new_admin_app_announcement_path, class: "btn btn-sm btn-outline-secondary" %> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<p> | ||
Quepid announcements allow administrators to broadcast important information to all Quepid users. | ||
</p> | ||
|
||
<p> | ||
<strong>Announcement:</strong> | ||
<%= @app_announcement.text %> | ||
</p> | ||
|
||
<p> | ||
<strong>Author:</strong> | ||
<%= @app_announcement.author.name %> | ||
</p> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateAppAnnouncements < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :app_announcements do |t| | ||
t.string :text | ||
t.integer :author_id | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
10 changes: 10 additions & 0 deletions
10
db/migrate/20231207110513_create_app_announcement_vieweds.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateAppAnnouncementVieweds < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :app_announcement_vieweds do |t| | ||
t.integer :app_announcement_id | ||
t.integer :user_id | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.