Permalink
Browse files

Observe issue, merge request, note creation - create event

  • Loading branch information...
dzaporozhets committed Feb 28, 2012
1 parent a847501 commit dcdb2fdfdb5b5429c945b7d8f6fecd0b3a2da32a
@@ -0,0 +1,12 @@
+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
View
@@ -1,17 +1,36 @@
class Event < ActiveRecord::Base
+ Created = 1
@Andrew8xx8

Andrew8xx8 Feb 13, 2013

Contributor

Why you completely ignore naming conventions?

@dzaporozhets

dzaporozhets Feb 13, 2013

Owner

Instead of blaming me I recommend you to propose a Pull Request. Or you just a troll

@Andrew8xx8

Andrew8xx8 Feb 13, 2013

Contributor

I apologize for my behaviour, please, here is a pull request

+ Updated = 2
+ Closed = 3
+ Reopened = 4
+ Pushed = 5
+ Commented = 6
+
belongs_to :project
+ belongs_to :target, :polymorphic => true
+
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
# == Schema Information
#
# Table name: events
#
-# id :integer not null, primary key
-# data_type :string(255)
-# data_id :string(255)
-# title :string(255)
-# data :text
-# project_id :integer
-# created_at :datetime not null
-# updated_at :datetime not null
+# id :integer not null, primary key
+# target_type :string(255)
+# target_id :integer
+# title :string(255)
+# data :text
+# project_id :integer
+# created_at :datetime not null
+# updated_at :datetime not null
+# action :integer
#
+
View
@@ -3,6 +3,7 @@
class Project < ActiveRecord::Base
belongs_to :owner, :class_name => "User"
+ has_many :events, :dependent => :destroy
has_many :merge_requests, :dependent => :destroy
has_many :issues, :dependent => :destroy, :order => "position"
has_many :users_projects, :dependent => :destroy
View
@@ -44,3 +44,4 @@ def regenerate_from wiki
# slug :string(255)
# user_id :integer
#
+
View
@@ -23,7 +23,7 @@ class Application < Rails::Application
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# 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.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
@@ -1,8 +1,9 @@
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
- t.string :data_type, :null => true
- t.string :data_id, :null => true
+ t.string :target_type, :null => true
+ t.integer :target_id, :null => true
+
t.string :title, :null => true
t.text :data, :null => true
t.integer :project_id, :null => true
@@ -0,0 +1,5 @@
+class AddActionToEvent < ActiveRecord::Migration
+ def change
+ add_column :events, :action, :integer, :null => true
+ end
+end
View
@@ -11,16 +11,17 @@
#
# 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|
- t.string "data_type"
- t.string "data_id"
+ t.string "target_type"
+ t.integer "target_id"
t.string "title"
t.text "data"
t.integer "project_id"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.integer "action"
end
create_table "issues", :force => true do |t|
View
@@ -32,10 +32,14 @@
Factory.add(:issue, Issue) do |obj|
obj.title = Faker::Lorem.sentence
+ obj.author = Factory :user
+ obj.assignee = Factory :user
end
Factory.add(:merge_request, MergeRequest) do |obj|
obj.title = Faker::Lorem.sentence
+ obj.author = Factory :user
+ obj.assignee = Factory :user
obj.source_branch = "master"
obj.target_branch = "master"
obj.closed = false
@@ -64,3 +68,8 @@
obj.title = Faker::Lorem.sentence
obj.content = Faker::Lorem.sentence
end
+
+Factory.add(:event, Event) do |obj|
+ obj.title = Faker::Lorem.sentence
+ obj.project = Factory(:project)
+end
@@ -0,0 +1,42 @@
+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
View
@@ -2,18 +2,31 @@
#
# Table name: events
#
-# id :integer not null, primary key
-# data_type :string(255)
-# data_id :string(255)
-# title :string(255)
-# data :text
-# project_id :integer
-# created_at :datetime not null
-# updated_at :datetime not null
+# id :integer not null, primary key
+# target_type :string(255)
+# target_id :integer
+# title :string(255)
+# data :text
+# project_id :integer
+# created_at :datetime not null
+# updated_at :datetime not null
+# action :integer
#
require 'spec_helper'
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
@@ -2,6 +2,7 @@
describe Project do
describe "Associations" do
+ it { should have_many(:events) }
it { should have_many(:users) }
it { should have_many(:users_projects) }
it { should have_many(:issues) }

0 comments on commit dcdb2fd

Please sign in to comment.