user_notifier 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 15379a4699aae37560c32fb9dce2e882817893b7
4
- data.tar.gz: fa27fd436b429727aeb069eda60ae2b0759cc489
3
+ metadata.gz: 962bf40c71ba6c53d8295649321c24be3121e235
4
+ data.tar.gz: 00d063c185a4135daade55cc0cb00fbb7774711b
5
5
  SHA512:
6
- metadata.gz: a03d4da19d187480440b1987f394d7ca2ea7151cca55775f671447c1b8bc65ace63d3935c9c8859a9d3d82caa8a2b6054122a467e094f095c08d2de90062e5ce
7
- data.tar.gz: 578fe31fb523895df0fde321e757844338b8d4baa4d28a445c4057b66aa8d1b866c68681acac8050ae4fb1d9ec9b95937f4b2369645da330dc880df19eb1e0da
6
+ metadata.gz: b4f8191e482b2e57cd4f9c4e152935edbde8a916e2e7756effff3c58346b615024a8c37c07a333fd346be5172c4efcff58840a57d74af081b02c12b60e0b8107
7
+ data.tar.gz: bb017c0c6cd815b97b586108781c806410471da0c20f9b43cf2ea68f59f919a16d95941fb565d768b6a0176fcf6559d3795b63453839aeb8a8de28b931cbeb69
@@ -1,11 +1,11 @@
1
- class UserNotifier::BaseMailer < ActionMailer::Base
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: "notifications_mailer/subjects/#{@notification.template_name}")
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, filter, params = {})
6
- notify(template_name, user, params) if is_unique?(template_name, filter)
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
@@ -18,6 +18,6 @@ class UserNotifier::EmailWorker
18
18
  end
19
19
 
20
20
  def mailer
21
- UserNotifier::BaseMailer
21
+ UserNotifier::Mailer
22
22
  end
23
23
  end
@@ -0,0 +1,2 @@
1
+ Description:
2
+ Generates migration for notification model.
@@ -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
@@ -4,7 +4,7 @@ module UserNotifier
4
4
 
5
5
  DEFAULT_SYSTEM_EMAIL = nil
6
6
  DEFAULT_EMAIL_LAYOUT = 'email'
7
- DEFAULT_USER_CLASS_NAME = :user
7
+ DEFAULT_USER_CLASS_NAME = 'User'
8
8
  DEFAULT_FROM_EMAIL = 'no-reply@yourdomain'
9
9
  DEFAULT_FROM_NAME = 'please configure a default from name'
10
10
 
@@ -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.notifications.notify(template_name, user)
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
@@ -1,3 +1,3 @@
1
1
  module UserNotifier
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,3 @@
1
+ class Order < ActiveRecord::Base
2
+ has_notifications
3
+ end
Binary file
@@ -0,0 +1,10 @@
1
+ class CreateOrders < ActiveRecord::Migration
2
+ def change
3
+ create_table :orders do |t|
4
+ t.integer :user_id
5
+ t.text :title
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -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
@@ -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: 20140625183616) do
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"
Binary file
@@ -31,3 +31,79 @@ Migrating to CreateUserNotifications (20140625183616)
31
31
   (0.2ms) SELECT version FROM "schema_migrations"
32
32
   (166.5ms) INSERT INTO "schema_migrations" (version) VALUES ('20140625183616')
33
33
   (178.1ms) INSERT INTO "schema_migrations" (version) VALUES ('20140625163719')
34
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
35
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
36
+  (350.4ms) 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) 
37
+  (165.6ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" text, "email" text, "created_at" datetime, "updated_at" datetime)
38
+  (167.7ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
39
+  (0.2ms) select sqlite_version(*)
40
+  (154.7ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
41
+  (0.3ms) SELECT version FROM "schema_migrations"
42
+  (154.9ms) INSERT INTO "schema_migrations" (version) VALUES ('20140625183616')
43
+  (167.1ms) INSERT INTO "schema_migrations" (version) VALUES ('20140625163719')
44
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
45
+ Migrating to CreateOrders (20140709164530)
46
+  (0.1ms) begin transaction
47
+  (0.6ms) CREATE TABLE "orders" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "title" text, "created_at" datetime, "updated_at" datetime) 
48
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20140709164530"]]
49
+  (205.2ms) commit transaction
50
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
51
+  (154.0ms) CREATE TABLE "orders" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "title" text, "created_at" datetime, "updated_at" datetime) 
52
+  (165.5ms) 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
+  (165.5ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" text, "email" text, "created_at" datetime, "updated_at" datetime) 
54
+  (167.7ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
55
+  (0.2ms) select sqlite_version(*)
56
+  (165.7ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
57
+  (0.4ms) SELECT version FROM "schema_migrations"
58
+  (166.0ms) INSERT INTO "schema_migrations" (version) VALUES ('20140709164530')
59
+  (166.9ms) INSERT INTO "schema_migrations" (version) VALUES ('20140625183616')
60
+  (189.6ms) INSERT INTO "schema_migrations" (version) VALUES ('20140625163719')
61
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
62
+ Migrating to CreateOrderNotifications (20140709170259)
63
+  (0.1ms) begin transaction
64
+  (0.6ms) CREATE 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) 
65
+ SQL (0.3ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20140709170259"]]
66
+  (183.2ms) commit transaction
67
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
68
+  (168.3ms) CREATE 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) 
69
+  (165.5ms) CREATE TABLE "orders" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "title" text, "created_at" datetime, "updated_at" datetime)
70
+  (143.2ms) 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) 
71
+  (143.2ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" text, "email" text, "created_at" datetime, "updated_at" datetime)
72
+  (143.8ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
73
+  (0.2ms) select sqlite_version(*)
74
+  (143.5ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
75
+  (0.4ms) SELECT version FROM "schema_migrations"
76
+  (154.8ms) INSERT INTO "schema_migrations" (version) VALUES ('20140709170259')
77
+  (166.9ms) INSERT INTO "schema_migrations" (version) VALUES ('20140709164530')
78
+  (167.0ms) INSERT INTO "schema_migrations" (version) VALUES ('20140625183616')
79
+  (167.0ms) INSERT INTO "schema_migrations" (version) VALUES ('20140625163719')
80
+ User Load (1.8ms) SELECT "users".* FROM "users"
81
+  (0.2ms) begin transaction
82
+ SQL (0.5ms) INSERT INTO "users" ("created_at", "email", "updated_at") VALUES (?, ?, ?) [["created_at", "2014-07-09 21:02:55.250061"], ["email", "teste@gmail.com"], ["updated_at", "2014-07-09 21:02:55.250061"]]
83
+  (174.8ms) commit transaction
84
+ User Load (0.4ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
85
+  (0.2ms) begin transaction
86
+ SQL (0.4ms) INSERT INTO "orders" ("created_at", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["created_at", "2014-07-09 21:03:20.607453"], ["title", "teste"], ["updated_at", "2014-07-09 21:03:20.607453"], ["user_id", 1]]
87
+  (138.1ms) commit transaction
88
+ OrderNotification Load (0.2ms) SELECT "order_notifications".* FROM "order_notifications" WHERE "order_notifications"."order_id" = ? [["order_id", 1]]
89
+  (0.2ms) begin transaction
90
+ SQL (0.8ms) INSERT INTO "order_notifications" ("from_email", "from_name", "locale", "order_id", "template_name", "user_id") VALUES (?, ?, ?, ?, ?, ?) [["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
+  (186.2ms) commit transaction
92
+ OrderNotification Load (0.5ms) SELECT "order_notifications".* FROM "order_notifications"
93
+ OrderNotification Load (0.6ms) SELECT "order_notifications".* FROM "order_notifications"
94
+ OrderNotification Load (0.5ms) SELECT "order_notifications".* FROM "order_notifications" ORDER BY "order_notifications"."id" ASC LIMIT 1
95
+ OrderNotification Load (0.6ms) SELECT "order_notifications".* FROM "order_notifications" ORDER BY "order_notifications"."id" ASC LIMIT 1
96
+ OrderNotification Load (0.6ms) SELECT "order_notifications".* FROM "order_notifications" ORDER BY "order_notifications"."id" ASC LIMIT 1
97
+ OrderNotification Load (0.5ms) SELECT "order_notifications".* FROM "order_notifications" ORDER BY "order_notifications"."id" ASC LIMIT 1
98
+ OrderNotification Load (0.4ms) SELECT "order_notifications".* FROM "order_notifications" ORDER BY "order_notifications"."id" ASC LIMIT 1
99
+ Order Load (0.6ms) SELECT "orders".* FROM "orders" ORDER BY "orders"."id" ASC LIMIT 1
100
+ Order Load (0.6ms) SELECT "orders".* FROM "orders" ORDER BY "orders"."id" ASC LIMIT 1
101
+ OrderNotification Load (0.3ms) SELECT "order_notifications".* FROM "order_notifications" WHERE "order_notifications"."order_id" = ? [["order_id", 1]]
102
+ Order Load (0.2ms) SELECT "orders".* FROM "orders" ORDER BY "orders"."id" ASC LIMIT 1
103
+ OrderNotification Load (0.3ms) SELECT "order_notifications".* FROM "order_notifications" WHERE "order_notifications"."order_id" = ? [["order_id", 1]]
104
+ Order Load (0.3ms) SELECT "orders".* FROM "orders" ORDER BY "orders"."id" ASC LIMIT 1
105
+ OrderNotification Load (0.6ms) SELECT "order_notifications".* FROM "order_notifications" WHERE "order_notifications"."order_id" = ? ORDER BY "order_notifications"."id" ASC LIMIT 1 [["order_id", 1]]
106
+ Order Load (0.6ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1 [["id", 1]]
107
+ Order Load (0.3ms) SELECT "orders".* FROM "orders" ORDER BY "orders"."id" ASC LIMIT 1
108
+ OrderNotification Load (0.3ms) SELECT "order_notifications".* FROM "order_notifications" WHERE "order_notifications"."order_id" = ? ORDER BY "order_notifications"."id" ASC LIMIT 1 [["order_id", 1]]
109
+ Order Load (0.7ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" = ? LIMIT 1 [["id", 1]]