Skip to content

Commit

Permalink
Refactoring Opportunies Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
patanj101 committed Jun 7, 2024
1 parent f6d81b7 commit cc6e131
Show file tree
Hide file tree
Showing 17 changed files with 331 additions and 70 deletions.
2 changes: 1 addition & 1 deletion app/assets/builds/tailwind.css

Large diffs are not rendered by default.

31 changes: 11 additions & 20 deletions app/controllers/opportunities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,25 @@ class OpportunitiesController < ApplicationController

def index
load_opportunities
load_facets
end

private

def load_opportunities
filtered_jobs = JobFilter.new(params).filter_and_sort
def load_facets
@facets = OpportunityFacetsBuilder.call(@opportunities, opportunity_params)
end

@jobs = filtered_jobs.eager_load(associated_tables)
.order(sort_order(params[:sort]))
.page(params[:page])
def load_opportunities
@opportunities = OpportunitiesFetcher.call(opportunity_scope, opportunity_params)

@pagy, @records = pagy(@jobs, items: 20)
@pagy, @records = pagy(@opportunities, items: 20)
end

def associated_tables
%i[requirement company locations countries]
def opportunity_params
{}
end

def sort_order(sort_param)
sort_options = {
'title' => 'jobs.title ASC',
'title_desc' => 'jobs.title DESC',
'company' => 'companies.name ASC',
'company_desc' => 'companies.name DESC',
'created_at' => 'jobs.created_at DESC',
'created_at_asc' => 'jobs.created_at ASC'
}.freeze

sort_options.fetch(sort_param, 'jobs.created_at DESC')
end
# policy_scope(Job)
def opportunity_scope = Job.all
end
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
module ApplicationHelper
include Pagy::Frontend
end
9 changes: 9 additions & 0 deletions app/helpers/pagination_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module PaginationHelper
def pagy_next(pagy)
pagy_url_for(pagy, pagy.next, absolute: false)
end

def pagy_prev(pagy)
pagy_url_for(pagy, pagy.prev, absolute: false)
end
end
25 changes: 25 additions & 0 deletions app/queries/application_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

class ApplicationQuery
def self.call(...)
new(...).call
end

def default_order = asc_order

def asc_order
:created_at
end

def desc_order
{ created_at: :desc }
end

def updated_at_asc_order
:updated_at
end

def updated_at_desc_order
{ updated_at: :desc }
end
end
18 changes: 18 additions & 0 deletions app/queries/opportunities_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

class OpportunitiesQuery < ApplicationQuery
def initialize(relation = Job.all)
@relation = relation
end

def call
@relation.eager_load(*associations)
# .order(desc_order)
end

private

def associations
%i[requirement company locations countries roles]
end
end
7 changes: 7 additions & 0 deletions app/tasks/application_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class ApplicationTask
def self.call(...)
new(...).call
end
end
54 changes: 54 additions & 0 deletions app/tasks/opportunities_fetcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

class OpportunitiesFetcher < ApplicationTask
def initialize(opportunities, params)
@opportunities = opportunities
@params = params.to_h
end

def call
return false unless processable

process
end

private

def processable
true
end

def process
# simple_query || full_search_query
plain_query
end

def plain_query
OpportunitiesQuery.call(@opportunities)
end

# def plain_query
# filtered_jobs = JobFilter.new(@params, @opportunities).filter_and_sort

# @jobs = filtered_jobs.eager_load(associated_tables)
# .order(sort_order(params[:sort]))
# # .page(params[:page])
# end

# def associated_tables
# %i[requirement company locations countries]
# end

# def sort_order(sort_param)
# sort_options = {
# 'title' => 'jobs.title ASC',
# 'title_desc' => 'jobs.title DESC',
# 'company' => 'companies.name ASC',
# 'company_desc' => 'companies.name DESC',
# 'created_at' => 'jobs.created_at DESC',
# 'created_at_asc' => 'jobs.created_at ASC'
# }.freeze

# sort_options.fetch(sort_param, 'jobs.created_at DESC')
# end
end
98 changes: 98 additions & 0 deletions app/tasks/opportunity_facets_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# frozen_string_literal: true

class OpportunityFacetsBuilder < ApplicationTask
def initialize(opportunities, params)
@opportunities = opportunities
@params = params.to_h
end

def call
return false unless processable

process
end

private

def processable
true
end

def process
build_facets
end

def build_facets
@facets = {}
build_posted_array
build_seniority_array
build_location_array
build_role_array
build_employment_array
@facets
end

def build_posted_array
date_cutoffs = Constants::DateConversion::CONVERT_TO_DAYS.transform_values { |v| v.days.ago.beginning_of_day }
results = date_cutoffs.map do |period, cutoff|
period_id = period.downcase.gsub(/last |within a /, '').gsub(' ', '-')
count = @opportunities.where(date_posted: cutoff..Date.today).count
params = @params[:posted] ? @params[:posted].include?(period_id) : period == 'Any time'

['radio', period, period_id, count, params]
end.compact
@facets['posted'] = results
end

def build_seniority_array
results = @opportunities.group(:seniority).count.map do |seniority, count|
next unless seniority

seniority_id = seniority.downcase.split.first
params = @params[:seniority]&.include?(seniority_id)

['checkbox', seniority, seniority_id, count, params]
end.compact
@facets['seniority'] = sort_by_count_desc(results)
end

def build_location_array
results = @opportunities.group(:'locations.city').count.map do |location, count|
location_id = location ? location.downcase.gsub(' ', '_') : 'remote'
params = location ? @params[:location]&.include?(location_id) : @params[:location]&.include?('remote')
location ||= 'Remote'

['checkbox', location, location_id, count, params]
end.compact
@facets['location'] = sort_by_count_desc(results)
end

def build_role_array
results = @opportunities.group(:'roles.name').count.map do |role, count|
next unless role

role_id = role
params = @params[:role]&.include?(role_id)
role = role.titleize

['checkbox', role, role_id, count, params]
end.compact
@facets['role'] = sort_by_count_desc(results)
end

def build_employment_array
results = @opportunities.group(:employment_type).count.map do |employment, count|
next unless employment

employment_id = employment.downcase.gsub('-', '_')
params = @params[:employment]&.include?(employment_id)

['checkbox', employment, employment_id, count, params]
end.compact
@facets['employment'] = sort_by_count_desc(results)
end

def sort_by_count_desc(data)
data.sort_by { |item| -item[3] }
end
end
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% if request.fullpath.eql?('/protocol') %>
<%= yield %>
<% elsif request.fullpath.eql?('/opportunities') %>
<% elsif request.fullpath.include?('/opportunities') %>
<!DOCTYPE html>
<html class="h-full dark" style="color-scheme: dark;">
<head>
Expand Down
3 changes: 2 additions & 1 deletion app/views/opportunities/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<% @records.each do |opportunity| %>
<%= render 'opportunities/opportunity', opportunity: %>
<% end %>
</ul>
</ul>
<%= render 'shared/template/pagination', pagy: @pagy %>
</article>
38 changes: 23 additions & 15 deletions app/views/shared/template/_page_footer.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
<% social_links = [
{
sr_text: 'Follow us on X',
icon_name: 'logos/x'
},
{
sr_text: 'Follow us on Github',
icon_name: 'logos/github'
},
{
sr_text: 'Join our Discord server',
icon_name: 'logos/discord'
},
] %>
<footer class="mx-auto w-full max-w-2xl space-y-10 pb-16 lg:max-w-5xl">
<div class="flex">
<div class="flex flex-col items-start gap-3">
<% if defined?(prev_options)%>
<%= link_to prev_options[:link_path], class: button_klasses(theme: :zinc) do %>
<%= inline_svg_tag('template/icons/left_arrow.svg') %> Previous
<div class="flex flex-col items-center justify-between gap-5 border-t border-zinc-900/5 pt-8 sm:flex-row dark:border-white/5">
<p class="text-xs text-zinc-600 dark:text-zinc-400">
&copy; Copyright 2024. All rights reserved.
</p>
<div class="flex gap-4">
<% social_links.each do |link| %>
<%= link_to '#', class: 'group' do %>
<span class="sr-only"><%= link[:sr_text] %></span>
<%= inline_svg_tag("template/icons/#{link[:icon_name]}.svg") %>
<% end %>
<%= link_to prev_options[:title], prev_options[:link_path], tab_index: -1, aria_hidden: true, class: 'text-base font-semibold text-zinc-900 transition hover:text-zinc-600 dark:text-white dark:hover:text-zinc-300' %>
<% end %>
</div>
<div class="ml-auto flex flex-col items-end gap-3">
<% if defined?(next_options) %>
<%= link_to next_options[:link_path], class: button_klasses(theme: :zinc) do %>
Next <%= inline_svg_tag('template/icons/right_arrow.svg') %>
<% end %>
<%= link_to next_options[:title], next_options[:link_path], tab_index: -1, aria_hidden: true, class: 'text-base font-semibold text-zinc-900 transition hover:text-zinc-600 dark:text-white dark:hover:text-zinc-300' %>
<% end %>
</div>
</div>
<%= render 'shared/template/small_print' %>
</footer>
27 changes: 27 additions & 0 deletions app/views/shared/template/_pagination.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<nav class="flex items-center justify-between border-t border-gray-200 dark:border-gray-800 bg-white dark:bg-zinc-900 px-4 py-3 sm:px-6" aria-label="Pagination">
<div class="invisible md:visible sm:block">
<p class="text-sm text-gray-700 dark:text-gray-500">
Showing
<!-- space -->
<span class="font-medium"><%= pagy.from %></span>
<!-- space -->
to
<!-- space -->
<span class="font-medium"><%= pagy.to %></span>
<!-- space -->
of
<!-- space -->
<span class="font-medium"><%= pagy.count %></span>
<!-- space -->
results
</p>
</div>
<div class="flex flex-2 justify-between sm:justify-end">
<%= link_to pagy_prev(pagy), class: button_klasses(theme: :zinc) do %>
<%= inline_svg_tag('template/icons/left_arrow.svg') %> Previous
<% end %>
<%= link_to pagy_next(pagy), class: button_klasses(theme: :zinc) do %>
Next <%= inline_svg_tag('template/icons/right_arrow.svg') %>
<% end %>
</div>
</nav>
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</li>
<% end %>
<% nav_groups.each do |group| %>
<%= render 'shared/template/layout/navigation_group_item', group: %>
<%= render 'shared/template/layout/navigation_group_item', group: group if false %>
<% end %>
<li class="sticky bottom-0 z-10 mt-6 min-[416px]:hidden">
<button type="button" href="#" class="w-full">
Expand Down
Loading

0 comments on commit cc6e131

Please sign in to comment.