Permalink
Browse files

Events improved. Open/close issue, merge request events displayed

  • Loading branch information...
1 parent 98b484b commit 94befdd502e619313534664393f4c72a24c9ebec @randx randx committed Mar 7, 2012
@@ -76,7 +76,7 @@ def create
end
def update
- @issue.update_attributes(params[:issue])
+ @issue.update_attributes(params[:issue].merge(:author_id_of_changes => current_user.id))
respond_to do |format|
format.js
@@ -87,7 +87,7 @@ def create
def update
respond_to do |format|
- if @merge_request.update_attributes(params[:merge_request])
+ if @merge_request.update_attributes(params[:merge_request].merge(:author_id_of_changes => current_user.id))
format.html { redirect_to [@project, @merge_request], notice: 'Merge request was successfully updated.' }
format.json { head :ok }
else
@@ -1,12 +1,25 @@
class ActivityObserver < ActiveRecord::Observer
- observe :issue, :merge_request, :note
+ observe :issue, :merge_request
def after_create(record)
Event.create(
:project => record.project,
:target_id => record.id,
:target_type => record.class.name,
- :action => Event.determine_action(record)
+ :action => Event.determine_action(record),
+ :author_id => record.author_id
)
end
+
+ def after_save(record)
+ if record.changed.include?("closed")
+ Event.create(
+ :project => record.project,
+ :target_id => record.id,
+ :target_type => record.class.name,
+ :action => (record.closed ? Event::Closed : Event::Reopened),
+ :author_id => record.author_id_of_changes
+ )
+ end
+ end
end
View
@@ -1,4 +1,6 @@
class Event < ActiveRecord::Base
+ default_scope where("author_id IS NOT NULL")
+
Created = 1
Updated = 2
Closed = 3
@@ -26,13 +28,22 @@ def self.determine_action(record)
# - new issue
# - merge request
def allowed?
- push? || new_issue? || new_merge_request?
+ push? || new_issue? || new_merge_request? ||
+ changed_merge_request? || changed_issue?
end
def push?
action == self.class::Pushed
end
+ def closed?
+ action == self.class::Closed
+ end
+
+ def reopened?
+ action == self.class::Reopened
+ end
+
def new_tag?
data[:ref]["refs/tags"]
end
@@ -57,10 +68,6 @@ def tag_name
@tag_name ||= data[:ref].gsub("refs/tags/", "")
end
- def pusher
- User.find_by_id(data[:user_id])
- end
-
def new_issue?
target_type == "Issue" &&
action == Created
@@ -71,6 +78,16 @@ def new_merge_request?
action == Created
end
+ def changed_merge_request?
+ target_type == "MergeRequest" &&
+ [Closed, Reopened].include?(action)
+ end
+
+ def changed_issue?
+ target_type == "Issue" &&
+ [Closed, Reopened].include?(action)
+ end
+
def issue
target if target_type == "Issue"
end
@@ -80,7 +97,7 @@ def merge_request
end
def author
- target.author
+ @author ||= User.find(author_id)
end
def commits
@@ -89,7 +106,6 @@ def commits
end
end
- delegate :id, :name, :email, :to => :pusher, :prefix => true, :allow_nil => true
delegate :name, :email, :to => :author, :prefix => true, :allow_nil => true
delegate :title, :to => :issue, :prefix => true, :allow_nil => true
delegate :title, :to => :merge_request, :prefix => true, :allow_nil => true
View
@@ -5,6 +5,7 @@ class Issue < ActiveRecord::Base
has_many :notes, :as => :noteable, :dependent => :destroy
attr_protected :author, :author_id, :project, :project_id
+ attr_accessor :author_id_of_changes
validates_presence_of :project_id
validates_presence_of :assignee_id
@@ -5,6 +5,7 @@ class MergeRequest < ActiveRecord::Base
has_many :notes, :as => :noteable, :dependent => :destroy
attr_protected :author, :author_id, :project, :project_id
+ attr_accessor :author_id_of_changes
validates_presence_of :project_id
validates_presence_of :assignee_id
View
@@ -68,7 +68,8 @@ def observe_push(oldrev, newrev, ref, author_key_id)
Event.create(
:project => self,
:action => Event::Pushed,
- :data => data
+ :data => data,
+ :author_id => Key.find_by_identifier(author_key_id).user.id
)
end
@@ -2,7 +2,11 @@
.event_feed
- if event.new_issue?
= render "events/event_new_issue", :event => event
- - if event.new_merge_request?
+ - elsif event.new_merge_request?
= render "events/event_new_merge_request", :event => event
+ - elsif event.changed_merge_request?
+ = render "events/event_changed_merge_request", :event => event
+ - elsif event.changed_issue?
+ = render "events/event_changed_issue", :event => event
- elsif event.push?
= render "events/event_push", :event => event
@@ -0,0 +1,14 @@
+= image_tag gravatar_icon(event.author_email), :class => "avatar"
+%strong #{event.author_name}
+- if event.closed?
+ closed
+- else
+ reopened
+issue
+= link_to project_issue_path(event.project, event.issue) do
+ %strong= truncate event.issue_title
+at
+%strong= link_to event.project.name, event.project
+%span.cgray
+ = time_ago_in_words(event.created_at)
+ ago.
@@ -0,0 +1,19 @@
+= image_tag gravatar_icon(event.author_email), :class => "avatar"
+%strong #{event.author_name}
+- if event.closed?
+ closed
+- else
+ reopened
+merge request
+= link_to project_merge_request_path(event.project, event.merge_request) do
+ %strong= truncate event.merge_request_title
+at
+%strong= link_to event.project.name, event.project
+%span.cgray
+ = time_ago_in_words(event.created_at)
+ ago.
+%br
+%span.label= event.merge_request.source_branch
+&rarr;
+%span.label= event.merge_request.target_branch
+
@@ -1,6 +1,6 @@
- if event.new_branch? || event.new_tag?
- = image_tag gravatar_icon(event.pusher_email), :class => "avatar"
- %strong #{event.pusher_name}
+ = image_tag gravatar_icon(event.author_email), :class => "avatar"
+ %strong #{event.author_name}
pushed new
- if event.new_tag?
tag
@@ -16,8 +16,8 @@
= time_ago_in_words(event.created_at)
ago.
- else
- = image_tag gravatar_icon(event.pusher_email), :class => "avatar"
- %strong #{event.pusher_name}
+ = image_tag gravatar_icon(event.author_email), :class => "avatar"
+ %strong #{event.author_name}
pushed to
= link_to project_commits_path(event.project, :ref => event.branch_name) do
%strong= event.branch_name
@@ -31,6 +31,10 @@
Compare #{event.commits.first.commit.id[0..8]}...#{event.commits.last.id[0..8]}
- @project = event.project
%ul.unstyled
- = render event.commits
+ - if event.commits.size > 4
+ = render event.commits[0..2]
+ %li ... and #{event.commits.size - 3} more commits
+ - else
+ = render event.commits
@@ -0,0 +1,5 @@
+class AddAuthorIdToEvent < ActiveRecord::Migration
+ def change
+ add_column :events, :author_id, :integer, :null => true
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120301185805) do
+ActiveRecord::Schema.define(:version => 20120307095918) do
create_table "events", :force => true do |t|
t.string "target_type"
@@ -22,6 +22,7 @@
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "action"
+ t.integer "author_id"
end
create_table "issues", :force => true do |t|

2 comments on commit 94befdd

In the project.rb model, is there a reason to not simply do
:author_id => data[:user_id]
instead of
:author_id => Key.find_by_identifier(author_key_id).user.id
in the observe_push method?

Owner

randx replied Mar 9, 2012

@davekaro nope. thanks for review

Please sign in to comment.