Adding gmaps
This commit is contained in:
4
app/assets/stylesheets/spaces.css
Normal file
4
app/assets/stylesheets/spaces.css
Normal file
@@ -0,0 +1,4 @@
|
||||
.marker-container h3 {
|
||||
display: inline-block;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
// Place all the styles related to the Spaces controller here.
|
||||
// They will automatically be included in application.css.
|
||||
// You can use Sass (SCSS) here: http://sass-lang.com/
|
||||
@@ -5,6 +5,35 @@ class SpacesController < ApplicationController
|
||||
# GET /spaces.json
|
||||
def index
|
||||
@spaces = Space.all
|
||||
|
||||
# Index, downcase, and capitalize the categories
|
||||
dot_index = 0
|
||||
@spaces.group_by{|s| s.category.downcase}.each do |group|
|
||||
dot_color = Space.dot_colors[dot_index]
|
||||
group.last.each do |space|
|
||||
space.category = group.first.capitalize
|
||||
space.dot_color = dot_color
|
||||
end
|
||||
dot_index += 1
|
||||
end
|
||||
|
||||
# Don't bother with generating markers for JSON
|
||||
respond_to do |format|
|
||||
format.html {
|
||||
|
||||
@json = @spaces.to_gmaps4rails do |space, marker|
|
||||
marker.infowindow render_to_string(:partial => "/spaces/marker", :locals => { :space => space})
|
||||
marker.picture({
|
||||
:picture => "http://maps.google.com/intl/en_us/mapfiles/ms/micons/#{space.dot_color}-dot.png",
|
||||
:width => 32,
|
||||
:height => 32
|
||||
})
|
||||
marker.title space.name
|
||||
marker.json({ :id => space.id })
|
||||
end
|
||||
}
|
||||
format.json #render json
|
||||
end
|
||||
end
|
||||
|
||||
# GET /spaces/1
|
||||
@@ -69,6 +98,6 @@ class SpacesController < ApplicationController
|
||||
|
||||
# Never trust parameters from the scary internet, only allow the white list through.
|
||||
def space_params
|
||||
params.require(:space).permit(:name, :type, :address, :hours, :phone, :email, :website, :description)
|
||||
params.require(:space).permit(:name, :category, :address, :city, :state, :hours, :phone, :email, :website, :description)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +1,41 @@
|
||||
class Space < ActiveRecord::Base
|
||||
has_paper_trail
|
||||
acts_as_gmappable
|
||||
|
||||
before_update :update_lat_lng
|
||||
|
||||
attr_accessor :dot_color
|
||||
|
||||
def self.categories
|
||||
# Downcased and capitalized for great justice
|
||||
Space.all.select(:category).map{|s| s.category.downcase.capitalize }.uniq
|
||||
end
|
||||
|
||||
def self.dot_colors
|
||||
["red","blue","green","yellow","purple","orange"]
|
||||
end
|
||||
|
||||
def website_with_protocol
|
||||
/^http/.match(self.website) ? self.website : "http://#{self.website}"
|
||||
end
|
||||
|
||||
def full_address
|
||||
"#{self.address}, #{self.city} #{self.state}"
|
||||
end
|
||||
|
||||
def gmaps4rails_address
|
||||
#describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
|
||||
self.full_address
|
||||
end
|
||||
|
||||
def update_lat_lng
|
||||
coords = self.geocode.first
|
||||
Rails.logger.info coords.inspect
|
||||
self.latitude = coords[:lat]
|
||||
self.longitude = coords[:lng]
|
||||
end
|
||||
|
||||
def geocode
|
||||
Gmaps4rails.geocode(self.full_address)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -17,12 +17,20 @@
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= f.label :category %><br>
|
||||
<%= f.text_field :category %>
|
||||
<%= f.text_field :category %> (suggested: <%= Space.categories.join(", ") %>)
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= f.label :address %><br>
|
||||
<%= f.text_field :address %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= f.label :city %><br>
|
||||
<%= f.text_field :city %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= f.label :state %><br>
|
||||
<%= f.text_field :state %>
|
||||
</div>
|
||||
<div class="field">
|
||||
<%= f.label :hours %><br>
|
||||
<%= f.text_field :hours %>
|
||||
|
||||
20
app/views/spaces/_marker.html.erb
Normal file
20
app/views/spaces/_marker.html.erb
Normal file
@@ -0,0 +1,20 @@
|
||||
<div class="marker-container">
|
||||
<h3><%= space.name %></h3>
|
||||
<em><%= space.category %></em>
|
||||
|
||||
<p>
|
||||
<%= space.full_address %><br/>
|
||||
|
||||
<strong>Hours:</strong> <%= space.hours %><br/>
|
||||
|
||||
<strong>Phone:</strong> <%= space.phone %><br/>
|
||||
|
||||
<strong>Email:</strong> <%= space.email %><br/>
|
||||
|
||||
<strong>Website:</strong> <%= link_to space.website, space.website_with_protocol %><br/>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<%= space.description %>
|
||||
</p>
|
||||
</div>
|
||||
@@ -11,7 +11,12 @@
|
||||
|
||||
<p>
|
||||
<strong>Address:</strong>
|
||||
<%= space.address %>
|
||||
<%= space.full_address %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Latitude/Longitude:</strong>
|
||||
<%= space.latitude %>, <%= space.longitude %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
|
||||
@@ -1,10 +1,21 @@
|
||||
<h1>Listing spaces</h1>
|
||||
<%= stylesheet_link_tag 'gmaps4rails' %>
|
||||
|
||||
<h1>Collaborative Spaces in Arizona</h1>
|
||||
<p>
|
||||
<%= link_to 'Add Space', new_space_path %> |
|
||||
<%= link_to 'JSON Feed', spaces_path("json") %>
|
||||
</p>
|
||||
|
||||
<%= gmaps4rails(@json) %>
|
||||
|
||||
<%= yield :scripts %>
|
||||
|
||||
<br/>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Category</th>
|
||||
<th>Address</th>
|
||||
<th>Hours</th>
|
||||
<th>Phone</th>
|
||||
@@ -18,24 +29,24 @@
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<% @spaces.each do |space| %>
|
||||
<tr>
|
||||
<td><%= space.name %></td>
|
||||
<td><%= space.category %></td>
|
||||
<td><%= space.address %></td>
|
||||
<td><%= space.hours %></td>
|
||||
<td><%= space.phone %></td>
|
||||
<td><%= space.email %></td>
|
||||
<td><%= space.website %></td>
|
||||
<td><%= space.description %></td>
|
||||
<td><%= link_to 'Show', space %></td>
|
||||
<td><%= link_to 'Edit', edit_space_path(space) %></td>
|
||||
<td><%= link_to 'Destroy', space, method: :delete, data: { confirm: 'Are you sure?' } %></td>
|
||||
</tr>
|
||||
<% @spaces.group_by{|s| s.category}.each do |category, spaces| %>
|
||||
<tr><th colspan="7" style="text-align: left;">
|
||||
<span style="color: <%= spaces.first.dot_color %>">•</span>
|
||||
<%= category.titleize.pluralize %></th></tr>
|
||||
<% spaces.each do |space| %>
|
||||
<tr>
|
||||
<td><%= space.name %></td>
|
||||
<td><%= space.full_address %></td>
|
||||
<td><%= space.hours %></td>
|
||||
<td><%= space.phone %></td>
|
||||
<td><%= space.email %></td>
|
||||
<td><%= space.website %></td>
|
||||
<td><%= space.description %></td>
|
||||
<td><%= link_to 'Show', space %></td>
|
||||
<td><%= link_to 'Edit', edit_space_path(space) %></td>
|
||||
<td><%= link_to 'Destroy', space, method: :delete, data: { confirm: 'Are you sure?' } %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<br>
|
||||
|
||||
<%= link_to 'New Space', new_space_path %>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
json.array!(@spaces) do |space|
|
||||
json.extract! space, :name, :type, :address, :hours, :phone, :email, :website, :description
|
||||
json.extract! space, :name, :category, :address, :city, :state, :hours, :phone, :email, :website, :description, :dot_color
|
||||
json.url space_url(space, format: :json)
|
||||
end
|
||||
|
||||
@@ -2,12 +2,11 @@
|
||||
|
||||
<%= render partial: "space", locals: {space: @space} %>
|
||||
|
||||
<%= link_to 'Edit', edit_space_path(@space) %> |
|
||||
<%= link_to 'Back', spaces_path %>
|
||||
<%= link_to 'Edit', edit_space_path(@space) %>
|
||||
|
||||
<h3>Revision history</h3>
|
||||
<dl>
|
||||
<% @space.versions.each do |v| %>
|
||||
<% @space.versions.reverse!.each do |v| %>
|
||||
<dt><b><%= v.event.capitalize %> <%= v.created_at %> <%= v.whodunnit if v.whodunnit %></b></dt>
|
||||
<dd>
|
||||
<% if v.reify %>
|
||||
|
||||
@@ -1 +1 @@
|
||||
json.extract! @space, :name, :type, :address, :hours, :phone, :email, :website, :description, :created_at, :updated_at
|
||||
json.extract! @space, :name, :category, :address, :city, :state, :hours, :phone, :email, :website, :description, :created_at, :updated_at
|
||||
|
||||
Reference in New Issue
Block a user