Commit dcdb2fdf authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Observe issue, merge request, note creation - create event

parent a847501f
class ActivityObserver < ActiveRecord::Observer
observe :issue, :merge_request, :note
def after_create(record)
Event.create(
:project => record.project,
:target_id => record.id,
:target_type => record.class.name,
:action => Event.determine_action(record)
)
end
end
class Event < ActiveRecord::Base class Event < ActiveRecord::Base
Created = 1
Updated = 2
Closed = 3
Reopened = 4
Pushed = 5
Commented = 6
belongs_to :project belongs_to :project
belongs_to :target, :polymorphic => true
serialize :data serialize :data
def self.determine_action(record)
if [Issue, MergeRequest].include? record.class
Event::Created
elsif record.kind_of? Note
Event::Commented
end
end
end end
# == Schema Information # == Schema Information
# #
# Table name: events # Table name: events
# #
# id :integer not null, primary key # id :integer not null, primary key
# data_type :string(255) # target_type :string(255)
# data_id :string(255) # target_id :integer
# title :string(255) # title :string(255)
# data :text # data :text
# project_id :integer # project_id :integer
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# action :integer
# #
...@@ -3,6 +3,7 @@ require "grit" ...@@ -3,6 +3,7 @@ require "grit"
class Project < ActiveRecord::Base class Project < ActiveRecord::Base
belongs_to :owner, :class_name => "User" belongs_to :owner, :class_name => "User"
has_many :events, :dependent => :destroy
has_many :merge_requests, :dependent => :destroy has_many :merge_requests, :dependent => :destroy
has_many :issues, :dependent => :destroy, :order => "position" has_many :issues, :dependent => :destroy, :order => "position"
has_many :users_projects, :dependent => :destroy has_many :users_projects, :dependent => :destroy
......
...@@ -44,3 +44,4 @@ end ...@@ -44,3 +44,4 @@ end
# slug :string(255) # slug :string(255)
# user_id :integer # user_id :integer
# #
...@@ -23,7 +23,7 @@ module Gitlab ...@@ -23,7 +23,7 @@ module Gitlab
# config.plugins = [ :exception_notification, :ssl_requirement, :all ] # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running. # Activate observers that should always be running.
config.active_record.observers = :mailer_observer config.active_record.observers = :mailer_observer, :activity_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # 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. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
......
class CreateEvents < ActiveRecord::Migration class CreateEvents < ActiveRecord::Migration
def change def change
create_table :events do |t| create_table :events do |t|
t.string :data_type, :null => true t.string :target_type, :null => true
t.string :data_id, :null => true t.integer :target_id, :null => true
t.string :title, :null => true t.string :title, :null => true
t.text :data, :null => true t.text :data, :null => true
t.integer :project_id, :null => true t.integer :project_id, :null => true
......
class AddActionToEvent < ActiveRecord::Migration
def change
add_column :events, :action, :integer, :null => true
end
end
...@@ -11,16 +11,17 @@ ...@@ -11,16 +11,17 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120228130210) do ActiveRecord::Schema.define(:version => 20120228134252) do
create_table "events", :force => true do |t| create_table "events", :force => true do |t|
t.string "data_type" t.string "target_type"
t.string "data_id" t.integer "target_id"
t.string "title" t.string "title"
t.text "data" t.text "data"
t.integer "project_id" t.integer "project_id"
t.datetime "created_at", :null => false t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
t.integer "action"
end end
create_table "issues", :force => true do |t| create_table "issues", :force => true do |t|
......
...@@ -32,10 +32,14 @@ end ...@@ -32,10 +32,14 @@ end
Factory.add(:issue, Issue) do |obj| Factory.add(:issue, Issue) do |obj|
obj.title = Faker::Lorem.sentence obj.title = Faker::Lorem.sentence
obj.author = Factory :user
obj.assignee = Factory :user
end end
Factory.add(:merge_request, MergeRequest) do |obj| Factory.add(:merge_request, MergeRequest) do |obj|
obj.title = Faker::Lorem.sentence obj.title = Faker::Lorem.sentence
obj.author = Factory :user
obj.assignee = Factory :user
obj.source_branch = "master" obj.source_branch = "master"
obj.target_branch = "master" obj.target_branch = "master"
obj.closed = false obj.closed = false
...@@ -64,3 +68,8 @@ Factory.add(:wikis, WebHook) do |obj| ...@@ -64,3 +68,8 @@ Factory.add(:wikis, WebHook) do |obj|
obj.title = Faker::Lorem.sentence obj.title = Faker::Lorem.sentence
obj.content = Faker::Lorem.sentence obj.content = Faker::Lorem.sentence
end end
Factory.add(:event, Event) do |obj|
obj.title = Faker::Lorem.sentence
obj.project = Factory(:project)
end
require 'spec_helper'
describe ActivityObserver do
let(:project) { Factory :project }
describe "Merge Request created" do
before do
@merge_request = Factory :merge_request, :project => project
@event = Event.last
end
it { @event.should_not be_nil }
it { @event.project.should == project }
it { @event.action.should == Event::Created }
it { @event.target.should == @merge_request }
end
describe "Issue created" do
before do
@issue = Factory :issue, :project => project
@event = Event.last
end
it { @event.should_not be_nil }
it { @event.project.should == project }
it { @event.action.should == Event::Created }
it { @event.target.should == @issue }
end
describe "Issue commented" do
before do
@issue = Factory :issue, :project => project
@note = Factory :note, :noteable => @issue, :project => project
@event = Event.last
end
it { @event.should_not be_nil }
it { @event.project.should == project }
it { @event.action.should == Event::Commented }
it { @event.target.should == @note }
end
end
...@@ -2,18 +2,31 @@ ...@@ -2,18 +2,31 @@
# #
# Table name: events # Table name: events
# #
# id :integer not null, primary key # id :integer not null, primary key
# data_type :string(255) # target_type :string(255)
# data_id :string(255) # target_id :integer
# title :string(255) # title :string(255)
# data :text # data :text
# project_id :integer # project_id :integer
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# action :integer
# #
require 'spec_helper' require 'spec_helper'
describe Event do describe Event do
pending "add some examples to (or delete) #{__FILE__}" describe "Associations" do
it { should belong_to(:project) }
end
describe "Creation" do
before do
@event = Factory :event
end
it "should create a valid event" do
@event.should be_valid
end
end
end end
...@@ -2,6 +2,7 @@ require 'spec_helper' ...@@ -2,6 +2,7 @@ require 'spec_helper'
describe Project do describe Project do
describe "Associations" do describe "Associations" do
it { should have_many(:events) }
it { should have_many(:users) } it { should have_many(:users) }
it { should have_many(:users_projects) } it { should have_many(:users_projects) }
it { should have_many(:issues) } it { should have_many(:issues) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment