Skip to content

Commit

Permalink
Fix geocode v2 (#15)
Browse files Browse the repository at this point in the history
* Fix geocode v2

* Refactor thanks to review
  • Loading branch information
Aitor Lopez Beltran authored and agustibr committed Jan 11, 2019
1 parent b2fdb09 commit 6c25ca1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
21 changes: 20 additions & 1 deletion app/commands/decidim/create_user_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def create_user_group
document_number: form.document_number,
scope_id: form.scope_id,
address: form.address,
url: form.url
url: form.url,
latitude: latitude,
longitude: longitude
}
)
end
Expand All @@ -56,5 +58,22 @@ def create_membership
user_group: @user_group
)
end

def latitude
geocode_address["Latitude"]
end

def longitude
geocode_address["Longitude"]
end

def geocode_address
return @geocode_address if defined?(@geocode_address)

geocode = Geocoder.search(form.address).first
return @geocode_address = {} unless geocode

@geocode_address = geocode.data['Location']["DisplayPosition"]
end
end
end
2 changes: 1 addition & 1 deletion app/helpers/decidim/sabarca/scopes_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def user_groups_data_for_map(geocoded_user_groups)
geocoded_user_groups.map do |user_group|
user_group.slice(:latitude, :longitude, :address).merge(title: user_group.name,
icon: icon("proposals", width: 40, height: 70, remove_icon_class: true),
email: user_group.users.first.email,
email: user_group.email,
address: user_group.address,
location: "",
phone: user_group.phone,
Expand Down
20 changes: 4 additions & 16 deletions app/models/decidim/user_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class UserGroup < UserBaseEntity
validate :correct_state
validate :unique_document_number

after_validation :geocode!

scope :verified, -> { where.not("extended_data->>'verified_at' IS ?", nil) }
scope :rejected, -> { where.not("extended_data->>'rejected_at' IS ?", nil) }
scope :pending, -> { where("extended_data->>'rejected_at' IS ? AND extended_data->>'verified_at' IS ?", nil, nil) }
Expand Down Expand Up @@ -54,6 +52,10 @@ def self.export_serializer
Decidim::DataPortabilitySerializers::DataPortabilityUserGroupSerializer
end

def geocoded?
latitude && longitude
end

def latitude
extended_data["latitude"]
end
Expand Down Expand Up @@ -104,20 +106,6 @@ def verify!

private

# To automatically geocode objects with Geocoder
# they have to have two attributes/columns (float or decimal)
# called latitude and longitude (that we don't have)
# Here we search for the address without using ActiveRecord
def geocode!
result = Geocoder.search(address).first
return unless result

coordinates = result.data['Location']['DisplayPosition']
extended_data['latitude'] = coordinates["Latitude"]
extended_data['longitude'] = coordinates["Longitude"]
save!
end

# Private: Checks if the state user group is correct.
def correct_state
errors.add(:base, :invalid) if verified? && rejected?
Expand Down
2 changes: 1 addition & 1 deletion lib/decidim/sabarca/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Decidim
module Sabarca
VERSION = "0.11.2"
VERSION = "0.11.3"
end
end

0 comments on commit 6c25ca1

Please sign in to comment.