Skip to content

Commit

Permalink
Remove scope usage
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineGirard committed Oct 30, 2024
1 parent 3459c9a commit 64877cb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
14 changes: 13 additions & 1 deletion app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ def mds

def accessibility; end

def contact; end
def contact
territories_with_phone_number = Territory.where.not(phone_number_formatted: nil)
territories_group_by_department = territories_with_phone_number.where(departement_number: Territory::DEPARTEMENTS_NAMES.keys)
.order(:departement_number, :name).group_by(&:departement_number)

territories_without_department = territories_with_phone_number.where.not(departement_number: Territory::DEPARTEMENTS_NAMES.keys)
.order(:name)

render locals: {
territories_group_by_department: territories_group_by_department,
territories_without_department: territories_without_department,
}
end

def domaines; end

Expand Down
9 changes: 3 additions & 6 deletions app/models/territory.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class Territory < ApplicationRecord
has_paper_trail

DEPARTEMENTS_NAMES = CSV.read(Rails.root.join("lib/assets/departements_fr.csv"), headers: :first_row)
.to_h { [_1["number"], _1["name"]] }.freeze

SPECIAL_NAMES = [
MAIRIES_NAME = "Mairies".freeze,
CNFS_NAME = "Conseillers Numériques".freeze,
Expand Down Expand Up @@ -65,9 +68,6 @@ class Territory < ApplicationRecord
where(id: Organisation.with_upcoming_rdvs.distinct.select(:territory_id))
}
scope :ordered_by_name, -> { order(Arel.sql("unaccent(LOWER(territories.name))")) }
scope :with_phone_number_formatted, -> { where.not(phone_number_formatted: nil) }
scope :with_valid_department, -> { where(departement_number: DEPARTEMENTS_NAMES.keys) }
scope :with_invalid_department, -> { where.not(departement_number: DEPARTEMENTS_NAMES.keys) }

## -

Expand Down Expand Up @@ -134,9 +134,6 @@ def department_name

private

DEPARTEMENTS_NAMES = CSV.read(Rails.root.join("lib/assets/departements_fr.csv"), headers: :first_row)
.to_h { [_1["number"], _1["name"]] }.freeze

def fill_name_for_departements
return if name.present? || departement_number.blank?

Expand Down
4 changes: 2 additions & 2 deletions app/views/static_pages/contact.html.slim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- content_for :title, "Contact"

- Territory.with_phone_number_formatted.with_valid_department.order(:departement_number, :name).group_by(&:departement_number).each do |department_number, territories|
- territories_group_by_department.each do |department_number, territories|
h2 #{department_number} - #{territories.first.department_name}
ul.list-group.mb-2
- territories.each do |territory|
Expand All @@ -10,7 +10,7 @@

h2 Autres contacts
ul.list-group.mb-2
- Territory.with_phone_number_formatted.with_invalid_department.order(:name).each do |territory|
- territories_without_department.each do |territory|
li
span> #{territory.name}
= link_to territory.phone_number, "tel:#{territory.phone_number_formatted}"
Expand Down

0 comments on commit 64877cb

Please sign in to comment.