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? %> +
Title | +Location | ++ | + | + |
---|---|---|---|---|
<%= 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 %> | +
<%= notice %>
+ ++ Title: + <%= @checkin.title %> +
+ ++ Location: + <%= @checkin.location %> +
+ + +<%= link_to 'Edit', edit_checkin_path(@checkin) %> | +<%= link_to 'Back', checkins_path %> + +