Skip to content

Commit

Permalink
Set current provider 75 (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcollie2 authored Mar 2, 2025
2 parents 5c7337d + d0ac74e commit e336916
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 48 deletions.
9 changes: 9 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions app/controllers/settings_controller.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 15 additions & 2 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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])
Expand All @@ -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
10 changes: 7 additions & 3 deletions app/javascript/controllers/topics_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
1 change: 1 addition & 0 deletions app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions app/views/topics/_choose_provider.html.erb
Original file line number Diff line number Diff line change
@@ -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| %>
<div class="form-body">
<div class="row">
<div class="col-md-6 col-12">
<div class="form-group">
<%= f.label :provider %>
<%= f.select :id, options_from_collection_for_select(providers, :id, :name), { prompt: "Change provider" }, class: "form-select", data: { action: "change->topics#chooseProvider" } %>
</div>
</div>
</div>
</div>
<% end %>
24 changes: 9 additions & 15 deletions app/views/topics/_search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,43 @@
</div>
<div class="card-content">
<div class="card-body">
<%= form_for :search, url: topics_path, method: :get, data: { controller: "topics", topics_target: "form", turbo_frame: "topic-list", turbo_action: "advance" } do |f| %>
<%= form_for :search, url: topics_path, method: :get, data: { controller: "topics", topics_target: "searchForm", turbo_frame: "topic-list", turbo_action: "advance" } do |f| %>
<div class="form-body">
<div class="row">
<div class="col-md-6 col-12">
<div class="col-12">
<div class="form-group">
<%= f.label :provider %>
<%= f.select :provider_id, options_from_collection_for_select(providers, :id, :name, params[:provider_id]), { prompt: "Select provider" }, class: "form-select", data: { action: "change->topics#search" } %>
<%= f.label :query %>
<%= f.text_field :query, value: params[:query], class: "form-control", data: { action: "input->topics#searchTopics" } %>
</div>
</div>
<div class="col-md-6 col-12">
<div class="form-group">
<%= f.label :language %>
<%= f.select :language_id, options_from_collection_for_select(languages, :id, :name, params[:provider_id]), { prompt: "Select language" }, class: "form-select", data: { action: "change->topics#search" } %>
</div>
</div>
<div class="col-md-6 col-12">
<div class="form-group">
<%= f.label :query %>
<%= f.text_field :query, value: params[:query], class: "form-control", data: { action: "input->topics#search" } %>
<%= f.select :language_id, options_from_collection_for_select(languages, :id, :name, params[:provider_id]), { prompt: "Select language" }, class: "form-select", data: { action: "change->topics#searchTopics" } %>
</div>
</div>
<div class="col-md-3 col-12">
<div class="form-group">
<%= f.label :year %>
<%= f.select :year, options_for_select((Date.today.year-10..Date.today.year).to_a, params[:year]), { prompt: "Select year" }, class: "form-select", data: { action: "change->topics#search" } %>
<%= f.select :year, options_for_select((Date.today.year-10..Date.today.year).to_a, params[:year]), { prompt: "Select year" }, class: "form-select", data: { action: "change->topics#searchTopics" } %>
</div>
</div>
<div class="col-md-3 col-12">
<div class="form-group">
<%= f.label :month %>
<%= f.select :month, options_for_select((1..12).to_a, params[:month]), { prompt: "Select month" }, class: "form-select", data: { action: "change->topics#search" } %>
<%= f.select :month, options_for_select((1..12).to_a, params[:month]), { prompt: "Select month" }, class: "form-select", data: { action: "change->topics#searchTopics" } %>
</div>
</div>
<div class="col-md-6 col-12">
<div class="form-group">
<%= f.label :state %>
<%= f.select :state, options_for_select(Topic::STATES.index_with(&:itself), params[:state]), { prompt: "Select state" }, class: "form-select", data: { action: "change->topics#search" } %>
<%= f.select :state, options_for_select(Topic::STATES.index_with(&:itself), params[:state]), { prompt: "Select state" }, class: "form-select", data: { action: "change->topics#searchTopics" } %>
</div>
</div>
<div class="col-md-6 col-12">
<div class="form-group">
<%= f.label :order %>
<%= f.select :order, options_for_select(Topic::SORTS.reverse.index_with(&:itself), params[:order]), {}, class: "form-select", data: { action: "change->topics#search" } %>
<%= f.select :order, options_for_select(Topic::SORTS.reverse.index_with(&:itself), params[:order]), {}, class: "form-select", data: { action: "change->topics#searchTopics" } %>
</div>
</div>
<div class="col-12 d-flex justify-content-end">
Expand Down
61 changes: 33 additions & 28 deletions app/views/topics/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,42 @@
<div class="row" id="table-striped">
<div class="col-12 cold-md-12">
<%= render "search", providers: @providers, languages: @languages, params: search_params %>
<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>
<%= turbo_frame_tag "topic-list" do %>
<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>
<%= render "list", topics: @topics %>
</table>
</div>
<%= turbo_frame_tag "topics" do %>
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h2 class="card-title"><%= topics_title %></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">
<% if @available_providers.any? %>
<%= render "choose_provider", providers: @available_providers %>
<% end %>
<p class="card-text"> Some important information or instruction can be placed here.</p>
<%= turbo_frame_tag "topic-list" do %>
<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>
<%= render "list", topics: @topics %>
</table>
</div>
<% end %>
</div>
</div>
</div>
</div>
<% end %>
</div>
</div>
</section>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
resources :topics do
put :archive, on: :member
end
resource :settings, only: [] do
put :provider, on: :collection
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
33 changes: 33 additions & 0 deletions spec/requests/settings/provider_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "rails_helper"

describe "Settings", type: :request do
describe "PUT /settings/provider" do
let(:user) { create(:user) }
let(:provider) { create(:provider) }

before { sign_in(user) }

context "when provider cannot be found" do
it "does not update current provider" do
put provider_settings_url, params: { provider: { id: provider.id } }

expect(response).to have_http_status(:not_found)
end
end

context "when user has access to provider" do
before do
user.providers << provider
end

it "updates current provider" do
put provider_settings_url, params: { provider: { id: provider.id } }

signed_cookies = ActionDispatch::Request.new(Rails.application.env_config).cookie_jar

expect(response).to redirect_to(root_url)
expect(signed_cookies.signed[:current_provider_id]).to eq(provider.id)
end
end
end
end

0 comments on commit e336916

Please sign in to comment.