Skip to content

Commit b58278c

Browse files
authored
add topics (#47)
1 parent c0d460a commit b58278c

File tree

21 files changed

+444
-11
lines changed

21 files changed

+444
-11
lines changed

app/controllers/application_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ class ApplicationController < ActionController::Base
22
include Authentication
33
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
44
allow_browser versions: :modern
5+
6+
def check_admin!
7+
redirect_to root_path unless Current.user.is_admin?
8+
end
59
end
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class TopicsController < ApplicationController
2+
before_action :set_topic, only: [ :show, :edit, :update, :destroy, :archive ]
3+
before_action :check_admin!, only: :destroy
4+
5+
def index
6+
@topics = scope.includes(:language, :provider)
7+
end
8+
9+
def new
10+
@topic = scope.new
11+
end
12+
13+
def create
14+
@topic = scope.new(topic_params)
15+
16+
if @topic.save
17+
redirect_to topics_path
18+
else
19+
render :new
20+
end
21+
end
22+
23+
def show
24+
end
25+
26+
def edit
27+
end
28+
29+
def update
30+
@topic.update(topic_params)
31+
redirect_to topics_path
32+
end
33+
34+
def destroy
35+
@topic.destroy
36+
redirect_to topics_path
37+
end
38+
39+
def archive
40+
@topic.archived!
41+
redirect_to topics_path
42+
end
43+
44+
private
45+
46+
def topic_params
47+
params.require(:topic).permit(:title, :description, :uid, :language_id, :provider_id)
48+
end
49+
50+
def set_topic
51+
@topic = Topic.find(params[:id])
52+
end
53+
54+
def scope
55+
@scope ||= if Current.user.is_admin?
56+
Topic.all
57+
else
58+
Topic.active
59+
end
60+
end
61+
end

app/models/topic.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Topic < ApplicationRecord
2+
belongs_to :language
3+
belongs_to :provider
4+
5+
validates :title, :language_id, :provider_id, presence: true
6+
7+
STATES = %i[active archived].freeze
8+
9+
enum :state, STATES.map.with_index.to_h
10+
11+
scope :active, -> { where(state: :active) }
12+
end

app/views/layouts/_sidebar.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@
5959
</li>
6060

6161
<li class="sidebar-item">
62-
<a href="ui-file-uploader.html" class="sidebar-link">
62+
<%= link_to topics_path, class: "sidebar-link" do %>
6363
<i class="bi bi-tags-fill"></i>
6464
<span>Topics</span>
65-
</a>
65+
<% end %>
6666
</li>
6767

6868
<li class="sidebar-item">

app/views/shared/_errors.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<% if resource.errors.any? %>
22
<div style="color: red">
3-
<h4><%= "#{pluralize(resource.errors.count, "error")} prohibited this #{resource.class_name} from being saved:" %>></h4>
3+
<h4><%= "#{pluralize(resource.errors.count, "error")} prohibited this #{resource.class.name} from being saved:" %>></h4>
44
<ul>
55
<% resource.errors.each do |error| %>
66
<li><%= error.full_message %></li>

app/views/topics/_form.html.erb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
<%= form_for topic do |f| %>
3+
<div class="form-body">
4+
<div class="row">
5+
<%= render "shared/errors", resource: topic %>
6+
<div class="col-12">
7+
<div class="form-group">
8+
<%= f.label :title %>
9+
<%= f.text_field :title, class: "form-control", placeholder: "Title" %>
10+
</div>
11+
<div class="form-group">
12+
<%= f.label :description %>
13+
<%= f.text_area :description, class: "form-control", placeholder: "Descripion" %>
14+
</div>
15+
<div class="form-group">
16+
<%= f.label :language %>
17+
<%= f.collection_select :language_id, Language.all, :id, :name, { prompt: "Select Language" }, class: "form-select" %>
18+
</div>
19+
<div class="form-group">
20+
<%= f.label :provider %>
21+
<%= f.collection_select :provider_id, Provider.all, :id, :name, { prompt: "Select Provider" }, class: "form-select" %>
22+
</div>
23+
<div class="form-group">
24+
<%= f.label :description %>
25+
<%= f.text_area :description, class: "form-control", placeholder: "Descripion" %>
26+
</div>
27+
<div class="col-12 d-flex justify-content-end">
28+
<%= f.submit "Save", class: "btn btn-primary me-1 mb-1" %>
29+
<%= link_to "Cancel", topics_path, class: "btn btn-light-secondary me-1 mb-1" %>
30+
</div>
31+
</div>
32+
</div>
33+
</div>
34+
<% end %>

app/views/topics/_topic.html.erb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<div id="<%= dom_id topic %>">
2+
<p>
3+
<strong>Title:</strong>
4+
<%= topic.title %>
5+
</p>
6+
7+
<p>
8+
<strong>Description:</strong>
9+
<%= topic.description %>
10+
</p>
11+
12+
<p>
13+
<strong>UID:</strong>
14+
<%= topic.uid %>
15+
</p>
16+
17+
<p>
18+
<strong>Language:</strong>
19+
<%= link_to topic.language.name, topic.language %>
20+
</p>
21+
22+
<p>
23+
<strong>Provider:</strong>
24+
<%= link_to topic.provider.name, topic.provider %>
25+
</p>
26+
</div>

app/views/topics/edit.html.erb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<% content_for :title, "Editing topic" %>
2+
3+
<section id="basic-vertical-layouts">
4+
<div class="row match-height">
5+
<div class="col-md-6 col-12">
6+
<div class="card">
7+
<div class="card-header">
8+
<h2 class="card-title">Edit topic</h2>
9+
</div>
10+
<div class="card-content">
11+
<div class="card-body">
12+
<%= render "form", topic: @topic %>
13+
</div>
14+
</div>
15+
</div>
16+
</div>
17+
</div>
18+
</section>
19+

app/views/topics/index.html.erb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<% content_for :title, "Topics" %>
2+
3+
<section class="section">
4+
<div class="row" id="table-striped">
5+
<div class="col-12 cold-md-12">
6+
<div class="card">
7+
<div class="card-header d-flex justify-content-between align-items-center">
8+
<h2 class="card-title">Topics</h2>
9+
<%= link_to new_topic_path, class: "btn btn-primary" do %>
10+
<i class="bi bi-plus"></i> Add New Topic
11+
<% end %>
12+
</div>
13+
<div class="card-content">
14+
<div class="card-body">
15+
<p class="card-text"> Some important information or instruction can be placed here.</p>
16+
<div class="table-responsive">
17+
<table class="table table-lg table-striped mb-0">
18+
<thead>
19+
<tr>
20+
<th>Title</th>
21+
<th>Description</th>
22+
<th>UID</th>
23+
<th>Language</th>
24+
<th>Provider</th>
25+
<th>State</th>
26+
<th class="text-end">Actions</th>
27+
</tr>
28+
</thead>
29+
<tbody>
30+
<% @topics.each do |topic| %>
31+
<tr>
32+
<td class="text-bold-500"><%= topic.title %></td>
33+
<td class="text-bold-500"><%= topic.description.truncate(25, omission: "...") %></td>
34+
<td class="text-bold-500"><%= topic.uid.truncate(10, omission: "...") %></td>
35+
<td class="text-bold-500"><%= topic.language.name %></td>
36+
<td class="text-bold-500"><%= topic.provider.name %></td>
37+
<td class="text-bold-500"><%= topic.state %></td>
38+
<td class="text-end">
39+
<%= link_to topic, class: "btn btn-primary btn-sm" do %>
40+
<i class="bi bi-search"></i> View
41+
<% end %>
42+
<%= link_to edit_topic_path(topic), class: "btn btn-secondary btn-sm" do %>
43+
<i class="bi bi-pencil"></i> Edit
44+
<% end %>
45+
<%= link_to archive_topic_path(topic), method: :put, data: { confirm: "Are you sure?" }, class: "btn btn-danger btn-sm" do %>
46+
<i class="bi bi-archive"></i> Archive
47+
<% end %>
48+
<% if Current.user.is_admin? %>
49+
<%= link_to topic, method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger btn-sm" do %>
50+
<i class="bi bi-trash"></i> Delete
51+
<% end %>
52+
<% end %>
53+
</td>
54+
</tr>
55+
<% end %>
56+
</tbody>
57+
</table>
58+
</div>
59+
</div>
60+
</div>
61+
</div>
62+
</div>
63+
</div>
64+
</section>

app/views/topics/new.html.erb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<% content_for :title, "New topic" %>
2+
3+
<section id="basic-vertical-layouts">
4+
<div class="row match-height">
5+
<div class="col-md-6 col-12">
6+
<div class="card">
7+
<div class="card-header">
8+
<h2 class="card-title">New topic</h2>
9+
</div>
10+
<div class="card-content">
11+
<div class="card-body">
12+
<%= render "form", topic: @topic %>
13+
</div>
14+
</div>
15+
</div>
16+
</div>
17+
</div>
18+
</section>

0 commit comments

Comments
 (0)