diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 94e7183f..6a19e3d0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb new file mode 100644 index 00000000..ac1331b1 --- /dev/null +++ b/app/controllers/topics_controller.rb @@ -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 diff --git a/app/models/topic.rb b/app/models/topic.rb new file mode 100644 index 00000000..e96609ca --- /dev/null +++ b/app/models/topic.rb @@ -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 diff --git a/app/views/layouts/_sidebar.html.erb b/app/views/layouts/_sidebar.html.erb index 89ac0ea1..3341c084 100644 --- a/app/views/layouts/_sidebar.html.erb +++ b/app/views/layouts/_sidebar.html.erb @@ -59,10 +59,10 @@
+ Title: + <%= topic.title %> +
+ ++ Description: + <%= topic.description %> +
+ ++ UID: + <%= topic.uid %> +
+ ++ Language: + <%= link_to topic.language.name, topic.language %> +
+ ++ Provider: + <%= link_to topic.provider.name, topic.provider %> +
+Some important information or instruction can be placed here.
+| Title | +Description | +UID | +Language | +Provider | +State | +Actions | +
|---|---|---|---|---|---|---|
| <%= topic.title %> | +<%= topic.description.truncate(25, omission: "...") %> | +<%= topic.uid.truncate(10, omission: "...") %> | +<%= topic.language.name %> | +<%= topic.provider.name %> | +<%= topic.state %> | ++ <%= link_to topic, class: "btn btn-primary btn-sm" do %> + View + <% end %> + <%= link_to edit_topic_path(topic), class: "btn btn-secondary btn-sm" do %> + Edit + <% end %> + <%= link_to archive_topic_path(topic), method: :put, data: { confirm: "Are you sure?" }, class: "btn btn-danger btn-sm" do %> + Archive + <% end %> + <% if Current.user.is_admin? %> + <%= link_to topic, method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-danger btn-sm" do %> + Delete + <% end %> + <% end %> + | +