[Git][noosfero/noosfero][master] 2 commits: Changed method self.by_location to be according to Clean Code principles

Rodrigo Souto gitlab at mg.gitlab.com
Thu Oct 5 14:46:01 BRT 2017


Rodrigo Souto pushed to branch master at Noosfero / noosfero


Commits:
9fe4d071 by Euler at 2017-10-05T09:23:47-03:00
Changed method self.by_location to be according to Clean Code principles

- - - - -
152913d9 by Rodrigo Souto at 2017-10-05T17:45:47+00:00
Merge branch '1268-redo' into 'master'

Changed method self.by_location to be according to Clean Code principles

See merge request noosfero/noosfero!1323
- - - - -


1 changed file:

- app/models/profile.rb


Changes:

=====================================
app/models/profile.rb
=====================================
--- a/app/models/profile.rb
+++ b/app/models/profile.rb
@@ -137,7 +137,6 @@ class Profile < ApplicationRecord
 
   scope :recent, -> limit=nil { order('id DESC').limit(limit) }
 
-
   # Returns a scoped object to select profiles in a given location or in a radius
   # distance from the given location center.
   # The parameter can be the `request.params` with the keys:
@@ -150,33 +149,43 @@ class Profile < ApplicationRecord
   # NOTE: This method may return an exception object, to inform filter error.
   # When chaining scopes, is hardly recommended you to add this as the last one,
   # if you can't be sure about the provided parameters.
+
+  def self.distance_is_blank (params)
+    where_code = []
+    [ :city, :state, :country ].each do |place|
+      unless params[place].blank?
+       # ... So we must to find on this named location
+       # TODO: convert location attrs to a table collumn
+       where_code << "(profiles.data like '%#{place}: #{params[place]}%')"
+     end
+   end
+   self.where where_code.join(' AND ')
+  end
+
+  def self.filter_in_a_georef_circle (params)
+    unless params[:lat].blank? && params[:lng].blank?
+      lat, lng = [ params[:lat].to_f, params[:lng].to_f ]
+    end
+    if !lat
+      location = [ params[:city], params[:state], params[:country] ].compact.join(', ')
+      if location.blank?
+        return Exception.new (
+        _('You must to provide `lat` and `lng`, or `city` and `country` to define the center of the search circle, defined by `distance`.')
+        )
+      end
+      lat, lng = Noosfero::GeoRef.location_to_georef location
+    end
+    dist = params[:distance].to_f
+    self.where "#{Noosfero::GeoRef.sql_dist lat, lng} <= #{dist}"
+  end
+
   def self.by_location(params)
     params = params.with_indifferent_access
     if params[:distance].blank?
-      where_code = []
-      [ :city, :state, :country ].each do |place|
-        unless params[place].blank?
-          # ... So we must to find on this named location
-          # TODO: convert location attrs to a table collumn
-          where_code << "(profiles.data like '%#{place}: #{params[place]}%')"
-        end
-      end
-      self.where where_code.join(' AND ')
+      distance_is_blank params
     else # Filter in a georef circle
-      unless params[:lat].blank? && params[:lng].blank?
-        lat, lng = [ params[:lat].to_f, params[:lng].to_f ]
-      end
-      if !lat
-        location = [ params[:city], params[:state], params[:country] ].compact.join(', ')
-        if location.blank?
-          return Exception.new (
-            _('You must to provide `lat` and `lng`, or `city` and `country` to define the center of the search circle, defined by `distance`.')
-          )
-        end
-        lat, lng = Noosfero::GeoRef.location_to_georef location
-      end
-      dist = params[:distance].to_f
-      self.where "#{Noosfero::GeoRef.sql_dist lat, lng} <= #{dist}"
+      # location = Location.new(params[:lat], params[:lng], params[:city], params[:state], params[:country], params[:distance])
+      self.filter_in_a_georef_circle(params)
     end
   end
 



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/ac1f62710b077276279b6c5dcdf10a21b857814a...152913d97fb741f9635a1aa9456c7206fdbd6ddc

---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/ac1f62710b077276279b6c5dcdf10a21b857814a...152913d97fb741f9635a1aa9456c7206fdbd6ddc
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20171005/510ce1fa/attachment-0001.html>


More information about the Noosfero-dev mailing list