Skip to content

Commit

Permalink
Merge pull request #9 from jahseng-lee/feature/user-specific-visas
Browse files Browse the repository at this point in the history
Feature/user specific visas
  • Loading branch information
jahseng-lee authored Dec 7, 2023
2 parents 880c151 + 3ecf204 commit b5c278a
Show file tree
Hide file tree
Showing 32 changed files with 611 additions and 237 deletions.
21 changes: 21 additions & 0 deletions app/controllers/citizenships/search_countries_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Citizenships::SearchCountriesController < ApplicationController
def index
@query = params[:query]

@countries = Country
.search_by_name(I18n.transliterate(@query))
.limit(10)

respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.replace(
"search-results",
partial: "citizenships/search_results",
locals: {
countries: @countries,
}
)
end
end
end
end
47 changes: 47 additions & 0 deletions app/controllers/citizenships_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class CitizenshipsController < ApplicationController
def new
respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.replace(
"profile-citizenship-section",
partial: "citizenships/add_citizenship_form"
)
end
end
end

def create
@citizenship = Citizenship.new(
citizenship_params.merge(
user: current_user
)
)

authorize(@citizenship)

unless @citizenship.save
flash.now[:error_create_citizenship] = "Oops! Something went wrong. Please try again"
end

respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.replace(
"profile-citizenship-section",
partial: "profiles/citizenships"
)
end
end
end

def destroy
raise NotImplementedError, "TODO"
end

private

def citizenship_params
params.require(:citizenship).permit(
:country_id
)
end
end
32 changes: 0 additions & 32 deletions app/controllers/countries_controller.rb

This file was deleted.

3 changes: 1 addition & 2 deletions app/controllers/visas/search_locations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# TODO rename to SearchCountriesController
class Visas::SearchLocationsController < ApplicationController

def index
if current_user.admin?
# NOTE: params[:query] is working

@visa = Visa.find(params[:visa_id])
@query = params[:query]

Expand Down
11 changes: 8 additions & 3 deletions app/controllers/visas_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def create

@visa = Visa.new(
country: @country,
name: visa_params[:name]
name: visa_params[:name],
description: visa_params[:description]
)

if @visa.save
Expand All @@ -31,6 +32,7 @@ def create
location_id: @location.id
)
else
# TODO this doesn't work... the error doesn't show. Something about turbo
flash[:error_save_visa] = "Couldn't save visa. Please try again"

render :new
Expand All @@ -52,7 +54,10 @@ def update

authorize(@country, :edit?)

@visa.assign_attributes(name: visa_params[:name])
@visa.assign_attributes(
name: visa_params[:name],
description: visa_params[:description]
)

if @visa.save
flash[:success_save_visa] = "Visa saved"
Expand Down Expand Up @@ -84,6 +89,6 @@ def destroy
private

def visa_params
params.require(:visa).permit(:name)
params.require(:visa).permit(:name, :description)
end
end
17 changes: 17 additions & 0 deletions app/helpers/visa_information_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module VisaInformationHelper
def helper_eligible_visas(country:, user:)
if user.admin? || user.citizenships.none?
country.visas
else
# Non-admin user who has added citizenships
country
.visas
.left_joins(:eligible_countries_for_visas)
.where(
eligible_countries_for_visas: {
country_id: user.citizenships.pluck(:country_id)
}
)
end
end
end
4 changes: 2 additions & 2 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ application.register("modal", ModalController)
import NavbarLinkController from "./navbar_link_controller"
application.register("navbar-link", NavbarLinkController)

import SearchEligibleCountriesController from "./search_eligible_countries_controller"
application.register("search-eligible-countries", SearchEligibleCountriesController)
import SearchCountriesController from "./search_countries_controller"
application.register("search-countries", SearchCountriesController)

import SearchLocationsController from "./search_locations_controller"
application.register("search-locations", SearchLocationsController)
4 changes: 4 additions & 0 deletions app/models/citizenship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Citizenship < ApplicationRecord
belongs_to :user
belongs_to :country
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class User < ApplicationRecord
has_many :channel_members
has_many :chat_channels,
through: :channel_members
has_many :citizenships
has_one :profile_picture
has_many :reviews

Expand Down
1 change: 1 addition & 0 deletions app/models/visa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ class Visa < ApplicationRecord
class_name: "Country"

validates :name, presence: true
validates :description, presence: true
end
5 changes: 5 additions & 0 deletions app/policies/citizenship_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class CitizenshipPolicy < ApplicationPolicy
def create?
record.user == user
end
end
32 changes: 32 additions & 0 deletions app/views/citizenships/_add_citizenship_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%= turbo_frame_tag "profile-citizenship-section" do %>
<div
data-controller="search-countries"
data-search-countries-search-url-value=<%= citizenships_search_countries_path %>
>
<ul>
<li>
TODO delete button for citizenships
</li>
<li>
TODO cancel button for this section
</li>
</ul>

<%= label_tag "I am a citizen of..." %>
<%= text_field_tag(
"search-countries",
"",
class: "form-control mb-2",
placeholder: "Search countries...",
data: {
"search-countries-target": "searchField",
action: "search-countries#searchOnDebounce"
}
) %>

<%= turbo_frame_tag "search-results" do %>
<div data-search-countries-target="searchResults">
</div>
<% end %>
</div>
<% end %>
17 changes: 17 additions & 0 deletions app/views/citizenships/_search_results.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%= turbo_frame_tag(
"search-results",
data: {
"search-countries-target": "searchResults",
}
) do %>
<% countries.each do |country| %>
<%= form_for Citizenship.new do |f| %>
<%= f.hidden_field :country_id, value: country.id %>

<%= f.button "", class: 'btn btn-primary mb-1' do %>
<i class="bi bi-plus-circle-fill" aria-hidden="true"></i>
<%= country.name %>
<% end %>
<% end %>
<% end %>
<% end %>
49 changes: 49 additions & 0 deletions app/views/profiles/_citizenships.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<%= turbo_frame_tag "profile-citizenship-section" do %>
<% if flash[:error_create_citizenship].present? %>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<%= flash[:error_create_citizenship] %>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<% end %>

<% if current_user.citizenships.any? %>
<div>
I am a citizen of
<% if current_user.citizenships.count == 1 %>
<span class="fw-bold">
<%= current_user.citizenships.first.country.name %>
</span>
<% else %>
<ul>
<% current_user.citizenships.each do |citizenship| %>
<li>
<span class="fw-bold"><%= citizenship.country.name %></span>
</li>
<% end %>
</ul>
<% end %>
</div>

<%= link_to(
new_citizenship_path,
"data-turbo-stream": ""
) do %>
<i class="bi bi-plus-circle-fill" aria-hidden="true"></i>
Add citizenship
<% end %>
<% else %>
<div class="mb-2">
<%= link_to(
new_citizenship_path,
"data-turbo-stream": ""
) do %>
<i class="bi bi-plus-circle-fill" aria-hidden="true"></i>
Add citizenship
<% end %>
</div>

<div class="fst-italic">
We use your citizenships to show ensure any visa information shown is relevant to you.
</div>
<% end %>
<% end %>
16 changes: 12 additions & 4 deletions app/views/profiles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
</div>
<% end %>

<h1>Profile</h1>

<div class="row">
<div class="col">
<div class="mb-3">
Expand Down Expand Up @@ -54,7 +52,7 @@
</div>

<%= form_for @user, data: { turbo_frame: "_top" } do |f| %>
<%= f.label :display_name %>
<strong><%= f.label :display_name %></strong>
<div class="row">
<div class="col">
<div class="mb-3">
Expand All @@ -80,7 +78,7 @@
</div>

<div class="mb-3">
<%= f.label :email %>
<strong><%= f.label :email %></strong>
<%= f.text_field(
:email,
class: "form-control-plaintext",
Expand All @@ -100,6 +98,16 @@
</div>

<div class="col">
<div class="card mb-3">
<div class="card-header">
Citizenships
</div>

<div class="card-body">
<%= render partial: "profiles/citizenships" %>
</div>
</div>

<div class="card">
<div class="card-header">
Subscription
Expand Down
Loading

0 comments on commit b5c278a

Please sign in to comment.