Merge pull request #131 from Marsupio/mailers

Mailers
This commit is contained in:
Saverio Trioni
2015-02-27 02:01:40 +01:00
12 changed files with 157 additions and 1 deletions

View File

@ -12,7 +12,7 @@ timeoverflow:
# delayed_job: 1
# sidekiq: 1
# clockwork: on
# whenever: on
whenever: on
# elasticsearch: on
databases:
- postgresql

View File

@ -61,6 +61,8 @@ gem 'fabrication'
gem "shelly-dependencies"
gem 'whenever', :require => false
group :development do
gem "binding_of_caller"
gem "better_errors"

View File

@ -72,6 +72,7 @@ GEM
rack-test (>= 0.5.4)
xpath (~> 2.0)
choice (0.1.6)
chronic (0.10.2)
coderay (1.1.0)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
@ -283,6 +284,8 @@ GEM
multi_json (~> 1.0, >= 1.0.2)
warden (1.2.3)
rack (>= 1.0)
whenever (0.9.4)
chronic (>= 0.6.3)
xpath (2.0.0)
nokogiri (~> 1.3)
@ -336,3 +339,4 @@ DEPENDENCIES
travis-lint
turbolinks
uglifier (>= 1.0.3)
whenever

View File

@ -0,0 +1,20 @@
class OrganizationNotifier < ActionMailer::Base
default from: "\"TimeOverflow\" <info@timeoverflow.org>"
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.organization_notifier.recent_posts.subject
#
def recent_posts(posts)
# last 10 posts of offers and inquiries
@offers = posts.where(type: "Offer").take(10)
@inquiries = posts.where(type: "Inquiry").take(10)
@organization_name = posts.take.organization.name
# users with email ok
emails = posts.take.organization.users.online_active.actives.pluck(:email)
mail(bcc: emails)
end
end

View File

@ -7,6 +7,7 @@ class Post < ActiveRecord::Base
attr_reader :member_id
belongs_to :category
delegate :name, to: :category, prefix: true, allow_nil: true
belongs_to :user
belongs_to :organization
belongs_to :publisher, class_name: "User", foreign_key: "publisher_id"
@ -42,6 +43,10 @@ class Post < ActiveRecord::Base
) #{Post.table_name}")
}
scope :from_last_week, -> {
where("created_at >= ?", 1.week.ago.beginning_of_day)
}
validates :user, presence: true
validates :category, presence: true

View File

@ -20,6 +20,7 @@ class User < ActiveRecord::Base
default_scope -> { order("users.id ASC") }
scope :actives, -> { where(members: { active: true }) }
scope :online_active, -> { where("sign_in_count > 0") }
validates :username, presence: true
validates :email, presence: true, uniqueness: true

View File

@ -0,0 +1,14 @@
class OrganizationNotifierService
def initialize(organization)
@organization = organization
end
def send_recent_posts_to_online_members
@organization.each do |org|
posts = org.posts.from_last_week
if posts.present?
OrganizationNotifier.recent_posts(posts).deliver
end
end
end
end

View File

@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<% if @offers.present? %>
<p>
<%= t("organization_notifier.recent_posts.text1") %>
</p>
<ul>
<% @offers.each do |offer| %>
<li>
<a href="<%= offer_url(offer.id)%>">
<%= "#{offer.title} (#{offer.try(&:category_name)})" %>
</a>
</li>
<% end %>
</ul>
<% end %>
<% if @inquiries.present? %>
<p>
<%= t("organization_notifier.recent_posts.text2") %>
</p>
<ul>
<% @inquiries.each do |inquiry| %>
<li>
<a href="<%= inquiry_url(inquiry.id)%>">
<%= "#{inquiry.title} (#{inquiry.try(&:category_name)})" %>
</a>
</li>
<% end %>
</ul>
<% end %>
--
<p>
<%= t("mailers_globals.footer.text", organization_name: @organization_name) %>
<a href="<%= page_url('home', only_path: false) %>">
TimeOverflow
<a>
</p>
</body>
</html>

View File

@ -0,0 +1,10 @@
ca:
mailers_globals:
footer:
text: "%{organization_name} en"
organization_notifier:
recent_posts:
subject: Boletín semanal
text1: "Últimas ofertas publicadas:"
text2: "Últimas demandas publicadas:"

View File

@ -0,0 +1,10 @@
en:
mailers_globals:
footer:
text: "%{organization_name} from"
organization_notifier:
recent_posts:
subject: Newsletter
text1: "Latest offers published:"
text2: "Lastest inquiries published:"

View File

@ -0,0 +1,10 @@
es:
mailers_globals:
footer:
text: "%{organization_name} en"
organization_notifier:
recent_posts:
subject: Boletín semanal
text1: "Últimas ofertas publicadas:"
text2: "Últimas demandas publicadas:"

34
config/schedule.rb Normal file
View File

@ -0,0 +1,34 @@
set :environment, "production"
set :output, "log/cron_log.log"
every :monday, at: "9am" do
runner "OrganizationNotifierService.new(Organization.all).
send_recent_posts_to_online_members"
end
# Cada vez que se modifique este archivo, crontab debe actualizarse:
# whenever --update-crontab
# Use this file to easily define all of your cron jobs.
#
# It's helpful, but not entirely necessary to understand cron before proceeding.
# http://en.wikipedia.org/wiki/Cron
# Example:
#
# set :output, "/path/to/my/cron_log.log"
#
# every 2.hours do
# command "/usr/bin/some_great_command"
# runner "MyModel.some_method"
# rake "some:great:rake:task"
# end
#
# every 4.days do
# runner "AnotherModel.prune_old_records"
# end
# Learn more: http://github.com/javan/whenever
# rake "pruebas:hello" #task en lib/tasks para comprobar el funcionamiento de
# whenever