Skip to content

Commit

Permalink
Notifiers now use ActiveRecord::Observer
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexDenisov committed Jul 9, 2012
1 parent e4fcaea commit 15de129
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 11 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -16,4 +16,3 @@
*.swp
*.DS_Store
public/assets/*
config/quotepad.yml
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -9,6 +9,7 @@ gem 'gravatar_image_tag'
gem 'cancan'
gem 'kaminari'
gem 'coffee-rails', '~> 3.2.1'
gem 'resque'
gem 'resque_mailer'

group :production do
Expand All @@ -25,7 +26,7 @@ end
group :test do
gem 'email_spec'
gem 'resque_spec'
gem 'cucumber-rails'
gem 'cucumber-rails', :require => nil
gem 'rspec-rails'
gem 'database_cleaner'
gem 'guard-cucumber'
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Expand Up @@ -272,6 +272,7 @@ DEPENDENCIES
jquery-rails
kaminari
rails (= 3.2.6)
resque
resque_mailer
resque_spec
rspec-rails
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/excerpts_controller.rb
Expand Up @@ -42,7 +42,7 @@ def update
def create
@excerpt = current_user.excerpts.new params[:excerpt]
if @excerpt.save
Resque.enqueue(Notifier, @excerpt.id)
#Resque.enqueue(Notifier, @excerpt.id)
redirect_to excerpts_path,
:notice => t(:excerpt_created_successfully)
else
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Expand Up @@ -21,7 +21,7 @@ def new
def create
@user = User.new params[:user]
if @user.save
UserMailer.welcome(@user, params[:user][:password]).deliver unless Rails.env == "test"
#UserMailer.welcome(@user, params[:user][:password]).deliver unless Rails.env == "test"
redirect_to users_path
else
@legend_title = t :new_user
Expand Down
6 changes: 3 additions & 3 deletions app/mailers/notifier.rb
Expand Up @@ -3,13 +3,13 @@ class Notifier < ActionMailer::Base

default from: "from@example.com"

def new_user(user_id, password)
@user = User.find user_id
def new_user(email, password)
@email = email
@password = password
@url = APP_CONFIG['url']
@service = APP_CONFIG['service_name']
subject = "#{t(:welcome_to)} #{APP_CONFIG['service_name']}"
mail(:to => @user.email, :subject => subject)
mail(:to => email, :subject => subject)
end

def new_excerpt(user_id, excerpt_id)
Expand Down
7 changes: 7 additions & 0 deletions app/observers/excerpt_observer.rb
@@ -0,0 +1,7 @@
class ExcerptObserver < ActiveRecord::Observer
def after_create(excerpt)
User.subscribers.each do | subscriber |
Notifier.new_excerpt(subscriber.id, excerpt.id).deliver
end
end
end
6 changes: 6 additions & 0 deletions app/observers/user_observer.rb
@@ -0,0 +1,6 @@
class UserObserver < ActiveRecord::Observer
def after_create(user)
Notifier.new_user(user.email, user.password).deliver
end
end

File renamed without changes.
Expand Up @@ -4,6 +4,6 @@ div
div
| Use this data to sign in
p
| Email: #{@user.email}
| Email: #{@email}
p
| Password: #{@password}
3 changes: 2 additions & 1 deletion config/application.rb
Expand Up @@ -23,7 +23,8 @@ class Application < Rails::Application
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]

# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# config.active_record.observers = :user_observer, :excerpt_observer
config.active_record.observers = :user_observer, :excerpt_observer unless Rails.env == 'test'

# 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.
Expand Down
1 change: 1 addition & 0 deletions config/environments/development.rb
Expand Up @@ -13,6 +13,7 @@
config.consider_all_requests_local = true
config.action_controller.perform_caching = false


# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default_url_options = { :host => APP_CONFIG['url'] }
Expand Down
2 changes: 1 addition & 1 deletion config/quotepad.yml
Expand Up @@ -2,6 +2,6 @@ service_name: QuotePad
smtp_user:
smtp_password:
mailer_from: admin@quote.pad
url:
url:


2 changes: 1 addition & 1 deletion spec/mailers/notifier_spec.rb
Expand Up @@ -16,7 +16,7 @@
end

describe "new_user" do
let(:mail) { Notifier.new_user(@recipient.id, @recipient.password) }
let(:mail) { Notifier.new_user(@recipient.email, @recipient.password) }

it "should sent new user email" do
mail.should deliver_to @recipient.email
Expand Down
29 changes: 29 additions & 0 deletions spec/models/user_spec.rb
@@ -0,0 +1,29 @@
require 'spec_helper'

describe User do
it { should have_many :excerpts }
it { should belong_to :role }
it { should validate_presence_of :role_id }
describe "should have correct display_name" do
before(:each) do
@email = "fuu@bar.baz"
@user = FactoryGirl.create :user, :email => @email
end
it "without nickname" do
@user.nickname = nil
@user.display_name == @email
end
it "within nickname" do
nickname = "alexd"
@user.nickname = nickname
@user.display_name == nickname
end
it "within empty nickname" do
nickname = ""
@user.display_name == @email
end

end
end


0 comments on commit 15de129

Please sign in to comment.