user_notifier 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/mailers/user_notifier/{base_mailer.rb → mailer.rb} +2 -2
- data/app/models/user_notifier/base.rb +13 -6
- data/app/workers/user_notifier/email_worker.rb +1 -1
- data/lib/generators/user_notifier/notification/USAGE +2 -0
- data/lib/generators/user_notifier/notification/notification_generator.rb +26 -0
- data/lib/generators/user_notifier/notification/templates/migration.rb +13 -0
- data/lib/user_notifier/configuration.rb +1 -1
- data/lib/user_notifier/models/has_notifications.rb +16 -3
- data/lib/user_notifier/version.rb +1 -1
- data/spec/dummy/app/models/order.rb +3 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20140709164530_create_orders.rb +10 -0
- data/spec/dummy/db/migrate/20140709170259_create_order_notifications.rb +13 -0
- data/spec/dummy/db/schema.rb +18 -1
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +76 -0
- data/spec/dummy/log/test.log +5684 -0
- data/spec/models/user_notifier/base_spec.rb +37 -5
- metadata +12 -4
- data/README.rdoc +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 962bf40c71ba6c53d8295649321c24be3121e235
|
4
|
+
data.tar.gz: 00d063c185a4135daade55cc0cb00fbb7774711b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4f8191e482b2e57cd4f9c4e152935edbde8a916e2e7756effff3c58346b615024a8c37c07a333fd346be5172c4efcff58840a57d74af081b02c12b60e0b8107
|
7
|
+
data.tar.gz: bb017c0c6cd815b97b586108781c806410471da0c20f9b43cf2ea68f59f919a16d95941fb565d768b6a0176fcf6559d3795b63453839aeb8a8de28b931cbeb69
|
@@ -1,11 +1,11 @@
|
|
1
|
-
class UserNotifier::
|
1
|
+
class UserNotifier::Mailer < ActionMailer::Base
|
2
2
|
layout UserNotifier.email_layout
|
3
3
|
|
4
4
|
def notify(notification)
|
5
5
|
@notification = notification
|
6
6
|
old_locale = I18n.locale
|
7
7
|
I18n.locale = @notification.locale
|
8
|
-
subject = render_to_string(template: "
|
8
|
+
subject = render_to_string(template: "user_notifier/mailer/#{@notification.template_name}_subject")
|
9
9
|
m = mail({
|
10
10
|
from: address_format(UserNotifier.system_email, @notification.from_name),
|
11
11
|
reply_to: address_format(@notification.from_email, @notification.from_name),
|
@@ -1,17 +1,19 @@
|
|
1
1
|
class UserNotifier::Base < ActiveRecord::Base
|
2
2
|
self.abstract_class = true
|
3
|
-
belongs_to UserNotifier.user_class_name
|
4
3
|
|
5
|
-
def self.notify_once(template_name, user,
|
6
|
-
notify(template_name, user, params) if is_unique?(template_name,
|
4
|
+
def self.notify_once(template_name, user, source = nil, params = {})
|
5
|
+
notify(template_name, user, source, params) if is_unique?(template_name, {self.user_association_name => user})
|
7
6
|
end
|
8
7
|
|
9
|
-
def self.notify(template_name, user, params = {})
|
8
|
+
def self.notify(template_name, user, source = nil, params = {})
|
9
|
+
source ||= user
|
10
10
|
create!({
|
11
11
|
template_name: template_name,
|
12
12
|
locale: I18n.locale,
|
13
13
|
from_email: UserNotifier.from_email,
|
14
|
-
from_name: UserNotifier.from_name
|
14
|
+
from_name: UserNotifier.from_name,
|
15
|
+
source: source,
|
16
|
+
self.user_association_name => user
|
15
17
|
}.merge(params)).tap{|n| n.deliver }
|
16
18
|
end
|
17
19
|
|
@@ -21,10 +23,15 @@ class UserNotifier::Base < ActiveRecord::Base
|
|
21
23
|
|
22
24
|
def deliver!
|
23
25
|
UserNotifier::EmailWorker.perform_async(self.class.name.to_s, self.id)
|
24
|
-
self.update_attributes sent_at: Time.now()
|
25
26
|
end
|
26
27
|
|
27
28
|
private
|
29
|
+
def self.user_association_name
|
30
|
+
UserNotifier.user_class_name.downcase.to_sym
|
31
|
+
end
|
32
|
+
|
33
|
+
belongs_to user_association_name
|
34
|
+
|
28
35
|
def self.is_unique?(template_name, filter)
|
29
36
|
filter.nil? || self.where(filter.merge(template_name: template_name)).empty?
|
30
37
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
|
3
|
+
module UserNotifier
|
4
|
+
module Generators
|
5
|
+
class NotificationGenerator < ActiveRecord::Generators::Base
|
6
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
7
|
+
|
8
|
+
def notifications_table_name
|
9
|
+
"#{table_name.singularize}_notifications"
|
10
|
+
end
|
11
|
+
|
12
|
+
def copy_migration
|
13
|
+
migration_template "migration.rb", "db/migrate/create_#{notifications_table_name}.rb"
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def user_model_name
|
18
|
+
UserNotifier.user_class_name.downcase
|
19
|
+
end
|
20
|
+
|
21
|
+
def is_user_model?
|
22
|
+
table_name.singularize == user_model_name
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Create<%= notifications_table_name.camelize %> < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :<%= notifications_table_name %> do |t|
|
4
|
+
t.integer :<%= user_model_name %>_id, null: false
|
5
|
+
<%= "t.integer :#{table_name.singularize}_id, null: false" unless is_user_model? %>
|
6
|
+
t.text :from_email, null: false
|
7
|
+
t.text :from_name, null: false
|
8
|
+
t.text :template_name, null: false
|
9
|
+
t.text :locale, null: false
|
10
|
+
t.timestamp :sent_at
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -3,9 +3,16 @@ module UserNotifier
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
def notify template_name, user=nil
|
6
|
+
def notify template_name, user=nil, source=nil, params={}
|
7
7
|
user ||= self
|
8
|
-
self
|
8
|
+
source ||= self
|
9
|
+
self.notifications.notify(template_name, user, source, params)
|
10
|
+
end
|
11
|
+
|
12
|
+
def notify_once template_name, user=nil, source=nil, params={}
|
13
|
+
user ||= self
|
14
|
+
source ||= self
|
15
|
+
self.notifications.notify_once(template_name, user, source, params)
|
9
16
|
end
|
10
17
|
end
|
11
18
|
|
@@ -13,7 +20,7 @@ module UserNotifier
|
|
13
20
|
private
|
14
21
|
def set_association
|
15
22
|
create_notification_class
|
16
|
-
self.has_many :notifications, class_name: notification_class_name
|
23
|
+
self.has_many :notifications, class_name: notification_class_name, dependent: :destroy
|
17
24
|
end
|
18
25
|
|
19
26
|
def notification_class_name
|
@@ -27,6 +34,12 @@ module UserNotifier
|
|
27
34
|
self.table_name = base_class_name.tableize
|
28
35
|
end
|
29
36
|
self.parent.const_set base_class_name, klass
|
37
|
+
|
38
|
+
source_name = self.table_name.singularize
|
39
|
+
if self.model_name.to_s.downcase != UserNotifier.user_class_name.downcase
|
40
|
+
klass.belongs_to source_name.to_sym, inverse_of: :notifications
|
41
|
+
end
|
42
|
+
klass.belongs_to :source, class_name: self.model_name.to_s, foreign_key: "#{source_name}_id", inverse_of: :notifications
|
30
43
|
end
|
31
44
|
end
|
32
45
|
end
|
Binary file
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class CreateOrderNotifications < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :order_notifications do |t|
|
4
|
+
t.integer :user_id, null: false
|
5
|
+
t.integer :order_id, null: false
|
6
|
+
t.text :from_email, null: false
|
7
|
+
t.text :from_name, null: false
|
8
|
+
t.text :template_name, null: false
|
9
|
+
t.text :locale, null: false
|
10
|
+
t.timestamp :sent_at
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -11,7 +11,24 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended that you check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(version:
|
14
|
+
ActiveRecord::Schema.define(version: 20140709170259) do
|
15
|
+
|
16
|
+
create_table "order_notifications", force: true do |t|
|
17
|
+
t.integer "user_id", null: false
|
18
|
+
t.integer "order_id", null: false
|
19
|
+
t.text "from_email", null: false
|
20
|
+
t.text "from_name", null: false
|
21
|
+
t.text "template_name", null: false
|
22
|
+
t.text "locale", null: false
|
23
|
+
t.datetime "sent_at"
|
24
|
+
end
|
25
|
+
|
26
|
+
create_table "orders", force: true do |t|
|
27
|
+
t.integer "user_id"
|
28
|
+
t.text "title"
|
29
|
+
t.datetime "created_at"
|
30
|
+
t.datetime "updated_at"
|
31
|
+
end
|
15
32
|
|
16
33
|
create_table "user_notifications", force: true do |t|
|
17
34
|
t.integer "user_id"
|
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|
@@ -31,3 +31,79 @@ Migrating to CreateUserNotifications (20140625183616)
|
|
31
31
|
[1m[35m (0.2ms)[0m SELECT version FROM "schema_migrations"
|
32
32
|
[1m[36m (166.5ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140625183616')[0m
|
33
33
|
[1m[35m (178.1ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140625163719')
|
34
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.2ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
35
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.3ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
36
|
+
[1m[36m (350.4ms)[0m [1mCREATE TABLE "user_notifications" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "from_email" text, "from_name" text, "template_name" text, "locale" text, "sent_at" datetime) [0m
|
37
|
+
[1m[35m (165.6ms)[0m CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" text, "email" text, "created_at" datetime, "updated_at" datetime)
|
38
|
+
[1m[36m (167.7ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
39
|
+
[1m[35m (0.2ms)[0m select sqlite_version(*)
|
40
|
+
[1m[36m (154.7ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
41
|
+
[1m[35m (0.3ms)[0m SELECT version FROM "schema_migrations"
|
42
|
+
[1m[36m (154.9ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140625183616')[0m
|
43
|
+
[1m[35m (167.1ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140625163719')
|
44
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.2ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
45
|
+
Migrating to CreateOrders (20140709164530)
|
46
|
+
[1m[35m (0.1ms)[0m begin transaction
|
47
|
+
[1m[36m (0.6ms)[0m [1mCREATE TABLE "orders" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "title" text, "created_at" datetime, "updated_at" datetime) [0m
|
48
|
+
[1m[35mSQL (0.2ms)[0m INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20140709164530"]]
|
49
|
+
[1m[36m (205.2ms)[0m [1mcommit transaction[0m
|
50
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
51
|
+
[1m[36m (154.0ms)[0m [1mCREATE TABLE "orders" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "title" text, "created_at" datetime, "updated_at" datetime) [0m
|
52
|
+
[1m[35m (165.5ms)[0m CREATE TABLE "user_notifications" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "from_email" text, "from_name" text, "template_name" text, "locale" text, "sent_at" datetime)
|
53
|
+
[1m[36m (165.5ms)[0m [1mCREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" text, "email" text, "created_at" datetime, "updated_at" datetime) [0m
|
54
|
+
[1m[35m (167.7ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
55
|
+
[1m[36m (0.2ms)[0m [1mselect sqlite_version(*)[0m
|
56
|
+
[1m[35m (165.7ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
57
|
+
[1m[36m (0.4ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
58
|
+
[1m[35m (166.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140709164530')
|
59
|
+
[1m[36m (166.9ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140625183616')[0m
|
60
|
+
[1m[35m (189.6ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140625163719')
|
61
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.2ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
62
|
+
Migrating to CreateOrderNotifications (20140709170259)
|
63
|
+
[1m[35m (0.1ms)[0m begin transaction
|
64
|
+
[1m[36m (0.6ms)[0m [1mCREATE TABLE "order_notifications" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer NOT NULL, "order_id" integer NOT NULL, "from_email" text NOT NULL, "from_name" text NOT NULL, "template_name" text NOT NULL, "locale" text NOT NULL, "sent_at" datetime) [0m
|
65
|
+
[1m[35mSQL (0.3ms)[0m INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20140709170259"]]
|
66
|
+
[1m[36m (183.2ms)[0m [1mcommit transaction[0m
|
67
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.3ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
68
|
+
[1m[36m (168.3ms)[0m [1mCREATE TABLE "order_notifications" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer NOT NULL, "order_id" integer NOT NULL, "from_email" text NOT NULL, "from_name" text NOT NULL, "template_name" text NOT NULL, "locale" text NOT NULL, "sent_at" datetime) [0m
|
69
|
+
[1m[35m (165.5ms)[0m CREATE TABLE "orders" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "title" text, "created_at" datetime, "updated_at" datetime)
|
70
|
+
[1m[36m (143.2ms)[0m [1mCREATE TABLE "user_notifications" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "from_email" text, "from_name" text, "template_name" text, "locale" text, "sent_at" datetime) [0m
|
71
|
+
[1m[35m (143.2ms)[0m CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" text, "email" text, "created_at" datetime, "updated_at" datetime)
|
72
|
+
[1m[36m (143.8ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) [0m
|
73
|
+
[1m[35m (0.2ms)[0m select sqlite_version(*)
|
74
|
+
[1m[36m (143.5ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
75
|
+
[1m[35m (0.4ms)[0m SELECT version FROM "schema_migrations"
|
76
|
+
[1m[36m (154.8ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140709170259')[0m
|
77
|
+
[1m[35m (166.9ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140709164530')
|
78
|
+
[1m[36m (167.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20140625183616')[0m
|
79
|
+
[1m[35m (167.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20140625163719')
|
80
|
+
[1m[36mUser Load (1.8ms)[0m [1mSELECT "users".* FROM "users"[0m
|
81
|
+
[1m[35m (0.2ms)[0m begin transaction
|
82
|
+
[1m[36mSQL (0.5ms)[0m [1mINSERT INTO "users" ("created_at", "email", "updated_at") VALUES (?, ?, ?)[0m [["created_at", "2014-07-09 21:02:55.250061"], ["email", "teste@gmail.com"], ["updated_at", "2014-07-09 21:02:55.250061"]]
|
83
|
+
[1m[35m (174.8ms)[0m commit transaction
|
84
|
+
[1m[36mUser Load (0.4ms)[0m [1mSELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1[0m
|
85
|
+
[1m[35m (0.2ms)[0m begin transaction
|
86
|
+
[1m[36mSQL (0.4ms)[0m [1mINSERT INTO "orders" ("created_at", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?)[0m [["created_at", "2014-07-09 21:03:20.607453"], ["title", "teste"], ["updated_at", "2014-07-09 21:03:20.607453"], ["user_id", 1]]
|
87
|
+
[1m[35m (138.1ms)[0m commit transaction
|
88
|
+
[1m[36mOrderNotification Load (0.2ms)[0m [1mSELECT "order_notifications".* FROM "order_notifications" WHERE "order_notifications"."order_id" = ?[0m [["order_id", 1]]
|
89
|
+
[1m[35m (0.2ms)[0m begin transaction
|
90
|
+
[1m[36mSQL (0.8ms)[0m [1mINSERT INTO "order_notifications" ("from_email", "from_name", "locale", "order_id", "template_name", "user_id") VALUES (?, ?, ?, ?, ?, ?)[0m [["from_email", "no-reply@yourdomain"], ["from_name", "please configure a default from name"], ["locale", "en"], ["order_id", 1], ["template_name", "test"], ["user_id", 1]]
|
91
|
+
[1m[35m (186.2ms)[0m commit transaction
|
92
|
+
[1m[36mOrderNotification Load (0.5ms)[0m [1mSELECT "order_notifications".* FROM "order_notifications"[0m
|
93
|
+
[1m[35mOrderNotification Load (0.6ms)[0m SELECT "order_notifications".* FROM "order_notifications"
|
94
|
+
[1m[36mOrderNotification Load (0.5ms)[0m [1mSELECT "order_notifications".* FROM "order_notifications" ORDER BY "order_notifications"."id" ASC LIMIT 1[0m
|
95
|
+
[1m[35mOrderNotification Load (0.6ms)[0m SELECT "order_notifications".* FROM "order_notifications" ORDER BY "order_notifications"."id" ASC LIMIT 1
|
96
|
+
[1m[36mOrderNotification Load (0.6ms)[0m [1mSELECT "order_notifications".* FROM "order_notifications" ORDER BY "order_notifications"."id" ASC LIMIT 1[0m
|
97
|
+
[1m[35mOrderNotification Load (0.5ms)[0m SELECT "order_notifications".* FROM "order_notifications" ORDER BY "order_notifications"."id" ASC LIMIT 1
|
98
|
+
[1m[36mOrderNotification Load (0.4ms)[0m [1mSELECT "order_notifications".* FROM "order_notifications" ORDER BY "order_notifications"."id" ASC LIMIT 1[0m
|
99
|
+
[1m[36mOrder Load (0.6ms)[0m [1mSELECT "orders".* FROM "orders" ORDER BY "orders"."id" ASC LIMIT 1[0m
|
100
|
+
[1m[35mOrder Load (0.6ms)[0m SELECT "orders".* FROM "orders" ORDER BY "orders"."id" ASC LIMIT 1
|
101
|
+
[1m[36mOrderNotification Load (0.3ms)[0m [1mSELECT "order_notifications".* FROM "order_notifications" WHERE "order_notifications"."order_id" = ?[0m [["order_id", 1]]
|
102
|
+
[1m[36mOrder Load (0.2ms)[0m [1mSELECT "orders".* FROM "orders" ORDER BY "orders"."id" ASC LIMIT 1[0m
|
103
|
+
[1m[35mOrderNotification Load (0.3ms)[0m SELECT "order_notifications".* FROM "order_notifications" WHERE "order_notifications"."order_id" = ? [["order_id", 1]]
|
104
|
+
[1m[36mOrder Load (0.3ms)[0m [1mSELECT "orders".* FROM "orders" ORDER BY "orders"."id" ASC LIMIT 1[0m
|
105
|
+
[1m[35mOrderNotification Load (0.6ms)[0m SELECT "order_notifications".* FROM "order_notifications" WHERE "order_notifications"."order_id" = ? ORDER BY "order_notifications"."id" ASC LIMIT 1 [["order_id", 1]]
|
106
|
+
[1m[36mOrder Load (0.6ms)[0m [1mSELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1[0m [["id", 1]]
|
107
|
+
[1m[36mOrder Load (0.3ms)[0m [1mSELECT "orders".* FROM "orders" ORDER BY "orders"."id" ASC LIMIT 1[0m
|
108
|
+
[1m[35mOrderNotification Load (0.3ms)[0m SELECT "order_notifications".* FROM "order_notifications" WHERE "order_notifications"."order_id" = ? ORDER BY "order_notifications"."id" ASC LIMIT 1 [["order_id", 1]]
|
109
|
+
[1m[36mOrder Load (0.7ms)[0m [1mSELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1[0m [["id", 1]]
|