Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ON HOLD] Feature | Geolocation back #295

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
87c6515
Change geolocation query at the begining of the search flow
AlanSoto31 Sep 14, 2022
695d52a
Add default distance for neoger query
AlanSoto31 Sep 22, 2022
4b50aae
Blocks added
daniel-enqz Oct 6, 2022
a62fbf8
Separations Added 'divs'
daniel-enqz Oct 6, 2022
8f03664
Removed required
daniel-enqz Oct 7, 2022
813c10f
Removed required
daniel-enqz Oct 7, 2022
070009c
Merge branch 'main' into feature/geolocation-back
AlanSoto31 Oct 7, 2022
fa5aaef
Improve some helpers
AlanSoto31 Oct 8, 2022
26aad77
Move input instructions
Oct 10, 2022
b4b729e
merge with main to be upto-date
Oct 10, 2022
eb1db67
required added back
daniel-enqz Oct 12, 2022
1a82c23
Merge pull request #319 from TelosLabs/feature/show-required-text
AlanSoto31 Oct 12, 2022
c4db7ce
Spaces and grid fixed
daniel-enqz Oct 12, 2022
0d2527a
Merge branch 'main' into feature/color-blocks-in-edit-nonprofit
daniel-enqz Oct 12, 2022
c2e6889
Merge pull request #316 from TelosLabs/feature/color-blocks-in-edit-n…
AlanSoto31 Oct 12, 2022
bde57f3
Merge with main to be upto-date
Oct 12, 2022
25dcced
Merge pull request #320 from TelosLabs/feature/add-profile-admin-fiel…
AlanSoto31 Oct 12, 2022
a67c834
Create random locations within 1000 Us cities
AlanSoto31 Oct 13, 2022
20d0036
Asign fake names, rnadom orgs and address to locations
AlanSoto31 Oct 13, 2022
ca43615
Create account undesired auto scroll behavior fixed.
Oct 13, 2022
915f20e
Merge pull request #325 from TelosLabs/hotfix/create-account-auto-scroll
AlanSoto31 Oct 14, 2022
a81ff38
Merge branch 'main' into feature/geolocation-back
AlanSoto31 Oct 20, 2022
fac0703
Create random location generator service
AlanSoto31 Oct 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/assets/images/heart_outlined.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions app/assets/images/like.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions app/assets/images/list.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions app/assets/images/office.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions app/assets/images/star.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions app/assets/images/user_group.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions app/assets/images/verify.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions app/helpers/locations_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module LocationsHelper
def display_day_working_hours(office_hour)
return "Closed" if office_hour.closed?
"#{office_hour.open_time.strftime("%l %p")}-#{office_hour.close_time.strftime("%l %p")} (CST)"
return "Closed" if office_hour&.closed?
"#{office_hour&.open_time&.strftime("%l %p")}-#{office_hour&.close_time&.strftime("%l %p")} (CST)"
end
end
2 changes: 1 addition & 1 deletion app/models/locations/officeable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def next_open_office_hours
end

def open_now?
today_office_hours.open_now?
today_office_hours&.open_now?
end

def open_same_day?(next_open)
Expand Down
13 changes: 9 additions & 4 deletions app/models/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ def execute_search
open_weekends: ActiveModel::Type::Boolean.new.cast(open_weekends),
beneficiary_groups: beneficiary_groups,
services: services,
causes: causes,
distance: distance.presence&.to_i,
lat: lat.presence&.to_f, lon: lon.presence&.to_f
causes: causes
}

@results = keyword.present? ? Locations::KeywordQuery.call({ keyword: keyword }) : Location.active
geo_filters = {
lat: lat.presence&.to_f,
lon: lon.presence&.to_f,
distance: distance.presence&.to_i
}

@results = Locations::GeolocationQuery.call(geo_filters)
@results = keyword.present? ? Locations::KeywordQuery.call({ keyword: keyword }, @results) : @results
@results = Locations::FilterQuery.call(filters, @results)
end
end
32 changes: 5 additions & 27 deletions app/queries/locations/filter_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,18 @@

module Locations
class FilterQuery
DEFAULT_LOCATION = {
latitude: 36.16404968727089,
longitude: -86.78125827725053
}.freeze

attr_reader :locations

class << self
def call(params = {}, locations = Location.active)
scope = locations
scope = geo_near(scope, starting_coordinates(params[:lat], params[:lon]), params[:distance])
scope = by_address(scope, params[:address])
#scope = by_address(scope, params[:address])
scope = by_cause(scope, params[:causes])
scope = by_service(scope, params[:services])
scope = by_beneficiary_groups_served(scope, params[:beneficiary_groups])
scope = opened_now(scope, params[:open_now])
scope = opened_on_weekends(scope, params[:open_weekends])
end

def geo_near(scope, coords, distance)
return scope if distance.blank? || distance.zero? || scope.empty?

scope.where(
'ST_DWithin(lonlat, :point, :distance)',
{ point: coords, distance: distance * 1000 } # wants meters not kms
)
opened_on_weekends(scope, params[:open_weekends])
end

def by_address(scope, address_params)
Expand Down Expand Up @@ -67,11 +53,11 @@ def by_service(scope, services)
complex_query = []
services.each do |cause, services_list|
services_list.each do |ser|
cause = cause&.gsub("'","''")
ser = ser&.gsub("'","''")
cause = cause&.gsub("'","''")
ser = ser&.gsub("'","''")
complex_query << "('#{cause}', '#{ser}')"
end
end
end

Location.joins(
location_services: { service: :cause }
Expand Down Expand Up @@ -102,14 +88,6 @@ def by_beneficiary_groups_served(scope, beneficiary_groups_filters)
).distinct
end

def starting_coordinates(lat, lon)
if lat.nil? || lon.nil?
Geo.to_wkt(Geo.point(DEFAULT_LOCATION[:longitude], DEFAULT_LOCATION[:latitude]))
else
Geo.to_wkt(Geo.point(lon, lat))
end
end

def parameterize_address_filters(address_params)
address_params.values.reject!(&:blank?).compact.map { |v| "%#{v}%" }
end
Expand Down
39 changes: 39 additions & 0 deletions app/queries/locations/geolocation_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module Locations
class GeolocationQuery
DEFAULT_LOCATION = {
latitude: 36.16404968727089,
longitude: -86.78125827725053
}.freeze
DEFAULT_DISTANCE = 20 # km

attr_reader :locations

class << self
def call(params = {}, locations = Location.active)
scope = locations
geo_near(scope, starting_coordinates(params[:lat], params[:lon]), params[:distance])
end

def geo_near(scope, coords, distance)
return scope if scope.empty?

distance = DEFAULT_DISTANCE if distance.blank? || distance.zero?

scope.where(
'ST_DWithin(lonlat, :point, :distance)',
{ point: coords, distance: distance * 1000 } # wants meters not kms
)
end

def starting_coordinates(lat, lon)
if lat.nil? || lon.nil?
Geo.to_wkt(Geo.point(DEFAULT_LOCATION[:longitude], DEFAULT_LOCATION[:latitude]))
else
Geo.to_wkt(Geo.point(lon, lat))
end
end
end
end
end
25 changes: 25 additions & 0 deletions app/services/random_coords.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class RandomCoords < ApplicationService
EARTH_RADIOUS = 6371 # km
ONE_DEGREE = EARTH_RADIOUS * 2 * Math::PI / 360 * 1000 # 1° latitude in meters

def initialize(central_lat:, central_lng:, max_radius:)
@central_lat = central_lat
@central_lng = central_lng
@max_radius = max_radius
end

def call
random_coords = {}

dx, dy = random_point_in_disk
random_coords[:lat] = @central_lat + (dy / ONE_DEGREE)
random_coords[:lng] = @central_lng + (dx / (ONE_DEGREE * Math::cos(@central_lat * Math::PI / 180)))
random_coords
end

def random_point_in_disk
r = @max_radius * (rand**0.5)
theta = rand * 2 * Math::PI
[r * Math.cos(theta), r * Math.sin(theta)]
end
end
16 changes: 8 additions & 8 deletions app/views/messages/new.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@
= "- #{message}"
div class="w-full"
= f.label :name, "*Name"
= f.text_field :name, class:"c-input", required: true
= f.text_field :name, class:"c-input"
div class="w-full"
= f.label :email, "*Email"
= f.email_field :email, class:"c-input", required: true
= f.email_field :email, class:"c-input"
div class="w-full"
= f.label :phone
= f.telephone_field :phone, class: "c-input"
div class="w-full"
= f.label :subject, "*Subject"
= f.select :subject,\
[['I want to publish a nonprofit on Giving Connection', '1'],['I want to claim ownership of a nonprofit page', '2'], ['Other', '3']],\
{}, {class: "c-input pr-7 bg-white", required: true, data: { action: "change->contact-form#subjectSelected",'contact-form-target': 'subject'}}
{}, {class: "c-input pr-7 bg-white", data: { action: "change->contact-form#subjectSelected",'contact-form-target': 'subject'}}
- if current_page?(non_profit_contact_path) || @form_to_render == "add_nonprofit"
= f.text_field :form_definition, value: "add_nonprofit", class:"hidden"
div class="w-full"
= f.label :organization_name, "*Organization Name"
= f.text_field :organization_name, class:"c-input", required: true
= f.text_field :organization_name, class:"c-input"
div class="w-full"
= f.label :organization_website, "Organization Website"
= f.text_field :organization_website, class:"c-input", placeholder:"Your Website"
Expand All @@ -65,15 +65,15 @@
= f.text_field :organization_ein, class:"c-input", placeholder:"Your Organization EIN"
div class="w-full"
= f.label :profile_admin_name, "*Profile Admin Name"
= f.text_field :profile_admin_name, class:"c-input", required: true
small class="inline-block mt-1.5 leading-normal"
p class="mt-1.5 text-xs leading-normal"
| Your profile admin is the person who will be updating the page information.
= f.text_field :profile_admin_name, class:"c-input", required: true
div class="w-full"
= f.label :profile_admin_email, "*Profile Admin Email"
= f.text_field :profile_admin_email, class:"c-input", required: true
= f.text_field :profile_admin_email, class:"c-input"
div class="w-full"
= f.label :content, "*Message"
= f.text_area :content, class:"c-input h-40", required: true, data: { 'contact-form-target': 'message'}
= f.text_area :content, class:"c-input h-40", data: { 'contact-form-target': 'message'}
div class="w-full"
= recaptcha_tags
div class="mt-3"
Expand Down
Loading