Peripatetic
Drop-in polymorphic location management for Rails models with geocoding support.
Requirements
- Rails 8.0+
- Ruby 3.1+
- Geocoder gem
Installation
Add to your Gemfile:
gem 'peripatetic', github: 'davingee/Peripatetic'Then run:
bundle install
rails generate peripatetic:install
rails db:migrateUsage
Include Peripatetic in any model:
class User < ApplicationRecord
include Peripatetic
endThis adds a polymorphic has_many :locations with nested attribute support.
Forms
<%= form_with model: @user do |f| %>
<%= f.fields_for :locations do |builder| %>
<%= builder.text_field :street %>
<%= builder.text_field :accessor_postal_code %>
<%= builder.select :accessor_country, all_countries.map { |c| [c.name, c.name] } %>
<%= builder.check_box :_destroy %> Remove
<% end %>
<%= f.submit %>
<% end %>Strong Parameters
locations_attributes: [:id, :street, :accessor_postal_code, :accessor_country, :_destroy]Accessing Locations
user.locations.each do |location|
puts location.full_address
puts "#{location.latitude}, #{location.longitude}"
endHow It Works
When a location is saved:
- Postal code is validated against the
peripatetic_postal_codestable - City, region, country code, and time zone are filled in from the postal code record
- If a street address is present, the geocoder refines the coordinates
Development
bundle install
bundle exec rspecContributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes
- Push to the branch and open a pull request
License
MIT