notifly 0.0.7 → 0.1.0

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.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +77 -14
  3. data/app/assets/javascripts/notifly.js +9 -1
  4. data/app/assets/javascripts/notifly/get_notifications.js.erb +17 -16
  5. data/app/assets/javascripts/notifly/more_notifications.js.erb +23 -0
  6. data/app/assets/javascripts/notifly/read_notifications.js.erb +22 -0
  7. data/app/assets/javascripts/notifly/seen_notifications.js.erb +22 -0
  8. data/app/controllers/notifly/notifications_controller.rb +32 -13
  9. data/app/mailers/notifly/notification_mailer.rb +21 -0
  10. data/app/models/notifly/notification.rb +24 -3
  11. data/app/views/notifly/layouts/_notification.html.erb +1 -1
  12. data/app/views/notifly/layouts/_notifly.html.erb +6 -3
  13. data/app/views/notifly/notification_mailer/notifly.html.erb +2 -0
  14. data/app/views/notifly/notifications/index.js.erb +28 -15
  15. data/app/views/notifly/notifications/{read_specific.js.erb → read.js.erb} +1 -1
  16. data/app/views/notifly/notifications/seen.js.erb +2 -0
  17. data/app/views/notifly/notifications/toggle_read.js.erb +1 -1
  18. data/app/views/notifly/templates/mails/_default.html.erb +1 -0
  19. data/app/views/notifly/templates/{_default.html.erb → notifications/_default.html.erb} +0 -0
  20. data/config/locales/en.yml +4 -0
  21. data/config/routes.rb +2 -3
  22. data/db/migrate/20141125165636_add_mail_to_notifly_notifications.rb +5 -0
  23. data/lib/generators/notifly/install/install_generator.rb +2 -1
  24. data/lib/generators/notifly/install/templates/config/initializers/notifly.rb +12 -0
  25. data/lib/generators/notifly/views/views_generator.rb +17 -2
  26. data/lib/notifly.rb +6 -0
  27. data/lib/notifly/engine.rb +0 -1
  28. data/lib/notifly/models/flyable.rb +13 -4
  29. data/lib/notifly/models/options/fly.rb +15 -3
  30. metadata +13 -22
  31. data/app/assets/javascripts/notifly/counter.js.erb +0 -23
  32. data/app/views/notifly/layouts/_footer.html.erb +0 -3
  33. data/app/views/notifly/notifications/counter.js.erb +0 -2
  34. data/lib/generators/notifly/views/USAGE +0 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a10193432e95569d125bb24dd62b696de6565c76
4
- data.tar.gz: d609cb8074f894e9664483b3ce56e2e9cfb17b45
3
+ metadata.gz: 7dcf76340dfa95ff24b649a6bdf908dbbbfaafa2
4
+ data.tar.gz: ee70a15bd945e508b4bbe4c81701bb2f3df0a1e1
5
5
  SHA512:
6
- metadata.gz: 97ef498cfd0600d05e039ef28f05f32e2b8ef49d8f55a56f6fa556b44b38a171dee6147e687983fe73f4080cf2b8b325970d65dc1f5022db3226cd09d7426098
7
- data.tar.gz: 80c043382c0d7c6d048f339cb0c36fb0bb6ac12f5f7eb3b1e97627127163370b3fcaaf12d56524fc053413683df2c87f4a02ace23d3a1f93815732f93c84bf37
6
+ metadata.gz: 62ad12ece6bc6fcdd874c83e26e7092ca7c4b65693f45c5bcaf8f8aa5d0762b0541b60acc23b6cbd27509bd881b0318256017171fb07a1711720b0b20810a7e1
7
+ data.tar.gz: c6943c8fdc1c939ff618c8d37307ebe0436d705a74ba0c4d82517a7e263fad52d72c36f8d1645eaa06596c26789cec2ec12c27382a4f39a95b34a70c370acdc5
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
- # Notifly [![Build Status](https://travis-ci.org/algorich/notifly.svg?branch=master)](https://travis-ci.org/algorich/notifly) [![Dependency Status](https://gemnasium.com/algorich/notifly.svg)](https://gemnasium.com/algorich/notifly)
1
+ # Notifly
2
+ [![Build Status](https://travis-ci.org/algorich/notifly.svg?branch=master)](https://travis-ci.org/algorich/notifly) [![Gem Version](https://badge.fury.io/rb/notifly.svg)](http://badge.fury.io/rb/notifly) [![Dependency Status](https://gemnasium.com/algorich/notifly.svg)](https://gemnasium.com/algorich/notifly)
2
3
 
3
4
  This project is still under development and it intend to offer a full notification
4
5
  system, back and front-end. Questions and suggestions are welcome and you can
@@ -48,7 +49,7 @@ Notifly **need** to storage the notifications and to do it you need to run the m
48
49
 
49
50
  We have two ways to create notifications:
50
51
 
51
- #### 1. Using `#notifly` method in your classes (as callback)
52
+ ### Using `#notifly` method in your classes (as callback)
52
53
 
53
54
  If you want to create notifications after (or before) **any** method call.
54
55
 
@@ -90,7 +91,7 @@ notiflies and set the values to all notiflies. If you need to overwrite some
90
91
  default value, just declare it again like the `:accept_gift` notifly above.
91
92
 
92
93
 
93
- #### Using `#notifly!` method on your receiver object
94
+ ### Using `#notifly!` method on your receiver object
94
95
 
95
96
  If you need to create notifications without callbacks, even in the
96
97
  controller scope.
@@ -123,12 +124,61 @@ end
123
124
 
124
125
  The receiver will be always the object which you call `#notifly!`
125
126
 
127
+ ### Mail
128
+
129
+ Notifly can send mails too. To do it, just add the option `mail` to your notifly
130
+ statement
131
+
132
+ ```ruby
133
+ class TicketOrder < ActiveRecord::Base
134
+ belongs_to :ticket
135
+ belongs_to :buyer
136
+ belongs_to :owner
137
+
138
+ notifly default_values: { receiver: :owner }
139
+
140
+ notifly before: :destroy, template: :destroy_order_notification, sender: :buyer,
141
+ data: :attributes, email: { template: :destroy_order_mail }
142
+ notifly after: :send_gift!, template: :ticket_gift, sender: :buyer,
143
+ target: :ticket, email: true, if: -> { completed? }
144
+ notifly after: :accept_gift, sender: :owner, receiver: :buyer, target: :ticket,
145
+ template: :accept_gift, email: { only: true }
146
+
147
+ def send_gift!
148
+ # code here
149
+ end
150
+
151
+ def accept_gift
152
+ # code here
153
+ end
154
+ end
155
+ ```
156
+
157
+ | Email | Description |
158
+ | ---------------------------- | ----------- |
159
+ | `true` | send email and notification using notifly template |
160
+ | `only: true` | send only an email using notifly template |
161
+ | `template: :foo` | send email using `foo` mail template and a notification using notifly template |
162
+
163
+ Notiflies with `mail: { only: true }` will persist notifications, but them won't
164
+ be in receivers notifications views. If you use [delayed_job](https://github.com/collectiveidea/delayed_job)
165
+ or [sidekiq](https://github.com/mperham/sidekiq) mails will be sent async.
166
+
167
+ ### Notifications access
168
+
126
169
  You can access the notifications using the following methods:
127
170
 
128
- - `receiver_object.notiflies`
171
+ - `receiver_object.notifly_notifications`
129
172
  - Querying `Notifly::Notifications`
130
173
  - Using our front-end helpers
131
174
 
175
+ #### Useful scopes
176
+
177
+ - all_from: used on `Notifly::Notifications` to show notifications from a specific receiver
178
+ - unseen: used on `Notifly::Notifications` and `#notifly_notifications` to show **only** unseen notifications
179
+ - not_only-mail: used on `Notifly::Notifications` and `#notifly_notifications` to remove notification that are **mail only**
180
+
181
+
132
182
  ## Front-end
133
183
 
134
184
  First, you need to have a `current_user` in `ApplicationController`, if you use
@@ -156,17 +206,29 @@ This will inject our views and it will be like that
156
206
 
157
207
  ![image](http://upl.io/i/4i26o3.png)
158
208
 
159
- As you can see, notifications are rendered with their templates. It uses a simple
160
- default template but if you want to change it or create new ones run the code below
161
- or create them in `app/views/notifly/templates/_your_template.html.erb`
209
+ Notifications and Mails are rendered with their templates. They use a simple default
210
+ template but if you want to change or create new ones run the generate below
211
+ with the option that you want or create them in `app/views/notifly/templates/`.
212
+ Remember that notifications templates should be in `notifications` folder and
213
+ mails templates in `mails` folder.
162
214
 
163
215
  ```shell
164
216
  $ rails generate notifly:views
165
217
  ```
166
218
 
167
- Now if you want to customize the layout, just generate it adding the option `--layout`
168
- to the code above and change it as you want. But if you already have a layout and just
169
- want add our features to it, take a look at [Adapting your layout](#adapting).
219
+ | Option | Description |
220
+ | ---------------- | ----------- |
221
+ | `--notification` | generates notifications templates files |
222
+ | `--layout` | generates layout files |
223
+ | `--mail` | generates mail templates files |
224
+
225
+ If you already have a layout and just want add our features to it, take a look
226
+ at [Adapting your layout](#adapting).
227
+
228
+ ### I18n
229
+
230
+ Notifly uses I18n to render the mail's subject and if you run the install generator
231
+ you can change it in `config/locales/notifly.en.yaml` or create your own.
170
232
 
171
233
  ### <a name='adapting'></a> Adapting your layout
172
234
 
@@ -188,13 +250,14 @@ Above are the elements that will loading the Notifly in your layout
188
250
  this element will contain all notifications (`_notification.html.erb`) rendered
189
251
  by `_index.html.erb`
190
252
  - **Next page link**: this link will append the next notifications page to the
191
- `#notifly-notifications-container`, this is rendered by `_footer.html.erb` and
192
- will be injected in `#notifly-notifications-footer`
253
+ `#notifly-notifications-container`, it should be in the page and should have
254
+ the id `#notifly-more-notifications-link`. This link should not have a href.
255
+ - **Mark as read**: this link will mark all notifications in the page as read,
256
+ it should be in the page and should have the id `#notifly-mark-as-read-link`.
257
+ This link should not have a href.
193
258
  - **Loading**: html element that will be showing while the notifications request
194
259
  isn't completed. It should be in `#notifly-notifications-container` and should
195
260
  have the class `loading`
196
- - **Mark as read**: this link will mark all notifications in the page as read,
197
- it will be rendered in `#notifly-header-actions`
198
261
  - **Toggle read**: this link will be rendered by `_actions.html.erb' in
199
262
  `_notification.html.erb`
200
263
 
@@ -1,5 +1,13 @@
1
1
  //= require 'jquery'
2
2
  //= require 'jquery_ujs'
3
- //= require 'notifly/counter'
4
3
  //= require 'notifly/get_notifications'
4
+ //= require 'notifly/seen_notifications'
5
+ //= require 'notifly/read_notifications'
6
+ //= require 'notifly/more_notifications'
5
7
  //= require 'twitter/bootstrap'
8
+
9
+ $(document).ready(function() {
10
+ $(document).on('click', '#notifly-notifications-panel.dropdown-menu', function (e) {
11
+ $('#notifly').hasClass('keep_open') && e.stopPropagation();
12
+ });
13
+ });
@@ -1,26 +1,27 @@
1
1
  <% notifly = Notifly::Engine.routes.url_helpers %>
2
- var notiflyNotificationsRequested;
2
+ var notiflyLastNotification, notiflyFirstNotification;
3
3
 
4
4
  var _notiflyGetNotifications = function () {
5
- var $notifly = $('#notifly-icon');
5
+ var $notifly = $('#notifly');
6
6
 
7
- $notifly.click(function () {
8
- if (!notiflyNotificationsRequested) {
9
- notiflyNotificationsRequested = true;
10
-
11
- $.ajax({
12
- url: '<%= notifly.notifications_path %>',
13
- type: 'GET',
14
- error: _notiflyHandleNotificationsError
15
- });
16
- }
17
- });
7
+ if($notifly !== undefined) {
8
+ $.ajax({
9
+ url: '<%= notifly.notifications_path %>',
10
+ data: {
11
+ scope: 'newer',
12
+ reference_notification_id: notiflyFirstNotification || null,
13
+ mark_as_seen: $notifly.hasClass('open')
14
+ },
15
+ type: 'GET',
16
+ complete: _notiflyRepeatRequest
17
+ });
18
+ }
18
19
  };
19
20
 
20
- var _notiflyHandleNotificationsError = function () {
21
- notiflyNotificationsRequested = false;
21
+ var _notiflyRepeatRequest = function () {
22
+ setTimeout(_notiflyGetNotifications, <%= Notifly.timeout %>);
22
23
  };
23
24
 
24
25
  $(document).ready(function() {
25
26
  _notiflyGetNotifications();
26
- });
27
+ });
@@ -0,0 +1,23 @@
1
+ <% notifly = Notifly::Engine.routes.url_helpers %>
2
+
3
+ var _notiflyMoreNotifications = function () {
4
+ var $notifly = $('#notifly');
5
+
6
+ if($notifly != undefined) {
7
+ $notifly.find('#notifly-more-notifications-link').click(function () {
8
+ $.ajax({
9
+ url: '<%= notifly.notifications_path %>',
10
+ data: {
11
+ scope: 'older',
12
+ reference_notification_id: notiflyLastNotification,
13
+ mark_as_seen: $notifly.hasClass('open')
14
+ },
15
+ type: 'GET'
16
+ });
17
+ });
18
+ }
19
+ };
20
+
21
+ $(document).ready(function() {
22
+ _notiflyMoreNotifications();
23
+ });
@@ -0,0 +1,22 @@
1
+ <% notifly = Notifly::Engine.routes.url_helpers %>
2
+
3
+ var _notiflyReadNotifications = function () {
4
+ var $notifly = $('#notifly-mark-as-read-link');
5
+
6
+ if($notifly != undefined) {
7
+ $notifly.click(function () {
8
+ $.ajax({
9
+ url: '<%= notifly.read_notifications_path %>',
10
+ data: {
11
+ first_notification_id: notiflyFirstNotification,
12
+ last_notification_id: notiflyLastNotification
13
+ },
14
+ type: 'PUT'
15
+ });
16
+ });
17
+ }
18
+ };
19
+
20
+ $(document).ready(function() {
21
+ _notiflyReadNotifications();
22
+ });
@@ -0,0 +1,22 @@
1
+ <% notifly = Notifly::Engine.routes.url_helpers %>
2
+
3
+ var _notiflySeenNotifications = function () {
4
+ var $trigger = $('#notifly-trigger');
5
+
6
+ if($trigger !== undefined) {
7
+ $trigger.click(function () {
8
+ $.ajax({
9
+ url: '<%= notifly.seen_notifications_path %>',
10
+ data: {
11
+ first_notification_id: notiflyFirstNotification,
12
+ last_notification_id: notiflyLastNotification
13
+ },
14
+ type: 'PUT'
15
+ });
16
+ });
17
+ }
18
+ };
19
+
20
+ $(document).ready(function() {
21
+ _notiflySeenNotifications();
22
+ });
@@ -2,21 +2,19 @@ require_dependency "notifly/application_controller"
2
2
 
3
3
  module Notifly
4
4
  class NotificationsController < ApplicationController
5
- def counter
6
- @counter = count_unseen
7
- end
8
-
9
5
  def index
10
- @notifications = current_user_notifications.page(params[:page]).per(Notifly.per_page)
11
- Notifly::Notification.where(id: @notifications.map(&:id)).update_all(seen: true)
6
+ @notifications = scoped_notifications
7
+ @notifications.update_all(seen: true) if params[:mark_as_seen] == 'true'
8
+
12
9
  @counter = count_unseen
10
+ @scope_param = scope_param
13
11
  end
14
12
 
15
-
16
- def read_specific
17
- size = params[:pages].to_i * Notifly.per_page
18
- @notifications = current_user_notifications.limit(size)
19
- @notifications.update_all read: true
13
+ def read
14
+ if params[:first_notification_id].present? and params[:last_notification_id].present?
15
+ @notifications = notifications_between
16
+ @notifications.update_all(read: true)
17
+ end
20
18
  end
21
19
 
22
20
  def toggle_read
@@ -24,13 +22,34 @@ module Notifly
24
22
  @notification.update(read: !@notification.read)
25
23
  end
26
24
 
25
+ def seen
26
+ if params[:first_notification_id].present? and params[:last_notification_id].present?
27
+ @notifications = notifications_between
28
+ @notifications.update_all(seen: true)
29
+ end
30
+ @counter = count_unseen
31
+ end
32
+
27
33
  private
34
+ def scoped_notifications
35
+ current_user_notifications.send(scope_param, than: params[:reference_notification_id])
36
+ end
37
+
38
+ def scope_param
39
+ return params[:scope] if ['older', 'newer'].include?(params[:scope])
40
+ end
41
+
28
42
  def current_user_notifications
29
- Notifly::Notification.all_from(current_user).order('created_at DESC')
43
+ current_user.notifly_notifications.not_only_mail
30
44
  end
31
45
 
32
46
  def count_unseen
33
- Notifly::Notification.unseen_from(current_user).count
47
+ current_user_notifications.unseen.count
48
+ end
49
+
50
+ def notifications_between
51
+ current_user_notifications.between(params[:first_notification_id],
52
+ params[:last_notification_id])
34
53
  end
35
54
  end
36
55
  end
@@ -0,0 +1,21 @@
1
+ module Notifly
2
+ class NotificationMailer < ActionMailer::Base
3
+ default from: Notifly.mailer_sender
4
+
5
+ def notifly(to: nil, notification_id: nil, template: nil)
6
+ if defined? Delayed::Job or defined? Sidekiq::Worker
7
+ delay.send_notification(to, notification_id, template)
8
+ else
9
+ send_notification(to, notification_id, template).deliver
10
+ end
11
+ end
12
+
13
+ private
14
+ def send_notification(to, notification_id, template)
15
+ @notification = Notifly::Notification.find(notification_id)
16
+ @template = template
17
+
18
+ mail to: to, subject: t("notifly.mail_subject.#{@template}")
19
+ end
20
+ end
21
+ end
@@ -6,10 +6,31 @@ module Notifly
6
6
 
7
7
  before_validation :convert_data, :set_template
8
8
 
9
- scope :all_from, ->(receiver) { where(receiver: receiver) }
10
- scope :unseen_from, ->(receiver) { where(receiver: receiver, seen: false) }
9
+ scope :all_from, -> (receiver) { where(receiver: receiver) }
10
+ scope :unseen, -> { where(seen: false) }
11
+ scope :not_only_mail, -> { where.not(mail: 'only') }
12
+ scope :limited, -> { limit(Notifly.per_page) }
13
+ scope :ordered, -> { order('created_at DESC') }
14
+ scope :newer, ->(than: nil) do
15
+ return ordered.limited if than.blank?
11
16
 
12
- validates :receiver, :template, presence: true
17
+ reference = find(than)
18
+ ordered.where('created_at > ?', reference.created_at).where.not(id: reference)
19
+ end
20
+ scope :older, ->(than: nil) do
21
+ reference = find(than)
22
+
23
+ ordered.
24
+ where('created_at < ?', reference.created_at).
25
+ where.not(id: reference).
26
+ limited
27
+ end
28
+ scope :between, ->(first, last) do
29
+ notifications = where(id: [first, last])
30
+ where(created_at: (notifications.first.created_at..notifications.last.created_at))
31
+ end
32
+
33
+ validates :receiver, :template, :mail, presence: true
13
34
 
14
35
  def data
15
36
  YAML.load(read_attribute(:data))
@@ -3,7 +3,7 @@
3
3
  <% read = notification.read ? '' : 'notifly-notification-not-read' %>
4
4
 
5
5
  <div id='<%= "notifly-notification-#{notification.id}" %>' class="notifly-notification <%= read %>">
6
- <%= render partial: "notifly/templates/#{notification.template}",
6
+ <%= render partial: "notifly/templates/notifications/#{notification.template}",
7
7
  locals: { notification: notification } %>
8
8
 
9
9
  <span class="notifly-notification-actions">
@@ -1,8 +1,8 @@
1
1
  <!-- This partial requires the locals: receiver -->
2
2
 
3
- <div id="notifly" class="dropdown">
3
+ <div id="notifly" class="dropdown keep_open">
4
4
 
5
- <a href="#" class="dropdown-toggle" data-toggle="dropdown">
5
+ <a id="notifly-trigger" href="#" class="dropdown-toggle" data-toggle="dropdown">
6
6
  <span id="notifly-counter"></span>
7
7
  <span id="notifly-icon"><%= notifly_icon %></span>
8
8
  </a>
@@ -13,6 +13,7 @@
13
13
  Notifications
14
14
 
15
15
  <span id="notifly-header-actions" class="pull-right">
16
+ <%= link_to 'Mark as read', '#', id: 'notifly-mark-as-read-link' %>
16
17
  </span>
17
18
  </div>
18
19
 
@@ -20,6 +21,8 @@
20
21
  <div class="loading">Loading notifications...</div>
21
22
  </div>
22
23
 
23
- <div id="notifly-notifications-footer"></div>
24
+ <div id="notifly-notifications-footer">
25
+ <%= link_to 'More', '#', id: 'notifly-more-notifications-link' %>
26
+ </div>
24
27
  </div>
25
28
  </div>
@@ -0,0 +1,2 @@
1
+ <%= render partial: "notifly/templates/mails/#{@template}",
2
+ locals: { notification: @notification } %>
@@ -1,20 +1,33 @@
1
- <%
2
- rendered_notifications = render partial: 'notifly/layouts/index',
3
- locals: { notifications: @notifications }
1
+ <% rendered_notifications = render partial: 'notifly/layouts/index',
2
+ locals: { notifications: @notifications } %>
3
+ <% last_notification = @notifications.last.try(:id) %>
4
+ <% first_notification = @notifications.first.try(:id) %>
4
5
 
5
- notifications_pages = { pages: @notifications.current_page }
6
- rendered_header_actions = link_to 'Mark as read', read_specific_notifications_path(notifications_pages),
7
- remote: true, method: :put
6
+ <% if @notifications.any? %>
7
+ $('#notifly-notifications-content .loading').remove();
8
8
 
9
- rendered_footer = render partial: 'notifly/layouts/footer',
10
- locals: { notifications: @notifications }
11
- %>
9
+ <% if @scope_param == 'older' %>
10
+ $('#notifly-notifications-content').append("<%= j rendered_notifications %>");
11
+ notiflyLastNotification = <%= last_notification %>;
12
12
 
13
- $('#notifly-header-actions').html("<%= escape_javascript rendered_header_actions %>");
13
+ <% else %>
14
+ $('#notifly-notifications-content').prepend("<%= j rendered_notifications %>");
15
+ notiflyFirstNotification = <%= first_notification %>;
14
16
 
15
- $('#notifly-notifications-content .loading').remove();
16
- $('#notifly-notifications-content').append("<%= escape_javascript rendered_notifications %>");
17
- $('#notifly-notifications-footer').html("<%= escape_javascript rendered_footer %>");
17
+ if (notiflyLastNotification == undefined)
18
+ notiflyLastNotification = <%= last_notification %>;
18
19
 
19
- $('#notifly-counter').html("<%= escape_javascript(render 'notifly/layouts/counter', counter: @counter)%>");
20
- $('#notifly-icon').html("<%= escape_javascript notifly_icon(@counter > 0) %>")
20
+ <% end %>
21
+
22
+ <% else %>
23
+ if (notiflyLastNotification == undefined)
24
+ $('#notifly-more-notifications-link').remove();
25
+
26
+ <% end %>
27
+
28
+ <% if @scope_param == 'older' and Notifly::Notification.older(than: last_notification).blank? %>
29
+ $('#notifly-more-notifications-link').remove();
30
+ <% end %>
31
+
32
+ $('#notifly-counter').html("<%= j(render 'notifly/layouts/counter', counter: @counter)%>");
33
+ $('#notifly-icon').html("<%= j notifly_icon(@counter > 0) %>")
@@ -1,4 +1,4 @@
1
1
  <% rendered_index = render partial: 'notifly/layouts/index',
2
2
  locals: { notifications: @notifications } %>
3
3
 
4
- $('#notifly-notifications-content').html("<%= escape_javascript rendered_index %>");
4
+ $('#notifly-notifications-content').html("<%= j rendered_index %>");
@@ -0,0 +1,2 @@
1
+ $('#notifly-counter').html("<%= j(render 'notifly/layouts/counter', counter: @counter)%>");
2
+ $('#notifly-icon').html("<%= j notifly_icon(@counter > 0) %>")
@@ -2,4 +2,4 @@
2
2
  locals: { notification: @notification } %>
3
3
 
4
4
  $('<%= "#notifly-notification-#{@notification.id}" %>')
5
- .replaceWith('<%= escape_javascript rendered_notification %>')
5
+ .replaceWith('<%= j rendered_notification %>')
@@ -0,0 +1 @@
1
+ Default notification mail
@@ -0,0 +1,4 @@
1
+ en:
2
+ notifly:
3
+ mail_subject:
4
+ default: 'You have a new notification'
data/config/routes.rb CHANGED
@@ -3,9 +3,8 @@ Notifly::Engine.routes.draw do
3
3
  put :toggle_read
4
4
 
5
5
  collection do
6
- get :counter
7
- put :update_counter
8
- put :read_specific
6
+ put :read
7
+ put :seen
9
8
  end
10
9
  end
11
10
  end
@@ -0,0 +1,5 @@
1
+ class AddMailToNotiflyNotifications < ActiveRecord::Migration
2
+ def change
3
+ add_column :notifly_notifications, :mail, :string
4
+ end
5
+ end
@@ -15,7 +15,8 @@ module Notifly
15
15
  end
16
16
 
17
17
  def copy_config
18
- template "config/initializers/notifly.rb"
18
+ template 'config/initializers/notifly.rb'
19
+ copy_file '../../../../../config/locales/en.yml', 'config/locales/notifly.en.yml'
19
20
  end
20
21
 
21
22
  def remember_to_install_migrations
@@ -1,4 +1,16 @@
1
1
  Notifly.setup do |config|
2
2
  # Define how many notifications per page.
3
3
  config.per_page = 10
4
+
5
+ # Define the time interval in miliseconfs between requests to check notifications.
6
+ config.timeout = 10000
7
+
8
+ # Define the notifly icon from font-awesome-rails
9
+ config.icon = 'bell'
10
+
11
+ # Define the notifly icon size
12
+ config.icon_size = '2x'
13
+
14
+ # Define your mailer sender
15
+ config.mailer_sender = 'change-me-at-config-initializers-notifly@exemple.com'
4
16
  end
@@ -2,10 +2,17 @@ module Notifly
2
2
  module Generators
3
3
  class ViewsGenerator < Rails::Generators::Base
4
4
  source_root File.expand_path('../../../../../app/views/notifly', __FILE__)
5
- class_option :layout, type: :boolean, default: false, desc: 'Include layout files.'
5
+
6
+ class_option :layout, type: :boolean, default: false,
7
+ desc: 'Include/Remove layout files.'
8
+ class_option :mail, type: :boolean, default: false,
9
+ desc: 'Include/Remove mails templates files.'
10
+ class_option :notification, type: :boolean, default: false,
11
+ desc: 'Include/Remove notifications templates files.'
6
12
 
7
13
  def copy_views
8
- copy_file 'templates/_default.html.erb', "#{main_app_path}/templates/_default.html.erb"
14
+ notifications_templates if options.notification?
15
+ notifications_mails_templates if options.mail?
9
16
  layout_files if options.layout?
10
17
  end
11
18
 
@@ -14,6 +21,14 @@ module Notifly
14
21
  'app/views/notifly'
15
22
  end
16
23
 
24
+ def notifications_templates
25
+ directory 'templates/notifications', "#{main_app_path}/templates/notifications"
26
+ end
27
+
28
+ def notifications_mails_templates
29
+ directory 'templates/mails', "#{main_app_path}/templates/mails"
30
+ end
31
+
17
32
  def layout_files
18
33
  directory 'layouts', "#{main_app_path}/layouts"
19
34
  end
data/lib/notifly.rb CHANGED
@@ -16,6 +16,12 @@ module Notifly
16
16
  mattr_accessor :icon_empty
17
17
  @@icon_empty = 'bell-o'
18
18
 
19
+ mattr_accessor :mailer_sender
20
+ @@mailer_sender = 'change-me-at-config-initializers-notifly@exemple.com'
21
+
22
+ mattr_accessor :timeout
23
+ @@timeout = 10000
24
+
19
25
  # Default way to setup Notifly. Run rails generate notifly:install to create
20
26
  # a fresh initializer with all configuration values.
21
27
  def self.setup
@@ -1,5 +1,4 @@
1
1
  require 'jquery-rails'
2
- require 'kaminari'
3
2
 
4
3
  module Notifly
5
4
  class Engine < ::Rails::Engine
@@ -91,11 +91,20 @@ module Notifly
91
91
 
92
92
  def _create_notification_for(fly)
93
93
  new_fly = _default_fly.merge(fly)
94
- Notifly::Notification.create! _get_attributes_from(new_fly)
94
+ notification = Notifly::Notification.create _get_attributes_from(new_fly)
95
+
96
+ if new_fly.mail.present?
97
+ template = new_fly.mail.try(:fetch, :template) || notification.template
98
+
99
+ Notifly::NotificationMailer.notifly to: self.email, template: template,
100
+ notification_id: notification.id
101
+ end
102
+ rescue => e
103
+ logger.error "Something goes wrong with Notifly, will ignore: #{e}"
95
104
  end
96
105
 
97
- def notifly_notifications(options={})
98
- Notifly::Notification.where(receiver: self)
106
+ def notifly_notifications
107
+ Notifly::Notification.all_from(self)
99
108
  end
100
109
 
101
110
  private
@@ -114,7 +123,7 @@ module Notifly
114
123
  end
115
124
 
116
125
  def _eval_for(key, value)
117
- if key.to_sym == :template
126
+ if [:template, :mail].include? key.to_sym
118
127
  value
119
128
  elsif value == :self
120
129
  self
@@ -3,7 +3,7 @@ module Notifly
3
3
  module Options
4
4
  class Fly
5
5
  attr_accessor :before, :after, :template, :sender, :receiver, :target,
6
- :if, :unless, :data
6
+ :if, :unless, :data, :mail
7
7
 
8
8
  def initialize(options={})
9
9
  options = options.fetch(:default_values, options)
@@ -23,13 +23,25 @@ module Notifly
23
23
  end
24
24
 
25
25
  def attributes
26
- instance_values.reject { |key| [hook, :if, :unless].include? key.to_sym }
26
+ no_attrs = [hook, :if, :unless, :mail]
27
+ attrs = instance_values.reject { |key| no_attrs.include? key.to_sym }
28
+ attrs.merge({mail: get_mail_type})
27
29
  end
28
30
 
29
31
  def merge(fly)
30
32
  raise TypeError, "#{fly} is not a Fly" unless fly.is_a? self.class
31
33
 
32
- Notifly::Models::Options::Fly.new attributes.merge(fly.attributes)
34
+ Notifly::Models::Options::Fly.new instance_values.merge(fly.instance_values)
35
+ end
36
+
37
+ def get_mail_type
38
+ if mail == true
39
+ :always
40
+ elsif mail.present? and mail[:only]
41
+ :only
42
+ else
43
+ :never
44
+ end
33
45
  end
34
46
  end
35
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notifly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pedro Passalini
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-24 00:00:00.000000000 Z
12
+ date: 2014-12-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -45,20 +45,6 @@ dependencies:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '3.0'
48
- - !ruby/object:Gem::Dependency
49
- name: kaminari
50
- requirement: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: 0.16.1
55
- type: :runtime
56
- prerelease: false
57
- version_requirements: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: 0.16.1
62
48
  - !ruby/object:Gem::Dependency
63
49
  name: font-awesome-rails
64
50
  requirement: !ruby/object:Gem::Requirement
@@ -255,34 +241,39 @@ files:
255
241
  - README.md
256
242
  - Rakefile
257
243
  - app/assets/javascripts/notifly.js
258
- - app/assets/javascripts/notifly/counter.js.erb
259
244
  - app/assets/javascripts/notifly/get_notifications.js.erb
245
+ - app/assets/javascripts/notifly/more_notifications.js.erb
246
+ - app/assets/javascripts/notifly/read_notifications.js.erb
247
+ - app/assets/javascripts/notifly/seen_notifications.js.erb
260
248
  - app/assets/stylesheets/notifly.css
261
249
  - app/assets/stylesheets/notifly/layout.css
262
250
  - app/controllers/notifly/application_controller.rb
263
251
  - app/controllers/notifly/notifications_controller.rb
264
252
  - app/helpers/notifly/view_helper.rb
253
+ - app/mailers/notifly/notification_mailer.rb
265
254
  - app/models/notifly/notification.rb
266
255
  - app/views/notifly/layouts/_actions.html.erb
267
256
  - app/views/notifly/layouts/_counter.html.erb
268
- - app/views/notifly/layouts/_footer.html.erb
269
257
  - app/views/notifly/layouts/_index.html.erb
270
258
  - app/views/notifly/layouts/_notification.html.erb
271
259
  - app/views/notifly/layouts/_notifly.html.erb
272
- - app/views/notifly/notifications/counter.js.erb
260
+ - app/views/notifly/notification_mailer/notifly.html.erb
273
261
  - app/views/notifly/notifications/index.js.erb
274
- - app/views/notifly/notifications/read_specific.js.erb
262
+ - app/views/notifly/notifications/read.js.erb
263
+ - app/views/notifly/notifications/seen.js.erb
275
264
  - app/views/notifly/notifications/toggle_read.js.erb
276
- - app/views/notifly/templates/_default.html.erb
265
+ - app/views/notifly/templates/mails/_default.html.erb
266
+ - app/views/notifly/templates/notifications/_default.html.erb
277
267
  - bin/rails
268
+ - config/locales/en.yml
278
269
  - config/routes.rb
279
270
  - db/migrate/20141103170528_create_notifly_notifications.rb
280
271
  - db/migrate/20141104150224_add_data_to_notification.rb
281
272
  - db/migrate/20141117193436_add_seen_to_notifly_notification.rb
273
+ - db/migrate/20141125165636_add_mail_to_notifly_notifications.rb
282
274
  - lib/generators/notifly/install/install_generator.rb
283
275
  - lib/generators/notifly/install/templates/config/initializers/notifly.rb
284
276
  - lib/generators/notifly/install/utils.rb
285
- - lib/generators/notifly/views/USAGE
286
277
  - lib/generators/notifly/views/views_generator.rb
287
278
  - lib/notifly.rb
288
279
  - lib/notifly/engine.rb
@@ -1,23 +0,0 @@
1
- <% notifly = Notifly::Engine.routes.url_helpers %>
2
- var notiflyCounterRequested;
3
-
4
- var _notiflyGetCounter = function (notiflyPath) {
5
- var $counter = $('#notifly-counter');
6
-
7
- if (!notiflyCounterRequested) {
8
- $.ajax({
9
- url: '<%= notifly.counter_notifications_path %>',
10
- type: 'GET',
11
- error: _notiflyHandleCounterError
12
- });
13
- }
14
- };
15
-
16
-
17
- var _notiflyHandleCounterError = function () {
18
- notiflyCounterRequested = false;
19
- };
20
-
21
- $(document).ready(function() {
22
- _notiflyGetCounter();
23
- });
@@ -1,3 +0,0 @@
1
- <!-- This partial requires the locals: notifications -->
2
-
3
- <%= link_to_next_page notifications, 'More', remote: true, id: 'notifly-next-page' %>
@@ -1,2 +0,0 @@
1
- $('#notifly-counter').html("<%= escape_javascript(render 'notifly/layouts/counter', counter: @counter)%>");
2
- $('#notifly-icon').html("<%= escape_javascript notifly_icon(@counter > 0) %>")
@@ -1,8 +0,0 @@
1
- Description:
2
- This generator will create Notifly views in your application
3
-
4
- Example:
5
- rails generate notifly:views:templates
6
-
7
- This will create:
8
- app/views/notifly/templates/default.html.erb