From aaeb37419ac00ed065f1c88f617db5788ada8525 Mon Sep 17 00:00:00 2001 From: Alex Denisov <1101.debian@gmail.com> Date: Mon, 27 Aug 2012 00:13:03 +0300 Subject: [PATCH] Send notifiation on create UserProject relation (access granted) --- app/mailers/notify.rb | 7 +++++ app/observers/users_project_observer.rb | 5 ++++ .../project_access_granted_email.html.haml | 14 ++++++++++ spec/factories.rb | 5 ++++ spec/mailers/notify_spec.rb | 20 ++++++++++++++ spec/observers/users_project_observer_spec.rb | 27 +++++++++++++++++++ 6 files changed, 78 insertions(+) create mode 100644 app/observers/users_project_observer.rb create mode 100644 app/views/notify/project_access_granted_email.html.haml create mode 100644 spec/observers/users_project_observer_spec.rb diff --git a/app/mailers/notify.rb b/app/mailers/notify.rb index d0571b7b2c2..1d222eb1a53 100644 --- a/app/mailers/notify.rb +++ b/app/mailers/notify.rb @@ -76,6 +76,13 @@ class Notify < ActionMailer::Base mail(to: recipient(recipient_id), subject: subject("changed issue ##{@issue.id}", @issue.title)) end + def project_access_granted_email(user_project_id) + @users_project = UsersProject.find user_project_id + @project = @users_project.project + mail(to: @users_project.user.email, + subject: subject("access to project was granted")) + end + private # Look up a User by their ID and return their email address diff --git a/app/observers/users_project_observer.rb b/app/observers/users_project_observer.rb new file mode 100644 index 00000000000..cb33eb3ffdd --- /dev/null +++ b/app/observers/users_project_observer.rb @@ -0,0 +1,5 @@ +class UsersProjectObserver < ActiveRecord::Observer + def after_create(users_project) + Notify.project_access_granted_email(users_project.id).deliver + end +end diff --git a/app/views/notify/project_access_granted_email.html.haml b/app/views/notify/project_access_granted_email.html.haml new file mode 100644 index 00000000000..154c2aaa01e --- /dev/null +++ b/app/views/notify/project_access_granted_email.html.haml @@ -0,0 +1,14 @@ +%td.content{align: "left", style: "font-family: Helvetica, Arial, sans-serif; padding: 20px 0 0;", valign: "top", width: "600"} + %table{border: "0", cellpadding: "0", cellspacing: "0", style: "color: #717171; font: normal 11px Helvetica, Arial, sans-serif; margin: 0; padding: 0;", width: "600"} + %tr + %td{style: "font-size: 1px; line-height: 1px;", width: "21"} + %td{align: "left", style: "padding: 20px 0 0;"} + %h2{style: "color:#646464; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "} + = "You got granted #{@users_project.project_access_human} access to project" + %td{style: "font-size: 1px; line-height: 1px;", width: "21"} + %tr + %td{style: "font-size: 1px; line-height: 1px;", width: "21"} + %td{align: "left", style: "padding: 20px 0 0;"} + %h2{style: "color:#646464 !important; font-weight: bold; margin: 0; padding: 0; line-height: 26px; font-size: 18px; font-family: Helvetica, Arial, sans-serif; "} + = link_to_gfm truncate(@project.name, length: 45), project_url(@project), title: @project.name + %br diff --git a/spec/factories.rb b/spec/factories.rb index ab2ca4687da..26868462797 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -90,3 +90,8 @@ Factory.add(:milestone, Milestone) do |obj| obj.title = Faker::Lorem.sentence obj.due_date = Date.today + 1.month end + +Factory.add(:users_project, UsersProject) do |obj| + obj.user = Factory :user + obj.project = Factory :project +end diff --git a/spec/mailers/notify_spec.rb b/spec/mailers/notify_spec.rb index 93427ebfacd..60f3231ce91 100644 --- a/spec/mailers/notify_spec.rb +++ b/spec/mailers/notify_spec.rb @@ -145,6 +145,26 @@ describe Notify do end end + describe 'project access changed' do + let(:project) { Factory.create(:project, + path: "Fuu", + code: "Fuu") } + let(:user) { Factory.create :user } + let(:users_project) { Factory.create(:users_project, + project: project, + user: user) } + subject { Notify.project_access_granted_email(users_project.id) } + it 'has the correct subject' do + should have_subject /access to project was granted/ + end + it 'contains name of project' do + should have_body_text /#{project.name}/ + end + it 'contains new user role' do + should have_body_text /#{users_project.project_access_human}/ + end + end + context 'items that are noteable, the email for a note' do let(:note_author) { Factory.create(:user, name: 'author_name') } let(:note) { Factory.create(:note, project: project, author: note_author) } diff --git a/spec/observers/users_project_observer_spec.rb b/spec/observers/users_project_observer_spec.rb new file mode 100644 index 00000000000..37de9e51ce6 --- /dev/null +++ b/spec/observers/users_project_observer_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe UsersProjectObserver do + let(:user) { Factory.create :user } + let(:project) { Factory.create(:project, + code: "Fuu", + path: "Fuu" ) } + let(:users_project) { Factory.create(:users_project, + project: project, + user: user )} + subject { UsersProjectObserver.instance } + + describe "#after_create" do + it "should called when UsersProject created" do + subject.should_receive(:after_create) + UsersProject.observers.enable :users_project_observer do + Factory.create(:users_project, + project: project, + user: user) + end + end + it "should send email to user" do + Notify.should_receive(:project_access_granted_email).with(users_project.id).and_return(double(deliver: true)) + subject.after_create(users_project) + end + end +end