From 033c032ed6ffbdd95d0cea1ace5c736d51934013 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Wed, 9 Aug 2023 20:15:28 +0300 Subject: [PATCH 1/1] Add home location condition to user model --- app/controllers/diary_entries_controller.rb | 2 +- app/helpers/application_helper.rb | 2 +- app/models/user.rb | 8 ++++++-- app/views/api/users/_user.json.jbuilder | 2 +- app/views/api/users/_user.xml.builder | 2 +- app/views/dashboards/_contact.html.erb | 2 +- app/views/dashboards/show.html.erb | 2 +- app/views/layouts/map.html.erb | 2 +- app/views/profiles/edit.html.erb | 4 ++-- 9 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/controllers/diary_entries_controller.rb b/app/controllers/diary_entries_controller.rb index ea9aacb21..018343d7c 100644 --- a/app/controllers/diary_entries_controller.rb +++ b/app/controllers/diary_entries_controller.rb @@ -280,7 +280,7 @@ class DiaryEntriesController < ApplicationController @lon = @diary_entry.longitude @lat = @diary_entry.latitude @zoom = 12 - elsif current_user.home_lat.nil? || current_user.home_lon.nil? + elsif !current_user.has_home? @lon = params[:lon] || -0.1 @lat = params[:lat] || 51.5 @zoom = params[:zoom] || 4 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index bb09f3a49..4b3a22cd3 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -54,7 +54,7 @@ module ApplicationHelper if current_user data[:user] = current_user.id.to_json - data[:user_home] = { :lat => current_user.home_lat, :lon => current_user.home_lon } unless current_user.home_lon.nil? || current_user.home_lat.nil? + data[:user_home] = { :lat => current_user.home_lat, :lon => current_user.home_lon } if current_user.has_home? end data[:location] = session[:location] if session[:location] diff --git a/app/models/user.rb b/app/models/user.rb index c809b6192..41f249d4e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -238,8 +238,12 @@ class User < ApplicationRecord @preferred_languages ||= Locale.list(languages) end + def has_home? + home_lat && home_lon + end + def nearby(radius = Settings.nearby_radius, num = Settings.nearby_users) - if home_lon && home_lat + if has_home? gc = OSM::GreatCircle.new(home_lat, home_lon) sql_for_area = QuadTile.sql_for_area(gc.bounds(radius), "home_") sql_for_distance = gc.sql_for_distance("home_lat", "home_lon") @@ -401,6 +405,6 @@ class User < ApplicationRecord end def update_tile - self.home_tile = QuadTile.tile_for_point(home_lat, home_lon) if home_lat && home_lon + self.home_tile = QuadTile.tile_for_point(home_lat, home_lon) if has_home? end end diff --git a/app/views/api/users/_user.json.jbuilder b/app/views/api/users/_user.json.jbuilder index 15f0685ac..b84916010 100644 --- a/app/views/api/users/_user.json.jbuilder +++ b/app/views/api/users/_user.json.jbuilder @@ -46,7 +46,7 @@ json.user do end if current_user && current_user == user && can?(:details, User) - if user.home_lat && user.home_lon + if user.has_home? json.home do json.lat user.home_lat json.lon user.home_lon diff --git a/app/views/api/users/_user.xml.builder b/app/views/api/users/_user.xml.builder index 1791c60ef..23ec1d34d 100644 --- a/app/views/api/users/_user.xml.builder +++ b/app/views/api/users/_user.xml.builder @@ -25,7 +25,7 @@ xml.tag! "user", :id => user.id, end end if current_user && current_user == user && can?(:details, User) - if user.home_lat && user.home_lon + if user.has_home? xml.tag! "home", :lat => user.home_lat, :lon => user.home_lon, :zoom => user.home_zoom diff --git a/app/views/dashboards/_contact.html.erb b/app/views/dashboards/_contact.html.erb index 7785c0552..aba6094b2 100644 --- a/app/views/dashboards/_contact.html.erb +++ b/app/views/dashboards/_contact.html.erb @@ -11,7 +11,7 @@

<%= link_to contact.display_name, user_path(contact) %> - <% if @user.home_lon and @user.home_lat and contact.home_lon and contact.home_lat %> + <% if @user.has_home? and contact.has_home? %> <% distance = @user.distance(contact) %> <% if distance < 1 %> (<%= t ".m away", :count => (distance * 1000).round %>) diff --git a/app/views/dashboards/show.html.erb b/app/views/dashboards/show.html.erb index d0344ce79..eb3ceef6c 100644 --- a/app/views/dashboards/show.html.erb +++ b/app/views/dashboards/show.html.erb @@ -5,7 +5,7 @@

<% if current_user and @user.id == current_user.id %>
- <% if @user.home_lat.nil? or @user.home_lon.nil? %> + <% if !@user.has_home? %>

<%= t(".no_home_location_html", :edit_profile_link => link_to(t(".edit_your_profile"), edit_profile_path)) %>

diff --git a/app/views/layouts/map.html.erb b/app/views/layouts/map.html.erb index 6d983d30e..40f06a899 100644 --- a/app/views/layouts/map.html.erb +++ b/app/views/layouts/map.html.erb @@ -4,7 +4,7 @@ <% content_for(:body_class) { "map-layout" } %> -<% if current_user and !current_user.home_lon.nil? and !current_user.home_lat.nil? %> +<% if current_user&.has_home? %> <% content_for :greeting do %> <%= link_to t("layouts.home"), "#", diff --git a/app/views/profiles/edit.html.erb b/app/views/profiles/edit.html.erb index 5bf7426f2..a50d4b5db 100644 --- a/app/views/profiles/edit.html.erb +++ b/app/views/profiles/edit.html.erb @@ -42,13 +42,13 @@
<%= t ".home location" -%> -

hidden<% end %>><%= t ".no home location" %>

+

hidden<% end %>><%= t ".no home location" %>

<%= f.text_field :home_lat, :wrapper_class => "col-sm-4", :id => "home_lat" %> <%= f.text_field :home_lon, :wrapper_class => "col-sm-4", :id => "home_lon" %>
- checked="checked" <% end %> id="updatehome" /> + checked="checked" <% end %> id="updatehome" />
<%= tag.div "", :id => "map", :class => "content_map set_location border border-grey rounded" %> -- 2.39.5