Commit dcdb2fdfdb5b5429c945b7d8f6fecd0b3a2da32a
1 parent
a847501f
Exists in
master
and in
4 other branches
Observe issue, merge request, note creation - create event
Showing
12 changed files
with
130 additions
and
25 deletions
Show diff stats
... | ... | @@ -0,0 +1,12 @@ |
1 | +class ActivityObserver < ActiveRecord::Observer | |
2 | + observe :issue, :merge_request, :note | |
3 | + | |
4 | + def after_create(record) | |
5 | + Event.create( | |
6 | + :project => record.project, | |
7 | + :target_id => record.id, | |
8 | + :target_type => record.class.name, | |
9 | + :action => Event.determine_action(record) | |
10 | + ) | |
11 | + end | |
12 | +end | ... | ... |
app/models/event.rb
1 | 1 | class Event < ActiveRecord::Base |
2 | + Created = 1 | |
3 | + Updated = 2 | |
4 | + Closed = 3 | |
5 | + Reopened = 4 | |
6 | + Pushed = 5 | |
7 | + Commented = 6 | |
8 | + | |
2 | 9 | belongs_to :project |
10 | + belongs_to :target, :polymorphic => true | |
11 | + | |
3 | 12 | serialize :data |
13 | + | |
14 | + def self.determine_action(record) | |
15 | + if [Issue, MergeRequest].include? record.class | |
16 | + Event::Created | |
17 | + elsif record.kind_of? Note | |
18 | + Event::Commented | |
19 | + end | |
20 | + end | |
4 | 21 | end |
5 | 22 | # == Schema Information |
6 | 23 | # |
7 | 24 | # Table name: events |
8 | 25 | # |
9 | -# id :integer not null, primary key | |
10 | -# data_type :string(255) | |
11 | -# data_id :string(255) | |
12 | -# title :string(255) | |
13 | -# data :text | |
14 | -# project_id :integer | |
15 | -# created_at :datetime not null | |
16 | -# updated_at :datetime not null | |
26 | +# id :integer not null, primary key | |
27 | +# target_type :string(255) | |
28 | +# target_id :integer | |
29 | +# title :string(255) | |
30 | +# data :text | |
31 | +# project_id :integer | |
32 | +# created_at :datetime not null | |
33 | +# updated_at :datetime not null | |
34 | +# action :integer | |
17 | 35 | # |
36 | + | ... | ... |
app/models/project.rb
... | ... | @@ -3,6 +3,7 @@ require "grit" |
3 | 3 | class Project < ActiveRecord::Base |
4 | 4 | belongs_to :owner, :class_name => "User" |
5 | 5 | |
6 | + has_many :events, :dependent => :destroy | |
6 | 7 | has_many :merge_requests, :dependent => :destroy |
7 | 8 | has_many :issues, :dependent => :destroy, :order => "position" |
8 | 9 | has_many :users_projects, :dependent => :destroy | ... | ... |
app/models/wiki.rb
config/application.rb
... | ... | @@ -23,7 +23,7 @@ module Gitlab |
23 | 23 | # config.plugins = [ :exception_notification, :ssl_requirement, :all ] |
24 | 24 | |
25 | 25 | # Activate observers that should always be running. |
26 | - config.active_record.observers = :mailer_observer | |
26 | + config.active_record.observers = :mailer_observer, :activity_observer | |
27 | 27 | |
28 | 28 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. |
29 | 29 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. | ... | ... |
db/migrate/20120228130210_create_events.rb
1 | 1 | class CreateEvents < ActiveRecord::Migration |
2 | 2 | def change |
3 | 3 | create_table :events do |t| |
4 | - t.string :data_type, :null => true | |
5 | - t.string :data_id, :null => true | |
4 | + t.string :target_type, :null => true | |
5 | + t.integer :target_id, :null => true | |
6 | + | |
6 | 7 | t.string :title, :null => true |
7 | 8 | t.text :data, :null => true |
8 | 9 | t.integer :project_id, :null => true | ... | ... |
db/schema.rb
... | ... | @@ -11,16 +11,17 @@ |
11 | 11 | # |
12 | 12 | # It's strongly recommended to check this file into your version control system. |
13 | 13 | |
14 | -ActiveRecord::Schema.define(:version => 20120228130210) do | |
14 | +ActiveRecord::Schema.define(:version => 20120228134252) do | |
15 | 15 | |
16 | 16 | create_table "events", :force => true do |t| |
17 | - t.string "data_type" | |
18 | - t.string "data_id" | |
17 | + t.string "target_type" | |
18 | + t.integer "target_id" | |
19 | 19 | t.string "title" |
20 | 20 | t.text "data" |
21 | 21 | t.integer "project_id" |
22 | - t.datetime "created_at", :null => false | |
23 | - t.datetime "updated_at", :null => false | |
22 | + t.datetime "created_at", :null => false | |
23 | + t.datetime "updated_at", :null => false | |
24 | + t.integer "action" | |
24 | 25 | end |
25 | 26 | |
26 | 27 | create_table "issues", :force => true do |t| | ... | ... |
spec/factories.rb
... | ... | @@ -32,10 +32,14 @@ end |
32 | 32 | |
33 | 33 | Factory.add(:issue, Issue) do |obj| |
34 | 34 | obj.title = Faker::Lorem.sentence |
35 | + obj.author = Factory :user | |
36 | + obj.assignee = Factory :user | |
35 | 37 | end |
36 | 38 | |
37 | 39 | Factory.add(:merge_request, MergeRequest) do |obj| |
38 | 40 | obj.title = Faker::Lorem.sentence |
41 | + obj.author = Factory :user | |
42 | + obj.assignee = Factory :user | |
39 | 43 | obj.source_branch = "master" |
40 | 44 | obj.target_branch = "master" |
41 | 45 | obj.closed = false |
... | ... | @@ -64,3 +68,8 @@ Factory.add(:wikis, WebHook) do |obj| |
64 | 68 | obj.title = Faker::Lorem.sentence |
65 | 69 | obj.content = Faker::Lorem.sentence |
66 | 70 | end |
71 | + | |
72 | +Factory.add(:event, Event) do |obj| | |
73 | + obj.title = Faker::Lorem.sentence | |
74 | + obj.project = Factory(:project) | |
75 | +end | ... | ... |
... | ... | @@ -0,0 +1,42 @@ |
1 | +require 'spec_helper' | |
2 | + | |
3 | +describe ActivityObserver do | |
4 | + let(:project) { Factory :project } | |
5 | + | |
6 | + describe "Merge Request created" do | |
7 | + before do | |
8 | + @merge_request = Factory :merge_request, :project => project | |
9 | + @event = Event.last | |
10 | + end | |
11 | + | |
12 | + it { @event.should_not be_nil } | |
13 | + it { @event.project.should == project } | |
14 | + it { @event.action.should == Event::Created } | |
15 | + it { @event.target.should == @merge_request } | |
16 | + end | |
17 | + | |
18 | + describe "Issue created" do | |
19 | + before do | |
20 | + @issue = Factory :issue, :project => project | |
21 | + @event = Event.last | |
22 | + end | |
23 | + | |
24 | + it { @event.should_not be_nil } | |
25 | + it { @event.project.should == project } | |
26 | + it { @event.action.should == Event::Created } | |
27 | + it { @event.target.should == @issue } | |
28 | + end | |
29 | + | |
30 | + describe "Issue commented" do | |
31 | + before do | |
32 | + @issue = Factory :issue, :project => project | |
33 | + @note = Factory :note, :noteable => @issue, :project => project | |
34 | + @event = Event.last | |
35 | + end | |
36 | + | |
37 | + it { @event.should_not be_nil } | |
38 | + it { @event.project.should == project } | |
39 | + it { @event.action.should == Event::Commented } | |
40 | + it { @event.target.should == @note } | |
41 | + end | |
42 | +end | ... | ... |
spec/models/event_spec.rb
... | ... | @@ -2,18 +2,31 @@ |
2 | 2 | # |
3 | 3 | # Table name: events |
4 | 4 | # |
5 | -# id :integer not null, primary key | |
6 | -# data_type :string(255) | |
7 | -# data_id :string(255) | |
8 | -# title :string(255) | |
9 | -# data :text | |
10 | -# project_id :integer | |
11 | -# created_at :datetime not null | |
12 | -# updated_at :datetime not null | |
5 | +# id :integer not null, primary key | |
6 | +# target_type :string(255) | |
7 | +# target_id :integer | |
8 | +# title :string(255) | |
9 | +# data :text | |
10 | +# project_id :integer | |
11 | +# created_at :datetime not null | |
12 | +# updated_at :datetime not null | |
13 | +# action :integer | |
13 | 14 | # |
14 | 15 | |
15 | 16 | require 'spec_helper' |
16 | 17 | |
17 | 18 | describe Event do |
18 | - pending "add some examples to (or delete) #{__FILE__}" | |
19 | + describe "Associations" do | |
20 | + it { should belong_to(:project) } | |
21 | + end | |
22 | + | |
23 | + describe "Creation" do | |
24 | + before do | |
25 | + @event = Factory :event | |
26 | + end | |
27 | + | |
28 | + it "should create a valid event" do | |
29 | + @event.should be_valid | |
30 | + end | |
31 | + end | |
19 | 32 | end | ... | ... |
spec/models/project_spec.rb