Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add friends functionality, maintain logged in state for a month

  • Loading branch information...
commit e958082831fa338b8b9f0861e2db1d936f6e070f 1 parent 95615f5
@SeanRoberts authored
Showing with 201 additions and 15 deletions.
  1. +1 −0  .rvmrc
  2. +1 −1  Gemfile
  3. +2 −0  Gemfile.lock
  4. +3 −0  app/assets/javascripts/friends.js.coffee
  5. +8 −1 app/assets/stylesheets/application.css.scss
  6. +3 −0  app/assets/stylesheets/friends.css.scss
  7. +4 −3 app/controllers/application_controller.rb
  8. +39 −0 app/controllers/friends_controller.rb
  9. +3 −2 app/controllers/sessions_controller.rb
  10. +2 −0  app/helpers/friends_helper.rb
  11. +24 −0 app/models/friend.rb
  12. +5 −1 app/models/player.rb
  13. +11 −1 app/models/user.rb
  14. +12 −0 app/views/friends/_form.html.haml
  15. +4 −0 app/views/friends/_friend.html.haml
  16. +2 −0  app/views/friends/edit.html.haml
  17. +13 −0 app/views/friends/index.html.haml
  18. +2 −0  app/views/friends/new.html.haml
  19. +7 −5 app/views/layouts/application.html.haml
  20. +3 −0  app/views/players/index.html.haml
  21. +2 −0  config/routes.rb
  22. +11 −0 db/migrate/20120329033141_create_friends.rb
  23. +10 −1 db/schema.rb
  24. +11 −0 test/fixtures/friends.yml
  25. +7 −0 test/functional/friends_controller_test.rb
  26. +7 −0 test/unit/friend_test.rb
  27. +4 −0 test/unit/helpers/friends_helper_test.rb
View
1  .rvmrc
@@ -0,0 +1 @@
+rvm 1.9.3
View
2  Gemfile
@@ -6,7 +6,7 @@ gem 'rails', '3.2.1'
# gem 'rails', :git => 'git://github.com/rails/rails.git'
-
+gem 'dynamic_form'
gem 'haml'
gem 'hpricot'
gem 'json'
View
2  Gemfile.lock
@@ -45,6 +45,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.2.0)
+ dynamic_form (1.1.4)
erubis (2.7.0)
execjs (1.3.0)
multi_json (~> 1.0)
@@ -126,6 +127,7 @@ PLATFORMS
DEPENDENCIES
coffee-rails (~> 3.2.1)
+ dynamic_form
haml
hpricot
jquery-rails
View
3  app/assets/javascripts/friends.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
9 app/assets/stylesheets/application.css.scss
@@ -66,6 +66,7 @@ body {
}
h1 { margin: 0px 0px 20px; font-size: 32px; }
+h2 { font-size: 16px; margin: 5px 0px 10px; }
small { font-size: 12px; }
strong { font-weight: bold; }
#container {
@@ -98,4 +99,10 @@ table {
width: 200px;
font-size: 12px;
text-align: right;
-}
+}
+
+nav.user-controls {
+ margin: 10px 0px;
+ a { display: block; }
+}
+
View
3  app/assets/stylesheets/friends.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the friends controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
7 app/controllers/application_controller.rb
@@ -1,20 +1,21 @@
class ApplicationController < ActionController::Base
protect_from_forgery
-
+
helper_method :current_user
private
def current_user
+ session[:user_id] ||= cookies[:user_id]
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
-
+
def login_required
if !current_user
flash[:warning] = "You must be logged in to do that."
redirect_to root_url
end
end
-
+
def not_a_pubby_required
if !current_user || current_user.pubby?
flash[:warning] = "You must be logged in and not a pubby to do that."
View
39 app/controllers/friends_controller.rb
@@ -0,0 +1,39 @@
+class FriendsController < ApplicationController
+ before_filter :not_a_pubby_required
+
+ def new
+ @friend = Friend.new
+ end
+
+ def edit
+ @friend = current_user.friend(params[:id])
+ end
+
+ def update
+ @friend = current_user.friend(params[:id])
+ if @friend.update_attributes(params[:friend])
+ flash[:notice] = "Friend updated!"
+ redirect_to friends_path
+ else
+ render action: :edit
+ end
+ end
+
+ def create
+ @friend = current_user.add_friend(params[:friend])
+ if @friend.valid?
+ flash[:notice] = "Welcome to the friend zone!"
+ redirect_to friends_path
+ else
+ render action: :new
+ end
+ end
+
+ def destroy
+ @friend = current_user.friend(params[:id])
+ @friend.destroy
+ flash[:notice] = "Sorry it didn't work out with #{@friend.name}."
+ redirect_to friends_path
+ end
+
+end
View
5 app/controllers/sessions_controller.rb
@@ -2,13 +2,14 @@ class SessionsController < ApplicationController
def create
auth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
+ cookies[:user_id] = {value: user.id, expires: 1.month.from_now}
session[:user_id] = user.id
redirect_to root_url, :notice => "Signed in!"
end
-
+
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Signed out!"
end
-
+
end
View
2  app/helpers/friends_helper.rb
@@ -0,0 +1,2 @@
+module FriendsHelper
+end
View
24 app/models/friend.rb
@@ -0,0 +1,24 @@
+class Friend < ActiveRecord::Base
+ MAX_FRIENDS_PER_USER = 4
+
+ belongs_to :user
+ validates_presence_of :user
+ validates_presence_of :steam_id
+ validates_uniqueness_of :steam_id, :on => :create, :message => "is already someone's friend."
+ validate :is_not_over_limit
+
+ attr_accessible :steam_id, :name
+
+ def name
+ super || "[unknown]"
+ end
+
+ def self.max_friends_per_user
+ MAX_FRIENDS_PER_USER
+ end
+
+ private
+ def is_not_over_limit
+ errors.add(:base, "You already have #{MAX_FRIENDS_PER_USER} friends, don't you think that's enough?") if user.friends.size > MAX_FRIENDS_PER_USER
+ end
+end
View
6 app/models/player.rb
@@ -13,13 +13,17 @@ def community_id
def pubby?
group = Group.new(ENV["GROUP_NAME"])
- bot? || !group.member_ids.include?(community_id)
+ bot? || (!group.member_ids.include?(community_id) && !friend?)
end
def bot?
steam_id == "BOT"
end
+ def friend?
+ @is_friend ||= Friend.find_by_steam_id(steam_id)
+ end
+
def kick
file = File.join(Rails.root, 'tmp', 'status_cache.txt')
File.unlink(file) if File.exist?(file)
View
12 app/models/user.rb
@@ -1,4 +1,6 @@
class User < ActiveRecord::Base
+ has_many :friends
+
def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
@@ -7,9 +9,17 @@ def self.create_with_omniauth(auth)
user.community_id = auth["extra"]["raw_info"]["steamid"]
end
end
-
+
def pubby?
group = Group.new(ENV["GROUP_NAME"])
!group.member_ids.include?(community_id)
end
+
+ def friend(friend_id)
+ friends.find(friend_id)
+ end
+
+ def add_friend(params)
+ friends.create(params)
+ end
end
View
12 app/views/friends/_form.html.haml
@@ -0,0 +1,12 @@
+= error_messages_for :friend
+
+= form_for @friend do |f|
+ = f.label :steam_id, "Steam ID"
+ = f.text_field :steam_id
+
+ = f.label :name
+ = f.text_field :name
+
+ = f.submit "Save"
+
+= link_to "Steam ID Finder", "http://steamidfinder.com/", target: "_"
View
4 app/views/friends/_friend.html.haml
@@ -0,0 +1,4 @@
+%tr
+ %td= link_to friend.name, edit_friend_path(friend)
+ %td
+ %small= link_to "(Delete)", friend_path(friend), :method => :delete
View
2  app/views/friends/edit.html.haml
@@ -0,0 +1,2 @@
+%h1 Edit #{@friend.name}
+= render partial: "form"
View
13 app/views/friends/index.html.haml
@@ -0,0 +1,13 @@
+%h1 Friends
+
+%p Add up to #{Friend.max_friends_per_user} friends to protect them from the wrath of kick-a-pubby.
+
+- unless current_user.friends.empty?
+ %table
+ %thead
+ %tr
+ %th Friend
+ %th
+ %tbody
+ = render partial: "friend", collection: current_user.friends
+= link_to "Add Friend", new_friend_path if current_user.friends.size < Friend.max_friends_per_user
View
2  app/views/friends/new.html.haml
@@ -0,0 +1,2 @@
+%h1 New Friend
+= render partial: "form"
View
12 app/views/layouts/application.html.haml
@@ -17,11 +17,14 @@
#user-nav
- if current_user
Hello #{current_user.name}
- %br/
- if current_user.pubby?
- You are a pubby and have no access to the kick command. You must be a member of the Something Awful forums and a member of the Lost Continents Steam group to be not a pubby.
- Our list is sometimes out of date by up to an hour. This will not affect your protection from being kicked, only your ability to kick others.
- = link_to "Sign Out", signout_path
+ %br/
+ You are a pubby and have no access to the kick command. You must be a member of the Something Awful forums and a member of the Lost Continents Steam group to be not a pubby. Our list is sometimes out of date by up to an hour.
+ %nav.user-controls
+ - unless current_user.pubby?
+ = link_to "Kick Pubbies", players_path
+ = link_to "Manage Friends", friends_path
+ = link_to "Sign Out", signout_path
- else
= link_to image_tag("sits_large_border.png", :alt => "Sign in with Steam"), "/auth/steam"
@@ -41,7 +44,6 @@
%strong Current Server Time:
%br/
= Time.now.strftime('%l:%M%p on %B %e, %Y')
- %h1 Pubby Kicker
= yield
%br/
%br/
View
3  app/views/players/index.html.haml
@@ -1,3 +1,6 @@
+%h1 Pubby Kicker
+
+
%table
%tr
%th Players (#{@players.size}/24)
View
2  config/routes.rb
@@ -1,4 +1,6 @@
Tf2lcs::Application.routes.draw do
+ resources :friends
+
resources :players do
post :kick, :on => :collection
end
View
11 db/migrate/20120329033141_create_friends.rb
@@ -0,0 +1,11 @@
+class CreateFriends < ActiveRecord::Migration
+ def change
+ create_table :friends do |t|
+ t.integer :user_id
+ t.string :steam_id
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
View
11 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,7 +11,15 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120225183057) do
+ActiveRecord::Schema.define(:version => 20120329033141) do
+
+ create_table "friends", :force => true do |t|
+ t.integer "user_id"
+ t.string "steam_id"
+ t.string "name"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
create_table "users", :force => true do |t|
t.string "uid"
View
11 test/fixtures/friends.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ user_id: 1
+ steam_id: MyString
+ name: MyString
+
+two:
+ user_id: 1
+ steam_id: MyString
+ name: MyString
View
7 test/functional/friends_controller_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class FriendsControllerTest < ActionController::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
7 test/unit/friend_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class FriendTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
View
4 test/unit/helpers/friends_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class FriendsHelperTest < ActionView::TestCase
+end
Please sign in to comment.
Something went wrong with that request. Please try again.