Skip to content

Commit

Permalink
Added player notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
okeen committed Sep 2, 2011
1 parent 096d433 commit a2c0d92
Show file tree
Hide file tree
Showing 38 changed files with 806 additions and 22 deletions.
60 changes: 60 additions & 0 deletions app/controllers/notifications_controller.rb
@@ -0,0 +1,60 @@
class NotificationsController < ApplicationController
before_filter :authenticate_player!, :load_player
# GET /notifications
# GET /notifications.xml
def index
@notifications = @player.notifications.unread.all

respond_to do |format|
format.json { render :json => @notifications }
end
end

# GET /notifications/1
# GET /notifications/1.xml
def show
@notification = @player.notifications.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @notification }
end
end


# PUT /notifications/1
# PUT /notifications/1.xml
def update
@notification = @player.notifications.find(params[:id])

respond_to do |format|
if @notification.update_attributes(params[:notification])
format.html { redirect_to(@notification, :notice => 'Notification was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @notification.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /notifications/1
# DELETE /notifications/1.xml
def destroy
@notification = @player.notifications.find(params[:id])
@notification.destroy

respond_to do |format|
format.html { redirect_to(notifications_url) }
format.xml { head :ok }
end
end

private

def load_player
@player = current_player
end


end
2 changes: 2 additions & 0 deletions app/helpers/notifications_helper.rb
@@ -0,0 +1,2 @@
module NotificationsHelper
end
11 changes: 10 additions & 1 deletion app/models/confirmable.rb
Expand Up @@ -6,7 +6,8 @@ module Confirmable

after_create :set_initial_status,
:create_confirmations_if_needed,
:deliver_ask_email
:deliver_ask_email,
:create_ask_notifications

default_scope where(:status => "confirmed")

Expand Down Expand Up @@ -47,6 +48,14 @@ def create_confirmations_if_needed
end
end

def create_ask_notifications
confirmating_player_groups.each do |notificating_group|
notificating_group.players.each do |player|
player.notifications.create NotificationType.NEW_TEAM
end
end
end

def mailer
"#{self.class}Mailer".constantize
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/game.rb
Expand Up @@ -116,7 +116,7 @@ def create_facebook_game_event(initiator_player,facebook_token)
private

def confirmating_player_groups
teams
self.team2
end

def create_playground_request_if_needed
Expand Down
19 changes: 19 additions & 0 deletions app/models/notification.rb
@@ -0,0 +1,19 @@
class Notification < ActiveRecord::Base
belongs_to :notification_type
belongs_to :player

serialize :params

default_scope includes(:notification_type)

scope :unread, where(:read => false)

def type
notification_type.name
end

def as_json(options = {})
super(:only => [:id, :created_at, :params, :read],
:methods => [:type])
end
end
59 changes: 59 additions & 0 deletions app/models/notification_type.rb
@@ -0,0 +1,59 @@
class NotificationType < ActiveRecord::Base
belongs_to :notification

scope :named, lambda {|name| where('name = ?', name)}


def self.create_all_notification_types
NotificationType.create(:name => "new_player")
NotificationType.create(:name => "new_team")
NotificationType.create(:name => "team_accepted")
NotificationType.create(:name => "new_game")
NotificationType.create(:name => "game_accepted")
NotificationType.create(:name => "new_result")
NotificationType.create(:name => "result_accepted")
end
create_all_notification_types if self.all.blank?;


def NotificationType.NEW_PLAYER
{
:notification_type_id => NotificationType.named("new_player").first.id,
:params => {
:title => "Welcome to Padelotron",
:message => "As your first actions, yo can create a new team, invite friends or check today's games in your area",
:urgent => true
}
}
end

def NotificationType.NEW_TEAM
{
:notification_type_id => NotificationType.named("new_team").first.id,
:params => {
:title => "New Team Request",
:message => "You have received an offer to join a team"}
}
end

def NotificationType.TEAM_ACCEPTED
{
:notification_type_id => NotificationType.named("team_accepted").first.id,
:params => {
:title => "Team Joined",
:message => "You have joined a new team"
}
}
end

def NotificationType.NEW_GAME
{
:notification_type_id => NotificationType.named("new_game").first.id,
:params => {
:title => "Team Joined",
:message => "You have joined a new team"
}
}
end

end
10 changes: 8 additions & 2 deletions app/models/player.rb
Expand Up @@ -3,13 +3,13 @@ class Player < ActiveRecord::Base
validates :email, :presence => true, :uniqueness => true

has_many :teams, :finder_sql => 'select * from teams t where t.player1_id = #{id} or t.player2_id = #{id}'

has_many :notifications
has_many :player_games, :class_name => "Game", :finder_sql => 'select * from games '

include Statable

devise :database_authenticatable, :omniauthable, :rememberable
before_create :init_devise_password
before_create :init_devise_password,:create_welcome_notification
before_create :geocode_with_gmaps
before_update :geocode_with_gmaps

Expand Down Expand Up @@ -79,4 +79,10 @@ def get_geocode_attribute_value(loc, att)
def init_devise_password
password = Devise.friendly_token[0,20]
end

def create_welcome_notifications
notification= NotificationType.NEW_PLAYER
notification[:params][:name] = self.name
@player.notifications.create notification
end
end
2 changes: 1 addition & 1 deletion app/models/result.rb
Expand Up @@ -67,7 +67,7 @@ def rejection_ask_message
private

def confirmating_player_groups
game.teams
game.winner == team1 ? team2 : team1
end

end
33 changes: 33 additions & 0 deletions app/views/notifications/_form.html.erb
@@ -0,0 +1,33 @@
<%= form_for(@notification) do |f| %>
<% if @notification.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@notification.errors.count, "error") %> prohibited this notification from being saved:</h2>

<ul>
<% @notification.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :player %><br />
<%= f.text_field :player %>
</div>
<div class="field">
<%= f.label :notification_type %><br />
<%= f.text_field :notification_type %>
</div>
<div class="field">
<%= f.label :params %><br />
<%= f.text_area :params %>
</div>
<div class="field">
<%= f.label :read %><br />
<%= f.check_box :read %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/notifications/edit.html.erb
@@ -0,0 +1,6 @@
<h1>Editing notification</h1>

<%= render 'form' %>
<%= link_to 'Show', @notification %> |
<%= link_to 'Back', notifications_path %>
29 changes: 29 additions & 0 deletions app/views/notifications/index.html.erb
@@ -0,0 +1,29 @@
<h1>Listing notifications</h1>

<table>
<tr>
<th>Player</th>
<th>Notification type</th>
<th>Params</th>
<th>Read</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @notifications.each do |notification| %>
<tr>
<td><%= notification.player %></td>
<td><%= notification.notification_type %></td>
<td><%= notification.params %></td>
<td><%= notification.read %></td>
<td><%= link_to 'Show', notification %></td>
<td><%= link_to 'Edit', edit_notification_path(notification) %></td>
<td><%= link_to 'Destroy', notification, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Notification', new_notification_path %>
5 changes: 5 additions & 0 deletions app/views/notifications/new.html.erb
@@ -0,0 +1,5 @@
<h1>New notification</h1>

<%= render 'form' %>
<%= link_to 'Back', notifications_path %>
25 changes: 25 additions & 0 deletions app/views/notifications/show.html.erb
@@ -0,0 +1,25 @@
<p id="notice"><%= notice %></p>

<p>
<b>Player:</b>
<%= @notification.player %>
</p>

<p>
<b>Notification type:</b>
<%= @notification.notification_type %>
</p>

<p>
<b>Params:</b>
<%= @notification.params %>
</p>

<p>
<b>Read:</b>
<%= @notification.read %>
</p>


<%= link_to 'Edit', edit_notification_path(@notification) %> |
<%= link_to 'Back', notifications_path %>
4 changes: 3 additions & 1 deletion app/views/players/_player_session_panel.html.erb
@@ -1,4 +1,6 @@
<div id="player_session_panel" class="fields_container_panel">
<input type="hidden" id="player_has_notifications"
value="<%=player.notifications.unread.count%>"></input>
<div id="player_session_info">
<%= player_photo(player) %>
<fieldset>
Expand All @@ -17,7 +19,7 @@
<%= link_to "Profile", player_home_path %>
</li>
<li>
<%= link_to "News", teams_path %>
<%= link_to "News", teams_path, :id=>"notifications_button" %>
</li>
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Expand Up @@ -30,7 +30,7 @@ class Application < Rails::Application
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# JavaScript files you want as :defaults (application.js is always included).
config.action_view.javascript_expansions[:defaults] = %w(jquery rails underscore json2 backbone jquery.cookie)
config.action_view.javascript_expansions[:defaults] = %w(jquery rails underscore json2 backbone jquery.cookie jquery.gritter.min)
config.action_view.javascript_expansions[:jquery_ui] = "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.15/jquery-ui.js"
config.action_view.javascript_expansions[:google] = "https://www.google.com/jsapi"

Expand Down
8 changes: 6 additions & 2 deletions config/routes.rb
@@ -1,4 +1,5 @@
Padelotron::Application.routes.draw do

match 'players/:id/graph_code' => 'graph#graph_code', :as => :graph_code
match 'players/:id/graph_games_played' => 'graph#graph_games_played', :as => :graph_games_played

Expand Down Expand Up @@ -43,8 +44,11 @@
get "sign_in", :to => "devise/sessions#new"
get "sign_out", :to => "devise/sessions#destroy"
end
resources :players

resources :players do

end
resources :notifications

devise_for :customers, :controllers => { :registrations => "customers",
:confirmation => "customers/confirmations"}
devise_scope :customers do
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20110902180233_create_notification_types.rb
@@ -0,0 +1,13 @@
class CreateNotificationTypes < ActiveRecord::Migration
def self.up
create_table :notification_types do |t|
t.string :name

t.timestamps
end
end

def self.down
drop_table :notification_types
end
end
16 changes: 16 additions & 0 deletions db/migrate/20110902180309_create_notifications.rb
@@ -0,0 +1,16 @@
class CreateNotifications < ActiveRecord::Migration
def self.up
create_table :notifications do |t|
t.references :player
t.references :notification_type
t.text :params
t.boolean :read, :default => false

t.timestamps
end
end

def self.down
drop_table :notifications
end
end

0 comments on commit a2c0d92

Please sign in to comment.