diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 94e7183..b2e8b65 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,13 @@ 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 current_provider + @current_provider ||= begin + Current.user.providers.find(cookies.signed[:current_provider_id]) + rescue ActiveRecord::RecordNotFound + Current.user.providers.first + end + end + helper_method :current_provider end diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb new file mode 100644 index 0000000..106248e --- /dev/null +++ b/app/controllers/settings_controller.rb @@ -0,0 +1,21 @@ +class SettingsController < ApplicationController + def provider + provider = provider_scope.find(provider_params[:id]) + cookies.signed[:current_provider_id] = provider.id if provider + redirect_to request.referer || root_path + end + + private + + def provider_params + params.expect(provider: :id) + end + + def provider_scope + @provider_scope ||= if Current.user.is_admin? + Provider.all + else + Current.user.providers + end + end +end diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 27e1d8b..a3b96aa 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -3,7 +3,7 @@ class TopicsController < ApplicationController def index @topics = scope.search_with_params(search_params) - @providers = scope.map(&:provider).uniq.sort_by(&:name) + @available_providers = other_available_providers @languages = scope.map(&:language).uniq.sort_by(&:name) end @@ -45,16 +45,22 @@ def archive private + def other_available_providers + return [] unless Current.user.providers.any? + + Current.user.providers.where.not(id: current_provider.id) + end + def topic_params params.require(:topic).permit(:title, :description, :uid, :language_id, :provider_id, documents: []) end - helper_method :search_params def search_params return {} unless params[:search].present? params.require(:search).permit(:query, :state, :provider_id, :language_id, :year, :month, :order) end + helper_method :search_params def set_topic @topic = Topic.find(params[:id]) @@ -63,8 +69,15 @@ def set_topic def scope @scope ||= if Current.user.is_admin? Topic.all + elsif current_provider.present? + current_provider.topics else Current.user.topics end.includes(:language, :provider) end + + def topics_title + current_provider.present? ? "#{current_provider.name}/topics" : "Topics" + end + helper_method :topics_title end diff --git a/app/javascript/controllers/topics_controller.js b/app/javascript/controllers/topics_controller.js index a52693a..8670f60 100644 --- a/app/javascript/controllers/topics_controller.js +++ b/app/javascript/controllers/topics_controller.js @@ -2,14 +2,18 @@ import { Controller } from "@hotwired/stimulus" import { useDebounce } from "stimulus-use" export default class extends Controller { - static targets = [ "form" ] + static targets = [ "searchForm", "chooseForm" ] static debounces = [ "search" ] connect() { useDebounce(this, { wait: 300 }) } - search() { - this.formTarget.requestSubmit() + searchTopics() { + this.searchFormTarget.requestSubmit() + } + + chooseProvider() { + this.chooseFormTarget.requestSubmit() } } diff --git a/app/models/provider.rb b/app/models/provider.rb index aaa6488..7137874 100644 --- a/app/models/provider.rb +++ b/app/models/provider.rb @@ -18,6 +18,7 @@ class Provider < ApplicationRecord has_many :regions, through: :branches has_many :contributors has_many :users, through: :contributors + has_many :topics validates :name, :provider_type, presence: true validates :name, uniqueness: true diff --git a/app/views/topics/_choose_provider.html.erb b/app/views/topics/_choose_provider.html.erb new file mode 100644 index 0000000..24dbd92 --- /dev/null +++ b/app/views/topics/_choose_provider.html.erb @@ -0,0 +1,12 @@ +<%= form_for :provider , url: provider_settings_path, method: :put, data: { controller: "topics", topics_target: "chooseForm", turbo_frame: "topics", turbo_action: "advance" } do |f| %> +
Some important information or instruction can be placed here.
- <%= turbo_frame_tag "topic-list" do %> -Title | -Description | -UID | -Language | -Provider | -State | -Actions | -
---|
Some important information or instruction can be placed here.
+ <%= turbo_frame_tag "topic-list" do %> +Title | +Description | +UID | +Language | +Provider | +State | +Actions | +
---|