]> git.openstreetmap.org Git - rails.git/commitdiff
Add home location condition to user model
authorAnton Khorev <tony29@yandex.ru>
Wed, 9 Aug 2023 17:15:28 +0000 (20:15 +0300)
committerAnton Khorev <tony29@yandex.ru>
Fri, 11 Aug 2023 18:44:15 +0000 (21:44 +0300)
app/controllers/diary_entries_controller.rb
app/helpers/application_helper.rb
app/models/user.rb
app/views/api/users/_user.json.jbuilder
app/views/api/users/_user.xml.builder
app/views/dashboards/_contact.html.erb
app/views/dashboards/show.html.erb
app/views/layouts/map.html.erb
app/views/profiles/edit.html.erb

index ea9aacb21d97f5e72b18e95fc3e28635cf89d903..018343d7c457dbc3d0565e106f9e6008a78fd0c9 100644 (file)
@@ -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
index bb09f3a49953bd66ec26fcb8734befb800c767e1..4b3a22cd3955c4984011fab89dc2b9f937f49be9 100644 (file)
@@ -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]
index c809b619294732038c97558423b349fe2050212c..41f249d4e484b628ca236e7e01af2be5a4cb386e 100644 (file)
@@ -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
index 15f0685ac9e24b9131b75ab2389a73c04157e008..b8491601055207ac3609b6dcf3ba9c120b27d5f5 100644 (file)
@@ -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
index 1791c60ef96fb0cf78d876106ed07c2b836b7135..23ec1d34d6c0af80fc9b6899b84f3aed8b731973 100644 (file)
@@ -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
index 7785c05523fb287cc3a239b430b65392b3c15953..aba6094b2c9a159815917d87c929a983160d48f0 100644 (file)
@@ -11,7 +11,7 @@
   <div class="col">
     <p class='text-muted mb-0'>
       <%= 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 %>)
index d0344ce79bd61a7c773994328dcc915bf3cc8656..eb3ceef6c528e35174acaf7c3053694f6a2741df 100644 (file)
@@ -5,7 +5,7 @@
 <div class="row">
   <% if current_user and @user.id == current_user.id %>
     <div class="col-md order-md-last">
-      <% if @user.home_lat.nil? or @user.home_lon.nil? %>
+      <% if !@user.has_home? %>
         <div id="map" class="content_map border border-grey">
           <p class="m-3"><%= t(".no_home_location_html", :edit_profile_link => link_to(t(".edit_your_profile"), edit_profile_path)) %></p>
         </div>
index 6d983d30ea153454ed225068d3baa97f79cd5c66..40f06a8990f41878c51033a95c35b277525cc5d1 100644 (file)
@@ -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"),
                 "#",
index 5bf7426f2946c74a1ecc3a770b5a4363954eb13f..a50d4b5db027f64334347c04203ae5ad863c8163 100644 (file)
 
   <fieldset>
     <legend><%= t ".home location" -%></legend>
-    <p id="home_message" class="text-muted"<% if current_user.home_lat and current_user.home_lon %> hidden<% end %>><%= t ".no home location" %></p>
+    <p id="home_message" class="text-muted"<% if current_user.has_home? %> hidden<% end %>><%= t ".no home location" %></p>
     <div class="row">
       <%= 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" %>
     </div>
     <div class="form-check">
-      <input class="form-check-input" type="checkbox" name="updatehome" value="1" <% unless current_user.home_lat and current_user.home_lon %> checked="checked" <% end %> id="updatehome" />
+      <input class="form-check-input" type="checkbox" name="updatehome" value="1" <% unless current_user.has_home? %> checked="checked" <% end %> id="updatehome" />
       <label class="form-check-label" for="updatehome"><%= t ".update home location on click" %></label>
     </div>
     <%= tag.div "", :id => "map", :class => "content_map set_location border border-grey rounded" %>