Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ class ApplicationController < ActionController::Base
include Authentication
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
allow_browser versions: :modern

def check_admin!
redirect_to root_path unless Current.user.is_admin?
end
end
61 changes: 61 additions & 0 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class TopicsController < ApplicationController
before_action :set_topic, only: [ :show, :edit, :update, :destroy, :archive ]
before_action :check_admin!, only: :destroy

def index
@topics = scope.includes(:language, :provider)
end

def new
@topic = scope.new
end

def create
@topic = scope.new(topic_params)

if @topic.save
redirect_to topics_path
else
render :new
end
end

def show
end

def edit
end

def update
@topic.update(topic_params)
redirect_to topics_path
end

def destroy
@topic.destroy
redirect_to topics_path
end

def archive
@topic.archived!
redirect_to topics_path
end

private

def topic_params
params.require(:topic).permit(:title, :description, :uid, :language_id, :provider_id)
end

def set_topic
@topic = Topic.find(params[:id])
end

def scope
@scope ||= if Current.user.is_admin?
Topic.all
else
Topic.active
end
end
end
12 changes: 12 additions & 0 deletions app/models/topic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Topic < ApplicationRecord
belongs_to :language
belongs_to :provider

validates :title, :language_id, :provider_id, presence: true

STATES = %i[active archived].freeze

enum :state, STATES.map.with_index.to_h

scope :active, -> { where(state: :active) }
end
4 changes: 2 additions & 2 deletions app/views/layouts/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@
</li>

<li class="sidebar-item">
<a href="ui-file-uploader.html" class="sidebar-link">
<%= link_to topics_path, class: "sidebar-link" do %>
<i class="bi bi-tags-fill"></i>
<span>Topics</span>
</a>
<% end %>
</li>

<li class="sidebar-item">
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_errors.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% if resource.errors.any? %>
<div style="color: red">
<h4><%= "#{pluralize(resource.errors.count, "error")} prohibited this #{resource.class_name} from being saved:" %>></h4>
<h4><%= "#{pluralize(resource.errors.count, "error")} prohibited this #{resource.class.name} from being saved:" %>></h4>
<ul>
<% resource.errors.each do |error| %>
<li><%= error.full_message %></li>
Expand Down
34 changes: 34 additions & 0 deletions app/views/topics/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

<%= form_for topic do |f| %>
<div class="form-body">
<div class="row">
<%= render "shared/errors", resource: topic %>
<div class="col-12">
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: "form-control", placeholder: "Title" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: "form-control", placeholder: "Descripion" %>
</div>
<div class="form-group">
<%= f.label :language %>
<%= f.collection_select :language_id, Language.all, :id, :name, { prompt: "Select Language" }, class: "form-select" %>
</div>
<div class="form-group">
<%= f.label :provider %>
<%= f.collection_select :provider_id, Provider.all, :id, :name, { prompt: "Select Provider" }, class: "form-select" %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: "form-control", placeholder: "Descripion" %>
</div>
<div class="col-12 d-flex justify-content-end">
<%= f.submit "Save", class: "btn btn-primary me-1 mb-1" %>
<%= link_to "Cancel", topics_path, class: "btn btn-light-secondary me-1 mb-1" %>
</div>
</div>
</div>
</div>
<% end %>
26 changes: 26 additions & 0 deletions app/views/topics/_topic.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div id="<%= dom_id topic %>">
<p>
<strong>Title:</strong>
<%= topic.title %>
</p>

<p>
<strong>Description:</strong>
<%= topic.description %>
</p>

<p>
<strong>UID:</strong>
<%= topic.uid %>
</p>

<p>
<strong>Language:</strong>
<%= link_to topic.language.name, topic.language %>
</p>

<p>
<strong>Provider:</strong>
<%= link_to topic.provider.name, topic.provider %>
</p>
</div>
19 changes: 19 additions & 0 deletions app/views/topics/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<% content_for :title, "Editing topic" %>

<section id="basic-vertical-layouts">
<div class="row match-height">
<div class="col-md-6 col-12">
<div class="card">
<div class="card-header">
<h2 class="card-title">Edit topic</h2>
</div>
<div class="card-content">
<div class="card-body">
<%= render "form", topic: @topic %>
</div>
</div>
</div>
</div>
</div>
</section>

64 changes: 64 additions & 0 deletions app/views/topics/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<% content_for :title, "Topics" %>

<section class="section">
<div class="row" id="table-striped">
<div class="col-12 cold-md-12">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h2 class="card-title">Topics</h2>
<%= link_to new_topic_path, class: "btn btn-primary" do %>
<i class="bi bi-plus"></i> Add New Topic
<% end %>
</div>
<div class="card-content">
<div class="card-body">
<p class="card-text"> Some important information or instruction can be placed here.</p>
<div class="table-responsive">
<table class="table table-lg table-striped mb-0">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>UID</th>
<th>Language</th>
<th>Provider</th>
<th>State</th>
<th class="text-end">Actions</th>
</tr>
</thead>
<tbody>
<% @topics.each do |topic| %>
<tr>
<td class="text-bold-500"><%= topic.title %></td>
<td class="text-bold-500"><%= topic.description.truncate(25, omission: "...") %></td>
<td class="text-bold-500"><%= topic.uid.truncate(10, omission: "...") %></td>
<td class="text-bold-500"><%= topic.language.name %></td>
<td class="text-bold-500"><%= topic.provider.name %></td>
<td class="text-bold-500"><%= topic.state %></td>
<td class="text-end">
<%= link_to topic, class: "btn btn-primary btn-sm" do %>
<i class="bi bi-search"></i> View
<% end %>
<%= link_to edit_topic_path(topic), class: "btn btn-secondary btn-sm" do %>
<i class="bi bi-pencil"></i> Edit
<% end %>
<%= link_to archive_topic_path(topic), method: :put, data: { confirm: "Are you sure?" }, class: "btn btn-danger btn-sm" do %>
<i class="bi bi-archive"></i> Archive
<% end %>
<% if Current.user.is_admin? %>
<%= link_to topic, method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger btn-sm" do %>
<i class="bi bi-trash"></i> Delete
<% end %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
18 changes: 18 additions & 0 deletions app/views/topics/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% content_for :title, "New topic" %>

<section id="basic-vertical-layouts">
<div class="row match-height">
<div class="col-md-6 col-12">
<div class="card">
<div class="card-header">
<h2 class="card-title">New topic</h2>
</div>
<div class="card-content">
<div class="card-body">
<%= render "form", topic: @topic %>
</div>
</div>
</div>
</div>
</div>
</section>
18 changes: 18 additions & 0 deletions app/views/topics/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div style="color: green"><%= notice %></div>

<%= render @topic %>

<div class="mt-4">
<%= link_to "Edit this topic", edit_topic_path(@topic) %> |
<%= link_to "Back to topics", topics_path %>
</div>

<div>
<%= button_to "Archive this topic", archive_topic_path(@topic), method: :put, class: "btn btn-danger mt-4" %>
</div>

<% if Current.user.is_admin? %>
<div>
<%= button_to "Delete this topic", @topic, method: :delete, class: "btn btn-danger mt-4" %>
</div>
<% end %>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
resource :registration, only: %i[new create]
resource :session
resources :users
resources :topics do
put :archive, on: :member
end

# Render dynamic PWA files from app/views/pwa/* (remember to link manifest in application.html.erb)
# get "manifest" => "rails/pwa#manifest", as: :pwa_manifest
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20250204140110_create_topics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateTopics < ActiveRecord::Migration[8.0]
def change
create_table :topics do |t|
t.references :provider
t.references :language
t.string :title, null: false
t.text :description, null: false
t.uuid :uid, default: 'gen_random_uuid()', null: false
t.integer :state, default: 0, null: false

t.timestamps
end
end
end
29 changes: 21 additions & 8 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,30 @@
].each do |language|
Language.find_or_create_by!(language)
end

[
{ name: "Provided by the government", provider_type: "government" },
].each do |provider|
Provider.find_or_create_by!(provider)
end

[
{
title: "Introduction to English",
description: "Learn the basics of English",
language_id: Language.find_by(name: "english").id,
provider_id: Provider.find_by(name: "Provided by the government").id,
uid: "d290f1ee-6c54-4b01-90e6-d701748f0851",
state: :active,
},
{
title: "Introduction to Spanish",
description: "Learn the basics of Spanish",
language_id: Language.find_by(name: "spanish").id,
provider_id: Provider.find_by(name: "Provided by the government").id,
uid: "d290f1ee-6c54-4b01-90e6-d701748f0852",
state: :archived,
},
].each do |topic|
Topic.find_or_create_by!(topic)
end
Loading