Skip to content
Browse files

Implemented user location ask and modification through their profile

  • Loading branch information...
1 parent 59c187c commit b7f784d87c329731776024f5d187229285591049 @okeen okeen committed Sep 6, 2011
View
9 app/controllers/players_controller.rb
@@ -1,6 +1,7 @@
class PlayersController < ApplicationController
before_filter :load_facebook_metadata, :only => :show
before_filter :load_facebook_player_data, :only => :create
+ before_filter :authenticate_player!, :only => :home
# GET /players
# GET /players.xml
@@ -15,7 +16,7 @@ def index
end
def home
- redirect_to player_path(current_player)
+ @player = Player.includes(:stat).includes(:achievements).find(current_player.id)
end
# GET /players/1
@@ -80,10 +81,10 @@ def update
respond_to do |format|
if @player.update_attributes(params[:player])
format.html { redirect_to(@player, :notice => 'Player was successfully updated.') }
- format.xml { head :ok }
+ format.js { render :json => {}, :status => :created}
else
- format.html { render :action => "edit" }
- format.xml { render :xml => @player.errors, :status => :unprocessable_entity }
+ format.html { render :action => "new" }
+ format.js { render :json => @player.errors, :status => :unprocessable_entity }
end
end
end
View
13 app/models/notification_type.rb
@@ -6,6 +6,7 @@ class NotificationType < ActiveRecord::Base
def self.create_all_notification_types
NotificationType.create(:name => "new_player")
+ NotificationType.create(:name => "ask_player_location")
NotificationType.create(:name => "new_team")
NotificationType.create(:name => "team_confirmed")
NotificationType.create(:name => "team_rejected")
@@ -31,6 +32,18 @@ def NotificationType.NEW_PLAYER(player)
}
end
+ def NotificationType.ASK_PLAYER_LOCATION(player)
+ {
+ :notification_type_id => NotificationType.named("ask_player_location").first.id,
+ :params => {
+ :title => "IMPORTANT: Where are you?",
+ :message => "We need to know where you are located to offer our best service."+
+ "Please set your location in your profile page",
+ :urgent => true
+ }
+ }
+ end
+
def NotificationType.NEW_TEAM(team)
accept_code = team.confirmations.to_accept.first.code
reject_code = team.confirmations.to_reject.first.code
View
8 app/models/player.rb
@@ -12,7 +12,7 @@ class Player < ActiveRecord::Base
before_create :init_devise_password
before_create :geocode_with_gmaps
before_update :geocode_with_gmaps
- after_create :create_welcome_notification
+ after_create :create_welcome_notification, :create_geographic_location_notification_if_needed
geocoded_by :full_address
@@ -56,6 +56,12 @@ def facebook_url
end
private
+ def create_geographic_location_notification_if_needed
+ unless self.geocoded?
+ self.notifications.create(NotificationType.ASK_PLAYER_LOCATION(self))
+ end
+ end
+
def geocode_with_gmaps
return true if self.full_address.blank?
require 'open-uri'
View
78 app/views/players/home.html.erb
@@ -0,0 +1,78 @@
+<%= javascript_include_tag 'player_home' %>
+<%= google_maps %>
+<p id="notice"><%= notice %></p>
+<h2 class="player_name_title">Profile page: <%= @player.name %></h2>
+<div id="player_info" class="fields_container_panel">
+ <fieldset id="player_info_card_subpanel" class="inner_container">
+ <legend>Player Card</legend>
+ <%= render :partial => "players/player_panel",
+ :locals => {:player => @player} %>
+ </fieldset>
+ <fieldset id="player_info_basic_info_subpanel" class="inner_container">
+ <legend>Info</legend>
+ <table id="player_basic_info_table">
+ <tr>
+ <td class="info_attribute"><label>Name: </label></td>
+ <td><%= @player.name%></td>
+ </tr>
+ <tr>
+ <td class="info_attribute"><label>From: </label></td>
+ <td id="player_full_address"><%= @player.city%>, <%= @player.country%> <a href="#" id="change_user_location">change</a></td>
+
+ </tr>
+ <tr>
+ <td class="info_attribute"><label>Player since: </label></td>
+ <td><%= @player.created_at%></td>
+ </tr>
+ <tr>
+ <td class="info_attribute"><label>games won: </label></td>
+ <td><%= @player.stat.wins%></td>
+ </tr>
+ <tr>
+ <td class="info_attribute"><label>games lost: </label></td>
+ <td><%= @player.stat.lost%></td>
+ </tr>
+ </table>
+ <%= form_for @player, :remote => true,
+ :html =>{:id =>"player_location_form", :style=>"display:none;"} do |location_form|%>
+ <div id="map" style="float:right;width:400px;height:400px"></div>
+ <div class="field">
+ <%= location_form.label :full_address %><br />
+ <%= location_form.text_field :full_address, :id => "place_full_address" %>
+ </div>
+ <button id="geocode">Find</button>
+ <%= location_form.submit "Save Location" %>
+ <ul id="geocoded_results"></ul>
+ <%= location_form.hidden_field :latitude, :id => "place_latitude" %>
+ <%= location_form.hidden_field :longitude, :id => "place_longitude" %>
+ <%= location_form.hidden_field :country, :id => "place_country" %>
+ <%= location_form.hidden_field :state, :id => "place_state" %>
+ <%= location_form.hidden_field :city, :id => "place_city" %>
+ <%= location_form.hidden_field :street, :id => "place_street" %>
+ <% end %>
+ </fieldset>
+ <fieldset id="player_info_teams_subpanel" class="inner_container">
+ <legend>Teams</legend>
+ <table id="player_teams_table" >
+ <div class="catalogue_items_list_container">
+ <% @player.teams.in_groups_of(3) do |n_teams_group| %>
+ <div class="catalogue_items_container_row">
+ <% n_teams_group.compact.each do |team|%>
+
+ <div class="catalogue_item_container clickable_container">
+ <%= render :partial => 'teams/team_panel',
+ :locals => {:team => team} %>
+ </div>
+ <% end %>
+ </div>
+ <% end %>
+ </div>
+ </table>
+ </fieldset>
+</div>
+
+<%= player_like_button(@player) %>
+<%= facebook_player_comments_panel(@player) %>
+
+<%#= render :partial => 'player_statistics', :locals => {:graphGamesWinLost => @graphGamesWinLost,
+:graph_games_played =>@graphGamesPlayed} %>
View
118 public/javascripts/player_home.js
@@ -0,0 +1,118 @@
+$(function() {
+ window.Player = Backbone.Model.extend({
+ defaults: {
+ name: ''
+ },
+ initialize: function(){
+ _.bindAll(this,
+ 'playerUpdateSuccess',
+ 'playerUpdateError');
+ $('form#player_location_form').bind('ajax:success', this.playerUpdateSuccess);
+ $('form#player_location_form').bind('ajax:error', this.playerUpdateError);
+ },
+ playerUpdateSuccess: function(data, response){
+ $("#player_location_form").hide();
+ $("#player_full_address").html($("#place_city").val() + ", " +$("#place_country").val());
+
+
+ },
+ playerUpdateError: function(){}
+ });
+
+ window.PlayerView = Backbone.View.extend({
+ el: "ul#geocoded_results",
+ places: [],
+
+ initialize: function(){
+ _.bindAll(this, 'render','createGmapsPanel', 'showSetLocationPanel', 'geocode', 'addGeocodedPlaces', 'getGeocodeAttribute', 'setSelectedPlace');
+
+ $('#change_user_location').bind('click', this.showSetLocationPanel);
+ $('button#geocode').bind('click', this.geocode);
+ $('ul#geocoded_results li.geocoded_place a').live('click', this.setSelectedPlace, this);
+
+ },
+ showSetLocationPanel: function(mapOptions){
+ $("#player_location_form").show();
+ this.createGmapsPanel(mapOptions);
+ this.geocoder = new google.maps.Geocoder();
+ $("#place_full_address").focus();
+ $("#place_full_address").select();
+
+ },
+ createGmapsPanel: function(){
+ var mapOptions = {
+ zoom: 13,
+ center: new google.maps.LatLng(43.35564,-8.389435),
+ mapTypeId: google.maps.MapTypeId.ROADMAP
+ };
+ this.map= new google.maps.Map($("#map")[0],mapOptions);
+ },
+ render: function(){
+ $(this.el).empty();
+ for (var i=0; i< this.places.length; i++){
+ var place = this.places[i];
+ $(this.el).append("<li class='geocoded_place'><a>"+
+ place.full_address+
+ "</a></li>");
+ }
+ return this;
+ },
+ setSelectedPlace: function(e, item){
+ var place = _(playerView.places).find(function(p){
+ return p.full_address == $(e.target).html();
+ });
+ var coordinates = new google.maps.LatLng(place.latitude, place.longitude);
+ this.map.panTo( coordinates );
+ var marker = new google.maps.Marker( {position: coordinates, map: this.map } );
+ $('input#place_state')[0].value = place.state;
+ $('input#place_city')[0].value = place.city;
+ $('input#place_country')[0].value = place.country;
+ $('input#place_street')[0].value = place.street;
+ $('input#place_latitude')[0].value = place.latitude;
+ $('input#place_longitude')[0].value = place.longitude;
+ },
+
+ geocode: function(e,response){
+ this.places = [];
+ var address = $("input#place_full_address")[0].value;
+ console.log("Geocode: getting geocode for " + address);
+ this.geocoder.geocode({
+ 'address':address,
+ 'partialmatch':true
+ }, this.addGeocodedPlaces);
+ return false;
+ },
+ addGeocodedPlaces: function(results, status){
+ console.log("Geocode: got response." );
+ if (status == 'OK'){
+ for (var i=0; i< results.length; i++){
+ console.log("Geocode: result#" +(i+1).toString() + " " + results[i].formatted_address);
+ var place = {
+ full_address: results[i].formatted_address,
+ latitude: results[i].geometry.location.lat(),
+ longitude: results[i].geometry.location.lng(),
+ country: this.getGeocodeAttribute("country", results[i]),
+ city: this.getGeocodeAttribute("locality", results[i]),
+ street: this.getGeocodeAttribute("route", results[i]) + "," + this.getGeocodeAttribute("street_number", results[i]),
+ state: this.getGeocodeAttribute("administrative_area_level_1", results[i])
+ };
+ place.id = i;
+ this.places.push(place);
+ }
+ }
+ this.render();
+ },
+ getGeocodeAttribute: function(attr, geocode_result){
+ var item = _(geocode_result.address_components)
+ .find(function(attribute_object){
+ return _(attribute_object.types).include(attr);
+ });
+ if (item) return item.long_name;
+ else return null;
+ }
+ });
+ window.player = new Player();
+ window.playerView = new PlayerView({
+ model: window.player
+ });
+})
View
6 public/javascripts/session.js
@@ -154,8 +154,12 @@ $(function() {
window.SessionView = Backbone.View.extend({
id: "user_panel",
initialize: function(){
- _.bindAll(this, 'render');
+ _.bindAll(this, 'render', 'openUserLocationPanel');
this.model.bind('change', this.render);
+ $("#ask_user_location").live("click", this.openUserLocationPanel);
+ },
+ openUserLocationPanel: function(e){
+ $.dialog("")
},
render: function(){
if (this.model.get('logged')) {

0 comments on commit b7f784d

Please sign in to comment.
Something went wrong with that request. Please try again.