Permalink
Browse files

added ability save and show point on the google map

  • Loading branch information...
1 parent e1bfeae commit 52e42895bbc9130293a64564db42b2adbe140f85 @munya committed Aug 16, 2012
View
2 Gemfile
@@ -22,6 +22,8 @@ group :assets do
end
gem 'jquery-rails'
+gem 'rgeo'
+gem 'activerecord-postgis-adapter'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
View
10 Gemfile.lock
@@ -22,6 +22,9 @@ GEM
activesupport (= 3.2.3)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
+ activerecord-postgis-adapter (0.4.3)
+ pg (>= 0.11.0)
+ rgeo-activerecord (~> 0.4.5)
activeresource (3.2.3)
activemodel (= 3.2.3)
activesupport (= 3.2.3)
@@ -80,6 +83,11 @@ GEM
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
+ rgeo (0.3.15)
+ rgeo-activerecord (0.4.5)
+ activerecord (>= 3.0.3)
+ arel (>= 2.0.6)
+ rgeo (>= 0.3.10)
sass (3.1.20)
sass-rails (3.2.5)
railties (~> 3.2.0)
@@ -103,10 +111,12 @@ PLATFORMS
ruby
DEPENDENCIES
+ activerecord-postgis-adapter
coffee-rails (~> 3.2.1)
jquery-rails
json
pg
rails (= 3.2.3)
+ rgeo
sass-rails (~> 3.2.3)
uglifier (>= 1.0.3)
View
14 app/assets/javascripts/application.js
@@ -13,3 +13,17 @@
//= require jquery
//= require jquery_ujs
//= require_tree .
+
+
+function findMe() {
+ if(navigator.geolocation) {
+ navigator.geolocation.getCurrentPosition(function(position) {
+ document.getElementById('checkin_latitude').value = position.coords.latitude;
+ document.getElementById('checkin_longitude').value = position.coords.longitude;
+ }, function() {
+ alert('We couldn\'t find your position.');
+ });
+ } else {
+ alert('Your browser doesn\'t support geolocation.');
+ }
+}
View
3 app/assets/javascripts/checkins.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
3 app/assets/stylesheets/checkins.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the checkins controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
56 app/assets/stylesheets/scaffolds.css.scss
@@ -0,0 +1,56 @@
+body {
+ background-color: #fff;
+ color: #333;
+ font-family: verdana, arial, helvetica, sans-serif;
+ font-size: 13px;
+ line-height: 18px; }
+
+p, ol, ul, td {
+ font-family: verdana, arial, helvetica, sans-serif;
+ font-size: 13px;
+ line-height: 18px; }
+
+pre {
+ background-color: #eee;
+ padding: 10px;
+ font-size: 11px; }
+
+a {
+ color: #000;
+ &:visited {
+ color: #666; }
+ &:hover {
+ color: #fff;
+ background-color: #000; } }
+
+div {
+ &.field, &.actions {
+ margin-bottom: 10px; } }
+
+#notice {
+ color: green; }
+
+.field_with_errors {
+ padding: 2px;
+ background-color: red;
+ display: table; }
+
+#error_explanation {
+ width: 450px;
+ border: 2px solid red;
+ padding: 7px;
+ padding-bottom: 0;
+ margin-bottom: 20px;
+ background-color: #f0f0f0;
+ h2 {
+ text-align: left;
+ font-weight: bold;
+ padding: 5px 5px 5px 15px;
+ font-size: 12px;
+ margin: -7px;
+ margin-bottom: 0px;
+ background-color: #c00;
+ color: #fff; }
+ ul li {
+ font-size: 12px;
+ list-style: square; } }
View
90 app/controllers/checkins_controller.rb
@@ -0,0 +1,90 @@
+class CheckinsController < ApplicationController
+ # GET /checkins
+ # GET /checkins.json
+ def index
+ @checkins = Checkin.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @checkins }
+ end
+ end
+
+ # GET /checkins/1
+ # GET /checkins/1.json
+ def show
+ @checkin = Checkin.find(params[:id])
+
+ @nearby_checkins = Checkin.nearby_to(@checkin, 1000)
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @checkin }
+ end
+ end
+
+ # GET /checkins/new
+ # GET /checkins/new.json
+ def new
+ @checkin = Checkin.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @checkin }
+ end
+ end
+
+ # GET /checkins/1/edit
+ def edit
+ @checkin = Checkin.find(params[:id])
+ end
+
+ # POST /checkins
+ # POST /checkins.json
+ def create
+ @checkin = Checkin.new(params[:checkin])
+
+ if @checkin.valid?
+ puts '---controller'
+ puts @checkin.inspect
+ end
+
+ respond_to do |format|
+ if @checkin.save
+ format.html { redirect_to @checkin, notice: 'Checkin was successfully created.' }
+ format.json { render json: @checkin, status: :created, location: @checkin }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @checkin.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /checkins/1
+ # PUT /checkins/1.json
+ def update
+ @checkin = Checkin.find(params[:id])
+
+ respond_to do |format|
+ if @checkin.update_attributes(params[:checkin])
+ format.html { redirect_to @checkin, notice: 'Checkin was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @checkin.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /checkins/1
+ # DELETE /checkins/1.json
+ def destroy
+ @checkin = Checkin.find(params[:id])
+ @checkin.destroy
+
+ respond_to do |format|
+ format.html { redirect_to checkins_url }
+ format.json { head :no_content }
+ end
+ end
+end
View
2 app/helpers/checkins_helper.rb
@@ -0,0 +1,2 @@
+module CheckinsHelper
+end
View
24 app/models/checkin.rb
@@ -0,0 +1,24 @@
+class Checkin < ActiveRecord::Base
+ attr_accessible :location, :title, :latitude, :longitude
+
+ scope :nearby_to,
+ lambda { |checkin, max_distance|
+ where("ST_DWithin(location, ?, ?) AND id != ?", checkin.location, max_distance, checkin.id)
+ }
+
+ def latitude
+ self.location.nil? ? 0 : self.location.y
+ end
+
+ def latitude=(value)
+ self.location = "POINT(#{longitude} #{value})"
+ end
+
+ def longitude
+ self.location.nil? ? 0 : self.location.x
+ end
+
+ def longitude=(value)
+ self.location = "POINT(#{value} #{latitude})"
+ end
+end
View
35 app/views/checkins/_form.html.erb
@@ -0,0 +1,35 @@
+<%= form_for(@checkin) do |f| %>
+ <% if @checkin.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@checkin.errors.count, "error") %> prohibited this checkin from being saved:</h2>
+
+ <ul>
+ <% @checkin.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :title %>
+ <br/>
+ <%= f.text_field :title %>
+ </div>
+ <div class="field">
+ <%= f.label :latitude %>
+ <br/>
+ <%= f.text_field :latitude %>
+ </div>
+ <div class="field">
+ <%= f.label :longitude %>
+ <br/>
+ <%= f.text_field :longitude %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
+
+
+<input type="button" value="Find me!" onclick="findMe();" />
View
6 app/views/checkins/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing checkin</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @checkin %> |
+<%= link_to 'Back', checkins_path %>
View
62 app/views/checkins/index.html.erb
@@ -0,0 +1,62 @@
+<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
+
+<div id="map" style="width: 600px; height: 500px;"></div>
+
+<h1>Listing checkins</h1>
+
+<table>
+ <tr>
+ <th>Title</th>
+ <th>Location</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @checkins.each do |checkin| %>
+ <tr>
+ <td><%= checkin.title %></td>
+ <td><%= checkin.location %></td>
+ <td><%= link_to 'Show', checkin %></td>
+ <td><%= link_to 'Edit', edit_checkin_path(checkin) %></td>
+ <td><%= link_to 'Destroy', checkin, confirm: 'Are you sure?', method: :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Checkin', new_checkin_path %>
+
+<script type="text/javascript">
+ // Create the map
+ var map = new google.maps.Map(document.getElementById("map"), {
+ mapTypeId: google.maps.MapTypeId.ROADMAP
+ });
+
+ // Initialize the bounds container
+ var bounds = new google.maps.LatLngBounds();
+
+ <% @checkins.each do |checkin| %>
+ // Create the LatLng
+ var latLng = new google.maps.LatLng(<%= checkin.latitude %>, <%= checkin.longitude %>);
+
+ // Create the marker
+ var marker = new google.maps.Marker({
+ position: latLng,
+ map: map,
+ title: '<%= escape_javascript(checkin.title) %>'
+ });
+
+ // Add click event
+ google.maps.event.addListener(marker, 'click', function() {
+ document.location = '<%= checkin_path(checkin) %>';
+ });
+
+ // Extend the bounds
+ bounds.extend(latLng);
+ <% end %>
+
+ // Fit to bounds
+ map.fitBounds(bounds);
+</script>
View
5 app/views/checkins/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New checkin</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', checkins_path %>
View
22 app/views/checkins/show.html.erb
@@ -0,0 +1,22 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Title:</b>
+ <%= @checkin.title %>
+</p>
+
+<p>
+ <b>Location:</b>
+ <%= @checkin.location %>
+</p>
+
+
+<%= link_to 'Edit', edit_checkin_path(@checkin) %> |
+<%= link_to 'Back', checkins_path %>
+
+<h2>Nearby check-ins</h2>
+<ul>
+ <% @nearby_checkins.each do |checkin| %>
+ <li><%= link_to checkin.title, checkin %></li>
+ <% end %>
+</ul>
View
2 config/application.rb
@@ -1,6 +1,8 @@
require File.expand_path('../boot', __FILE__)
require 'rails/all'
+#require 'spatial_adapter/postgresql'
+require 'active_record/connection_adapters/postgis_adapter/railtie'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
View
2 config/routes.rb
@@ -1,4 +1,6 @@
PartyEvent::Application.routes.draw do
+ resources :checkins
+
# The priority is based upon order of creation:
# first created -> highest priority.
View
15 db/migrate/20120816081310_create_checkins.rb
@@ -0,0 +1,15 @@
+class CreateCheckins < ActiveRecord::Migration
+ def up
+ create_table :checkins do |t|
+ t.string :title
+ t.point :location, :geographic => true
+ end
+ change_table :checkins do |t|
+ t.index :location, :spatial => true
+ end
+ end
+
+ def down
+ drop_table :checkins
+ end
+end
View
8 db/schema.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
@@ -10,6 +11,11 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 0) do
+ActiveRecord::Schema.define(:version => 20120816081310) do
+
+ create_table "checkins", :force => true do |t|
+ t.string "title"
+ t.spatial "location", :limit => {:srid=>4326, :type=>"point", :geographic=>true}
+ end
end
View
9 test/fixtures/checkins.yml
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ title: MyString
+ location:
+
+two:
+ title: MyString
+ location:
View
49 test/functional/checkins_controller_test.rb
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class CheckinsControllerTest < ActionController::TestCase
+ setup do
+ @checkin = checkins(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:checkins)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create checkin" do
+ assert_difference('Checkin.count') do
+ post :create, checkin: { location: @checkin.location, title: @checkin.title }
+ end
+
+ assert_redirected_to checkin_path(assigns(:checkin))
+ end
+
+ test "should show checkin" do
+ get :show, id: @checkin
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, id: @checkin
+ assert_response :success
+ end
+
+ test "should update checkin" do
+ put :update, id: @checkin, checkin: { location: @checkin.location, title: @checkin.title }
+ assert_redirected_to checkin_path(assigns(:checkin))
+ end
+
+ test "should destroy checkin" do
+ assert_difference('Checkin.count', -1) do
+ delete :destroy, id: @checkin
+ end
+
+ assert_redirected_to checkins_path
+ end
+end
View
7 test/unit/checkin_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class CheckinTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
4 test/unit/helpers/checkins_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class CheckinsHelperTest < ActionView::TestCase
+end

0 comments on commit 52e4289

Please sign in to comment.