Skip to content
This repository has been archived by the owner on Jul 30, 2022. It is now read-only.

Commit

Permalink
redesign activity
Browse files Browse the repository at this point in the history
  • Loading branch information
daqing committed Jun 2, 2011
1 parent f893444 commit 2b9f41b
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 83 deletions.
2 changes: 0 additions & 2 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ class ActivitiesController < ApplicationController
def create
@activity = Activity.new(:user_id => current_user.id,
:event_name => 'chat',
:target_type => 'chat',
:target_id => 0,
:data => params[:activity][:data])
respond_to do |format|
if @activity.save
Expand Down
24 changes: 12 additions & 12 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ def create
respond_to do |format|
if @comment.save
mb_chars = @comment.content.mb_chars
Activity.create!(:user_id => current_user.id,
:event_name => 'create_comment',
:target_id => @issue.id,
:target_type => 'Issue',
:related_id => @issue.project.id,
:related_type => 'Project',
:data => {
:project_name => @issue.project.name,
:issue_title => @issue.title,
:comment_body => mb_chars.length > 20 ? "#{mb_chars[0..20]}..." : @comment.content
}
)
Activity.create!(
:user_id => current_user.id,
:project_id => @issue.project.id,
:event_name => 'create_comment',
:target_id => @issue.id,
:data => {
:project_name => @issue.project.name,
:issue_title => @issue.title,
:comment_body => mb_chars.length > 20 ? "#{mb_chars[0..20]}..." : @comment.content
}
)

format.html { redirect_to @issue }
format.js
else
Expand Down
47 changes: 25 additions & 22 deletions app/controllers/issues_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ def create
@issue.label = params[:issue][:label]
@issue.user = current_user
if @issue.save
Activity.create!(:user_id => current_user.id,
:event_name => 'create_issue',
:target_type => 'Issue',
:target_id => @issue.id,
:related_id => @project.id,
:related_type => 'Project',
:data => {:title => @issue.title, :related_name => @project.name, :label => @issue.label}
)
Activity.create!(
:user_id => current_user.id,
:project_id => params[:project_id],
:event_name => 'create_issue',
:target_id => @issue.id,
:data => { :title => @issue.title, :project_name => @project.name, :label => @issue.label }
)
redirect_to @issue
else
render :new
Expand Down Expand Up @@ -106,12 +105,13 @@ def change_state
if @issue.respond_to? event_action
begin
@issue.send(event_action)
Activity.create!(:user_id => current_user.id,
:event_name => 'change_issue_state',
:target_id => @issue.id,
:target_type => 'Issue',
:data => {:current_state => @issue.current_state.name, :issue_title => @issue.title}
)
Activity.create!(
:user_id => current_user.id,
:project_id => @issue.project.id,
:event_name => 'change_issue_state',
:target_id => @issue.id,
:data => {:current_state => @issue.current_state.name, :issue_title => @issue.title}
)
format.html { redirect_to [@issue.project, @issue] }
format.js { render :layout => false }
rescue
Expand All @@ -128,14 +128,17 @@ def change_state
def assign_to
user = User.find(params[:user_id])
@issue.assigned_user = user
Activity.create!(:user_id => current_user.id,
:event_name => 'assign_issue',
:target_id => @issue.id,
:target_type => 'Issue',
:related_id => params[:user_id],
:related_type => 'User',
:data => {:issue_title => @issue.title, :related_name => user.name}
)
Activity.create!(
:user_id => current_user.id,
:project_id => @issue.project.id,
:event_name => 'assign_issue',
:target_id => @issue.id,
:data => {
:issue_title => @issue.title,
:assigned_id => params[:user_id],
:assigned_name => user.name
}
)

respond_to do |format|
format.html { redirect_to @issue }
Expand Down
12 changes: 6 additions & 6 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def new
def create
@project = current_user.projects.new(params[:project])
if @project.save
Activity.create!(:user_id => current_user.id,
:event_name => 'create_project',
:target_type => 'Project',
:target_id => @project.id,
:data => {:name => @project.name}
)
Activity.create!(
:user_id => current_user.id,
:event_name => 'create_project',
:project_id => @project.id,
:data => {:name => @project.name}
)
redirect_to @project
else
render :new
Expand Down
12 changes: 6 additions & 6 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ def issue_title_link(issue, *css_class)

def format_activity(activity)
case activity.event_name
when 'create_project': "#{t('activity.create_project')} #{link_to activity.data['name'], url_for(:controller => :projects, :action => :show, :id => activity.target_id)}"
when 'create_project'
"#{t('activity.create_project')} #{link_to activity.data['name'], url_for(:controller => :projects, :action => :show, :id => activity.project_id)}"
when 'create_issue'
issue_url = link_to activity.data['title'], url_for(:controller => :issues, :action => :show, :id => activity.target_id)
project_url = t('activity.in_project', :url =>
link_to(activity.data['related_name'],
url_for(:controller => :projects, :action => :show, :id => activity.related_id),
:class => 'plain'
link_to(activity.data['project_name'],
url_for(:controller => :projects, :action => :show, :id => activity.project_id)
)
)
locale_str = "activity.create_issue.#{activity.data['label']}"
Expand All @@ -131,7 +131,7 @@ def format_activity(activity)
end
when 'assign_issue'
issue_url = link_to activity.data['issue_title'], url_for(:controller => :issues, :action => :show, :id => activity.target_id)
user_url = link_to activity.data['related_name'], url_for(:controller => :users, :action => :show, :id => activity.related_id)
user_url = link_to activity.data['assigned_name'], url_for(:controller => :users, :action => :show, :id => activity.data['assigned_id'])
t('activity.assigned_issue_to', {:issue_url => issue_url, :user_url => user_url})
when 'change_issue_state'
issue_url = link_to activity.data['issue_title'], url_for(:controller => :issues, :action => :show, :id => activity.target_id)
Expand All @@ -141,7 +141,7 @@ def format_activity(activity)
issue_url = link_to activity.data['issue_title'], url_for(:controller => :issues, :action => :show, :id => activity.target_id)
t('activity.commented_on_issue', :issue_url => issue_url) + "<blockquote>&gt;&nbsp;#{h(activity.data['comment_body'])}</blockquote>"
when 'chat'
%(<span style="color: green;">#{h(activity.data)}</span>)
h(activity.data)
end
end
end
23 changes: 11 additions & 12 deletions app/models/activity.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
# == Schema Information
# Schema version: 20110514032858
# Schema version: 20110602082645
#
# Table name: activities
#
# id :integer not null, primary key
# user_id :integer
# event_name :string(255)
# target_type :string(255)
# target_id :integer
# data :text
# created_at :datetime
# updated_at :datetime
# related_id :integer
# related_type :string(255)
# id :integer not null, primary key
# user_id :integer
# event_name :string(255)
# target_id :integer
# data :text
# created_at :datetime
# updated_at :datetime
# project_id :integer
#

class Activity < ActiveRecord::Base
belongs_to :user
belongs_to :project

default_scope :order => 'created_at DESC'

validates :user_id, :event_name, :target_type, :target_id, :presence => true
validates :user_id, :event_name, :presence => true

def data=(data)
write_attribute(:data, ActiveSupport::JSON.encode(data))
Expand Down
1 change: 1 addition & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Project < ActiveRecord::Base

has_many :issues, :dependent => :destroy
has_many :milestones, :dependent => :destroy
has_many :activities, :dependent => :destroy

validates :name, :user_id, :presence => true
end
Expand Down
17 changes: 17 additions & 0 deletions db/migrate/20110602082645_update_activity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class UpdateActivity < ActiveRecord::Migration
def self.up
change_table :activities do |t|
t.remove :target_type, :related_id, :related_type
t.integer :project_id
end
end

def self.down
change_table :activities do |t|
t.string :target_type
t.string :related_type
t.integer :related_id
t.remove :project_id
end
end
end
13 changes: 9 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110520083521) do
ActiveRecord::Schema.define(:version => 20110602082645) do

create_table "activities", :force => true do |t|
t.integer "user_id"
t.string "event_name"
t.string "target_type"
t.integer "target_id"
t.text "data"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "related_id"
t.string "related_type"
t.integer "project_id"
end

create_table "comments", :force => true do |t|
Expand Down Expand Up @@ -66,6 +64,13 @@
t.datetime "updated_at"
end

create_table "project_users", :force => true do |t|
t.integer "project_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "projects", :force => true do |t|
t.integer "user_id"
t.string "name"
Expand Down
22 changes: 9 additions & 13 deletions test/fixtures/activities.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
# == Schema Information
# Schema version: 20110514032858
# Schema version: 20110602082645
#
# Table name: activities
#
# id :integer not null, primary key
# user_id :integer
# event_name :string(255)
# target_type :string(255)
# target_id :integer
# data :text
# created_at :datetime
# updated_at :datetime
# related_id :integer
# related_type :string(255)
# id :integer not null, primary key
# user_id :integer
# event_name :string(255)
# target_id :integer
# data :text
# created_at :datetime
# updated_at :datetime
# project_id :integer
#

# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
user: daqing
event_name: new_project
target_type: Project
target_id: 1

two:
user: daqing
event_name: new_issue
target_type: Issue
target_id: 1
6 changes: 0 additions & 6 deletions test/unit/activity_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ class ActivityTest < ActiveSupport::TestCase
assert !activity.save

activity.event_name = 'created_project'
assert !activity.save

activity.target_id = 1
assert !activity.save

activity.target_type = 'project'
assert activity.save
end

Expand Down

0 comments on commit 2b9f41b

Please sign in to comment.