diff --git a/Gemfile b/Gemfile index 67a33e4..2132019 100644 --- a/Gemfile +++ b/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' diff --git a/Gemfile.lock b/Gemfile.lock index cdb58dd..9b645d9 100644 --- a/Gemfile.lock +++ b/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) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 9097d83..e9ba15d 100644 --- a/app/assets/javascripts/application.js +++ b/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.'); + } +} \ No newline at end of file diff --git a/app/assets/javascripts/checkins.js.coffee b/app/assets/javascripts/checkins.js.coffee new file mode 100644 index 0000000..7615679 --- /dev/null +++ b/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/ diff --git a/app/assets/stylesheets/checkins.css.scss b/app/assets/stylesheets/checkins.css.scss new file mode 100644 index 0000000..fb7e790 --- /dev/null +++ b/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/ diff --git a/app/assets/stylesheets/scaffolds.css.scss b/app/assets/stylesheets/scaffolds.css.scss new file mode 100644 index 0000000..05188f0 --- /dev/null +++ b/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; } } diff --git a/app/controllers/checkins_controller.rb b/app/controllers/checkins_controller.rb new file mode 100644 index 0000000..15a89ce --- /dev/null +++ b/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 diff --git a/app/helpers/checkins_helper.rb b/app/helpers/checkins_helper.rb new file mode 100644 index 0000000..0e06a6d --- /dev/null +++ b/app/helpers/checkins_helper.rb @@ -0,0 +1,2 @@ +module CheckinsHelper +end diff --git a/app/models/checkin.rb b/app/models/checkin.rb new file mode 100644 index 0000000..b7b3830 --- /dev/null +++ b/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 diff --git a/app/views/checkins/_form.html.erb b/app/views/checkins/_form.html.erb new file mode 100644 index 0000000..2425bee --- /dev/null +++ b/app/views/checkins/_form.html.erb @@ -0,0 +1,35 @@ +<%= form_for(@checkin) do |f| %> + <% if @checkin.errors.any? %> +
+

<%= pluralize(@checkin.errors.count, "error") %> prohibited this checkin from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :title %> +
+ <%= f.text_field :title %> +
+
+ <%= f.label :latitude %> +
+ <%= f.text_field :latitude %> +
+
+ <%= f.label :longitude %> +
+ <%= f.text_field :longitude %> +
+
+ <%= f.submit %> +
+<% end %> + + + \ No newline at end of file diff --git a/app/views/checkins/edit.html.erb b/app/views/checkins/edit.html.erb new file mode 100644 index 0000000..a6fffd2 --- /dev/null +++ b/app/views/checkins/edit.html.erb @@ -0,0 +1,6 @@ +

Editing checkin

+ +<%= render 'form' %> + +<%= link_to 'Show', @checkin %> | +<%= link_to 'Back', checkins_path %> diff --git a/app/views/checkins/index.html.erb b/app/views/checkins/index.html.erb new file mode 100644 index 0000000..1b40386 --- /dev/null +++ b/app/views/checkins/index.html.erb @@ -0,0 +1,62 @@ + + +
+ +

Listing checkins

+ + + + + + + + + + +<% @checkins.each do |checkin| %> + + + + + + + +<% end %> +
TitleLocation
<%= checkin.title %><%= checkin.location %><%= link_to 'Show', checkin %><%= link_to 'Edit', edit_checkin_path(checkin) %><%= link_to 'Destroy', checkin, confirm: 'Are you sure?', method: :delete %>
+ +
+ +<%= link_to 'New Checkin', new_checkin_path %> + + \ No newline at end of file diff --git a/app/views/checkins/new.html.erb b/app/views/checkins/new.html.erb new file mode 100644 index 0000000..3c0429c --- /dev/null +++ b/app/views/checkins/new.html.erb @@ -0,0 +1,5 @@ +

New checkin

+ +<%= render 'form' %> + +<%= link_to 'Back', checkins_path %> diff --git a/app/views/checkins/show.html.erb b/app/views/checkins/show.html.erb new file mode 100644 index 0000000..f003d0e --- /dev/null +++ b/app/views/checkins/show.html.erb @@ -0,0 +1,22 @@ +

<%= notice %>

+ +

+ Title: + <%= @checkin.title %> +

+ +

+ Location: + <%= @checkin.location %> +

+ + +<%= link_to 'Edit', edit_checkin_path(@checkin) %> | +<%= link_to 'Back', checkins_path %> + +

Nearby check-ins

+ \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index 1dcee60..ce820ae 100644 --- a/config/application.rb +++ b/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 diff --git a/config/routes.rb b/config/routes.rb index ad5feb8..1d9db13 100644 --- a/config/routes.rb +++ b/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. diff --git a/db/migrate/20120816081310_create_checkins.rb b/db/migrate/20120816081310_create_checkins.rb new file mode 100644 index 0000000..28e2faa --- /dev/null +++ b/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 \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index 671c0da..39a5a75 100644 --- a/db/schema.rb +++ b/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 diff --git a/test/fixtures/checkins.yml b/test/fixtures/checkins.yml new file mode 100644 index 0000000..7f8ae02 --- /dev/null +++ b/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: diff --git a/test/functional/checkins_controller_test.rb b/test/functional/checkins_controller_test.rb new file mode 100644 index 0000000..e32694f --- /dev/null +++ b/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 diff --git a/test/unit/checkin_test.rb b/test/unit/checkin_test.rb new file mode 100644 index 0000000..ccaae5f --- /dev/null +++ b/test/unit/checkin_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CheckinTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/unit/helpers/checkins_helper_test.rb b/test/unit/helpers/checkins_helper_test.rb new file mode 100644 index 0000000..40a28f8 --- /dev/null +++ b/test/unit/helpers/checkins_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class CheckinsHelperTest < ActionView::TestCase +end