Skip to content

Commit

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

* Refactor thanks to review

* Update version
  • Loading branch information
Aitor Lopez Beltran authored and agustibr committed Jan 8, 2019
1 parent 6aa873f commit b2fdb09
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
27 changes: 0 additions & 27 deletions app/commands/decidim/sabarca/create_registration.rb

This file was deleted.

16 changes: 15 additions & 1 deletion app/forms/decidim/user_group_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class UserGroupForm < Form
validates :nickname, presence: true
validates :document_number, presence: true
validates :phone, presence: true
validates :url, presence: true
validates :scope_id, presence: true
validates :address, presence: true

Expand All @@ -32,6 +31,7 @@ class UserGroupForm < Form
validate :unique_email
validate :unique_name
validate :unique_nickname
validate :valid_url

private

Expand Down Expand Up @@ -83,5 +83,19 @@ def unique_nickname
errors.add :nickname, :taken
false
end

def valid_url
return unless url
errors.add(:url, :invalid) unless uri?(url)
end

def uri?(string)
uri = URI.parse(string)
%w(http https).include?(uri.scheme)
rescue URI::BadURIError
false
rescue URI::InvalidURIError
false
end
end
end
18 changes: 15 additions & 3 deletions app/models/decidim/user_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class UserGroup < UserBaseEntity
validate :correct_state
validate :unique_document_number

geocoded_by :address

after_validation :geocode
after_validation :geocode!

scope :verified, -> { where.not("extended_data->>'verified_at' IS ?", nil) }
scope :rejected, -> { where.not("extended_data->>'rejected_at' IS ?", nil) }
Expand Down Expand Up @@ -106,6 +104,20 @@ 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 app/views/decidim/sabarca/shared/_user_group.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<br/>
<%= user_group.phone %>
</br>
<%= user_group.users.first.email %>
<%= user_group.email %>
</div>
</div>
</div>
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.1"
VERSION = "0.11.2"
end
end

0 comments on commit b2fdb09

Please sign in to comment.