Permalink
Browse files

Added basic activities

  • Loading branch information...
1 parent 3bf22d5 commit 696b6f1a998badb2f148db6afc17bf138962aad1 @amitamb committed Apr 9, 2012
@@ -3,6 +3,8 @@ class ApplicationController < ActionController::Base
helper_method :current_user, :current_person
+ before_filter :set_current_user
+
private
def current_user
@@ -12,4 +14,9 @@ def current_user
def current_person
@current_person ||= current_user.person
end
+
+ # try to use http://stackoverflow.com/questions/7896298/safety-of-thread-current-usage-in-rails
+ def set_current_user
+ User.current = current_user
+ end
end
@@ -0,0 +1,43 @@
+module ActivitiesHelper
+ def sentence_person_link(person, capitalize = false)
+ if current_person.id == person.id
+ capitalize ? "You" : "you"
+ else
+ link_to person.name, person
+ end
+ end
+
+ # sentence_person_link(activity.person) %> <%= sentence_middle(person) %> <%=
+ def activity_sentence(activity)
+
+ start = sentence_person_link(activity.person, true)
+
+ middle = case activity.activity_type
+ when Activity::WINGMAN_REQUEST_CREATED then " sent wingmanship request to "
+ when Activity::WINGMAN_REQUEST_ACCEPTED then " accepted wingmanship request from "
+ #when Activity::WINGMAN_REQUEST_CANCELED then " canceled wingmanship request to "
+ #when Activity::WINGMAN_REQUEST_REJECTED then " rejected wingmanship request from "
+ when Activity::ITEM_SHARED then " shared item with "
+ #when Activity::ITEM_DELETED then " deleted an item "
+ when Activity::COMMENT_CREATED then " commented on "
+ else "Unknown"
+ end
+
+ last = case activity.activity_type
+ when Activity::WINGMAN_REQUEST_CREATED then sentence_person_link(activity.target.wingman)
+ when Activity::WINGMAN_REQUEST_ACCEPTED then sentence_person_link(activity.target.person)
+ #when Activity::WINGMAN_REQUEST_CANCELED then sentence_person_link(activity.secondary)
+ #when Activity::WINGMAN_REQUEST_REJECTED then sentence_person_link(activity.target.person)
+ when Activity::ITEM_SHARED then activity.target.listeners.length == 1 ? sentence_person_link(activity.target.listeners.first) : pluralize(activity.target.listeners.length, "person")
+ #when Activity::ITEM_DELETED then " deleted the item "
+ when Activity::COMMENT_CREATED then " commented on "
+ else "Unknown"
+ end
+
+
+
+ sent = start + middle + last + "."
+
+ sent.html_safe
+ end
+end
@@ -0,0 +1,38 @@
+class Activity < ActiveRecord::Base
+ belongs_to :person
+ belongs_to :secondary, :foreign_key => :secondary_id, :class_name => "Person"
+ belongs_to :target, :polymorphic => true
+
+ has_and_belongs_to_many :listeners, :class_name => "Person" # :foreign_key => person
+
+ default_scope joins("INNER JOIN activities_people ON activities_people.activity_id = activities.id").where("activities_people.person_id = ?", Person.current).order('activities.created_at DESC').limit(10)
+
+ # User sent wingmanship request to wingmanship.wingman
+ WINGMAN_REQUEST_CREATED = 1 #target —> model wingmanship
+ # User accepted wingmanship request to wingmanship.person
+ WINGMAN_REQUEST_ACCEPTED = 2 #target —> model wingmanship
+ # User canceled wingmanship request to wingmanship.wingman
+ # WINGMAN_REQUEST_CANCELED = 3 #target —> model wingmanship
+ # User rejected wingmanship request from wingmanship.person
+ # WINGMAN_REQUEST_REJECTED = 4 #target —> model wingmanship
+
+ # User shared item with [list of people] i.e. item.listeners
+ ITEM_SHARED = 11 #target —> model item
+ # User shared item with [list of people] i.e. item.listeners
+ # ITEM_DELETED = 12 #target —> model item
+
+ # User commented on comment.commentable
+ COMMENT_CREATED = 21 #target —> model comment
+ # User commented on comment.commentable
+ # not needed
+ # COMMENT_DELETED = 22 #target —> model comment
+
+ class << self
+ def add(person, activity_type, target, listeners = [], secondary = nil )
+ return false if person.blank? or activity_type.blank? or target.blank?
+ activity = Activity.create(:person => person, :activity_type => activity_type, :target => target) #, :secondary => secondary )
+ activity.listeners << listeners
+ end
+ end
+
+end
@@ -1,6 +0,0 @@
-ruby "item.rb"
-
-pause
-del "%0"
-
-pause
View
@@ -9,4 +9,6 @@ class Item < ActiveRecord::Base
acts_as_commentable
+ has_many :activities, :as => :target
+
end
@@ -0,0 +1,14 @@
+class ItemObserver < ActiveRecord::Observer
+ def after_create(item)
+ Activity.add(item.sharer, Activity::ITEM_SHARED, item, item.listeners.all.concat( [item.sharer] ) )
+ end
+
+ def before_destroy(item)
+ item.activities.destroy_all
+ end
+
+ def after_save(item)
+ #Activity.add(wingmanship.person, Activity::WINGMAN_REQUEST_CANCELED, wingmanship, [], wingmanship.wingman)
+ end
+
+end
View
@@ -1,4 +1,13 @@
class Person < ActiveRecord::Base
+
+ def self.current
+ Thread.current[:user].person
+ end
+
+ def self.current=(person)
+ Thread.current[:user] = person.user
+ end
+
acts_as_taggable_on :knows, :needs
belongs_to :user
@@ -36,5 +45,7 @@ def suggested_people
end
has_many :comments
+
+ has_and_belongs_to_many :activities
end
View
@@ -1,6 +1,14 @@
class User < ActiveRecord::Base
+ def self.current
+ Thread.current[:user]
+ end
+
+ def self.current=(user)
+ Thread.current[:user] = user
+ end
+
has_one :person
def image_large
@@ -5,9 +5,13 @@ class Wingmanship < ActiveRecord::Base
def approved?
self.wingman_approved == true
end
-
+
def approve!
self.wingman_approved = true
+ WingmanshipObserver.approved(self)
self.save!
end
+
+ has_many :activities, :as => :target
+
end
@@ -0,0 +1,14 @@
+class WingmanshipObserver < ActiveRecord::Observer
+ def after_create(wingmanship)
+ Activity.add(wingmanship.person, Activity::WINGMAN_REQUEST_CREATED, wingmanship, [wingmanship.person, wingmanship.wingman])
+ end
+
+ def before_destroy(wingmanship)
+ wingmanship.activities.destroy_all
+ end
+
+ def self.approved(wingmanship)
+ Activity.add(wingmanship.wingman, Activity::WINGMAN_REQUEST_ACCEPTED, wingmanship, [wingmanship.person, wingmanship.wingman])
+ end
+
+end
@@ -0,0 +1,8 @@
+<div class="activity" style="width:100%;">
+ <%= link_to image_tag(activity.person.image, :style => "width:50px;heiht:50px;"), activity.person, :style => "float:left;" %>
+ <div style="display:table-cell;padding-left:10px;">
+ <%= activity_sentence(activity) %>
+ </div>
+</div>
+<div class="clear"></div>
+<hr />
@@ -33,6 +33,14 @@
<% end %>
<% end %>
<% unless Item.all.empty? %>
+ <h3>Recently Activity</h3><br />
+ <% Activity.all.each do |activity| %>
+ <%= render activity %>
+ <% end %>
+ <% else %>
+ You have not shared anything. <a href="/">Start sharing</a>
+ <% end %>
+ <% unless Item.all.empty? %>
<!-- <ul class="items"> -->
<h3>Recently Shared Items</h3><br />
<% Item.order("created_at DESC").each do |i| %>
@@ -24,6 +24,7 @@ class Application < Rails::Application
# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
+ config.active_record.observers = :wingmanship_observer, :item_observer #, :signup_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
@@ -0,0 +1,15 @@
+class CreateActivities < ActiveRecord::Migration
+ def change
+ create_table :activities do |t|
+ t.references :person
+ #t.integer :secondary_id
+ t.integer :activity_type
+ t.integer :target_id
+ t.string :target_type
+
+ t.timestamps
+ end
+ add_index :activities, :person_id
+ add_index :activities, [ :target_id , :target_type ]
+ end
+end
@@ -0,0 +1,10 @@
+class CreateActivitiesPeople < ActiveRecord::Migration
+ def change
+ create_table :activities_people do |t|
+ t.references :activity
+ t.references :person
+ end
+ add_index :activities_people, :activity_id
+ add_index :activities_people, :person_id
+ end
+end
View
@@ -11,7 +11,27 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120406111613) do
+ActiveRecord::Schema.define(:version => 20120406232129) do
+
+ create_table "activities", :force => true do |t|
+ t.integer "person_id"
+ t.integer "activity_type"
+ t.integer "target_id"
+ t.string "target_type"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "activities", ["person_id"], :name => "index_activities_on_person_id"
+ add_index "activities", ["target_id", "target_type"], :name => "index_activities_on_target_id_and_target_type"
+
+ create_table "activities_people", :force => true do |t|
+ t.integer "activity_id"
+ t.integer "person_id"
+ end
+
+ add_index "activities_people", ["activity_id"], :name => "index_activities_people_on_activity_id"
+ add_index "activities_people", ["person_id"], :name => "index_activities_people_on_person_id"
create_table "comments", :force => true do |t|
t.string "title", :limit => 50, :default => ""
View
@@ -0,0 +1,13 @@
+module PseudoDestroyable
+ def acts_as_pseudo_destroyable
+ include InstanceMethods
+ alias_method_chain :destroy, :build
+ default_scope where(:is_destroyed => false)
+ end
+
+ module InstanceMethods
+ def destroy_with_pseudo
+ self.update_attribute(:is_destroyed, true)
+ end
+ end
+end
@@ -0,0 +1,13 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ person:
+ activity_type:
+ target_id:
+ target_type: MyString
+
+two:
+ person:
+ activity_type:
+ target_id:
+ target_type: MyString
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ activity:
+ person:
+
+two:
+ activity:
+ person:
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class ActivitiesPeopleTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class ActivityTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit 696b6f1

Please sign in to comment.