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 @@