notifly 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 92f6c588e4f3502444595b31560180669c637cd9
4
- data.tar.gz: 302667266d98f457bd82e4e622daccb84ca0c62c
3
+ metadata.gz: 199c4354275abe7fdd5dfbc605a68a17205477ec
4
+ data.tar.gz: 83ab9a0f55faaba7ff98f6fbf6e68919fbf8d963
5
5
  SHA512:
6
- metadata.gz: 6754215c7d59660b4b2e535b370776b2fc4a8b6431c234a42f8872e80bcb83c4d8e08ea2aff68d5716be2b4952c2857176482c42f43f8e07f4036c0845c50080
7
- data.tar.gz: 4a1fcda0742faafb2d8b35f9296500ed90a7026d0f63cc5c7756d2884952d7a3eae1af5d56ca9279301fae9f775bfb7b080c8f3b1a655fb9aa6d8177e4608582
6
+ metadata.gz: e78062c032b1bab6bb4ca30983d91ce80564051deac0ba68dc044aa9d49012f8aa828684bedeb154e450abc1b348e46b3ed6bcd0747ac9cbfa93873181e97b8e
7
+ data.tar.gz: bbb624c480c39326c2fef39a1b5028223c08a7220ba4aec51241cd3bdea3b19b10cfdf84b40a7df5fe10d17eba3a8743a0bb6939ad81cfb8eef775544d451e30
data/README.md CHANGED
@@ -187,6 +187,10 @@ You can access the notifications using the following methods:
187
187
  - unseen: used on `Notifly::Notifications` and `#notifly_notifications` to show **only** unseen notifications
188
188
  - not_only-mail: used on `Notifly::Notifications` and `#notifly_notifications` to remove notification that are **mail only**
189
189
 
190
+ ### Websocket
191
+
192
+ If you want to use websocket just install the gem [websocket-rails](https://github.com/websocket-rails/websocket-rails/wiki/Installation-and-Setup) and change the notifly's configuration
193
+ at `config/initializers/notifly.rb`
190
194
 
191
195
  ## Front-end
192
196
 
@@ -249,7 +253,8 @@ Notifications and Mails are rendered with their templates. They use a simple def
249
253
  template but if you want to change or create new ones run the generate above
250
254
  with the option that you want or create them in `app/views/notifly/templates/`.
251
255
  Remember that notifications templates should be in `notifications` folder and
252
- mails templates in `mails` folder.
256
+ mails templates in `mails` folder and with **both** you need to use the `main_app`
257
+ to render links.
253
258
 
254
259
  If you already have a layout and just want add our features to it, take a look
255
260
  at [Adapting your layout](#adapting).
@@ -5,6 +5,7 @@
5
5
  //= require 'notifly/seen_notifications'
6
6
  //= require 'notifly/read_notifications'
7
7
  //= require 'notifly/more_notifications'
8
+ <% require_asset 'notifly/real_time' if Notifly.websocket %>
8
9
 
9
10
  $(document).ready(function() {
10
11
  $(document).on('click', '#notifly-notifications-panel.dropdown-menu', function (e) {
@@ -1,6 +1,10 @@
1
1
  <% notifly = Notifly::Engine.routes.url_helpers %>
2
2
  var notiflyLastNotification, notiflyFirstNotification;
3
3
 
4
+ $(document).ready(function() {
5
+ _notiflyGetNotifications();
6
+ });
7
+
4
8
  var _notiflyGetNotifications = function () {
5
9
  var $notifly = $('#notifly');
6
10
 
@@ -19,9 +23,7 @@ var _notiflyGetNotifications = function () {
19
23
  };
20
24
 
21
25
  var _notiflyRepeatRequest = function () {
22
- setTimeout(_notiflyGetNotifications, <%= Notifly.timeout %>);
26
+ <% if not Notifly.websocket %>
27
+ setTimeout(_notiflyGetNotifications, <%= Notifly.timeout %>);
28
+ <% end %>
23
29
  };
24
-
25
- $(document).ready(function() {
26
- _notiflyGetNotifications();
27
- });
@@ -1,5 +1,10 @@
1
1
  <% notifly = Notifly::Engine.routes.url_helpers %>
2
2
 
3
+ $(document).ready(function() {
4
+ _notiflyReadNotifications();
5
+ _notiflyReadNotificationWithLink();
6
+ });
7
+
3
8
  var _notiflyReadNotifications = function () {
4
9
  var $notifly = $('#notifly-mark-as-read-link');
5
10
 
@@ -17,6 +22,19 @@ var _notiflyReadNotifications = function () {
17
22
  }
18
23
  };
19
24
 
20
- $(document).ready(function() {
21
- _notiflyReadNotifications();
22
- });
25
+ var _notiflyReadNotificationWithLink = function () {
26
+ if ($('#notifly').length > 0) {
27
+ $('#notifly').on('click', '.notifly-notification-message a', function () {
28
+ var notificationId = $(this).closest('div.notifly-notification').data('id');
29
+
30
+ $.ajax({
31
+ url: '<%= notifly.notification_toggle_read_path %>',
32
+ data: {
33
+ notification_id: notificationId,
34
+ read: true
35
+ },
36
+ type: 'PUT'
37
+ });
38
+ });
39
+ }
40
+ };
@@ -0,0 +1,26 @@
1
+ window.dispatcher = new WebSocketRails(location.host + '/websocket');
2
+ var notiflyFirstNotification;
3
+
4
+ $(function () {
5
+ var $notifly = $('#notifly');
6
+
7
+ if ($notifly !== undefined) {
8
+ dispatcher.bind('notifly.notifications.new', _injectNotificationFrom);
9
+ }
10
+ });
11
+
12
+ var _injectNotificationFrom = function (data) {
13
+ if (data.message) {
14
+ $('#notifly-notifications-content .notifly-empty').remove();
15
+ notiflyFirstNotification = data.id;
16
+
17
+ if (!$('#notifly').hasClass('open')) {
18
+ $counter = $('#notifly-counter');
19
+ $counter.html(eval($counter.html()) + 1);
20
+ $counter.removeClass('hide');
21
+ }
22
+
23
+ return $('#notifly').find('#notifly-notifications-content')
24
+ .prepend(data.message)
25
+ }
26
+ };
@@ -19,7 +19,7 @@ module Notifly
19
19
 
20
20
  def toggle_read
21
21
  @notification = Notifly::Notification.find(params[:notification_id])
22
- @notification.update(read: !@notification.read)
22
+ @notification.update(read: params[:read] || !@notification.read)
23
23
  end
24
24
 
25
25
  def seen
@@ -5,23 +5,24 @@ module Notifly
5
5
  belongs_to :receiver, polymorphic: true
6
6
 
7
7
  before_validation :set_defaults
8
+ after_create :send_to_receiver, if: -> { Notifly.websocket }
8
9
 
9
10
  scope :all_from, -> (receiver) { where(receiver: receiver) }
10
11
  scope :unseen, -> { where(seen: false) }
11
12
  scope :not_only_mail, -> { where.not(mail: 'only') }
12
13
  scope :limited, -> { limit(Notifly.per_page) }
13
- scope :ordered, -> { order('created_at DESC') }
14
+ scope :ordered, -> { order('notifly_notifications.created_at DESC') }
14
15
  scope :newer, ->(than: nil) do
15
16
  return ordered if than.blank?
16
17
 
17
18
  reference = find_by(id: than)
18
- ordered.where('created_at > ?', reference.created_at).where.not(id: reference)
19
+ ordered.where('notifly_notifications.created_at > ?', reference.created_at).where.not(id: reference)
19
20
  end
20
21
  scope :older, ->(than: nil) do
21
22
  reference = find_by(id: than)
22
23
 
23
24
  ordered.
24
- where('created_at < ?', reference.created_at).
25
+ where('notifly_notifications.created_at < ?', reference.created_at).
25
26
  where.not(id: reference)
26
27
  end
27
28
  scope :between, ->(first, last) do
@@ -39,5 +40,9 @@ module Notifly
39
40
  self.kind ||= :notification
40
41
  self.template ||= :default
41
42
  end
43
+
44
+ def send_to_receiver
45
+ Notifly::NotificationChannel.new(self.receiver_id).trigger(self)
46
+ end
42
47
  end
43
48
  end
@@ -2,9 +2,11 @@
2
2
 
3
3
  <% read = notification.read ? '' : 'notifly-notification-not-read' %>
4
4
 
5
- <div id='<%= "notifly-notification-#{notification.id}" %>' class="notifly-notification <%= read %>">
6
- <%= render partial: "notifly/templates/notifications/#{notification.template}",
7
- locals: { notification: notification } %>
5
+ <div id='<%= "notifly-notification-#{notification.id}" %>' class="notifly-notification <%= read %>" data-id="<%= notification.id %>">
6
+ <div class="notifly-notification-message">
7
+ <%= render partial: "notifly/templates/notifications/#{notification.template}",
8
+ locals: { notification: notification } %>
9
+ </div>
8
10
 
9
11
  <span class="notifly-notification-actions">
10
12
  <%= render partial: 'notifly/layouts/actions', locals: { notification: notification } %>
@@ -1,4 +1,4 @@
1
- <div id="notifly" class="dropdown keep_open">
1
+ <div id="notifly" class="dropdown keep_open" data-user-id="<%= current_user.id %>">
2
2
 
3
3
  <a id="notifly-trigger" href="#" class="dropdown-toggle" data-toggle="dropdown">
4
4
  <span id="notifly-counter"></span>
@@ -1,17 +1,17 @@
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) %>
5
-
6
1
  $('#notifly-notifications-content .notifly-loading').remove();
7
2
  $('#notifly-notifications-content .notifly-empty').remove();
8
3
 
9
4
  <% if @notifications.any? %>
5
+ <% rendered_notifications = render partial: 'notifly/layouts/index',
6
+ locals: { notifications: @notifications } %>
7
+ <% last_notification = @notifications.last.try(:id) %>
8
+ <% first_notification = @notifications.first.try(:id) %>
10
9
 
11
10
  <% if @scope_param == 'older' %>
12
- $('#notifly-notifications-content').append("<%= j rendered_notifications %>");
11
+ $('#notifly-notifications-content').append("<%= j rendered_notifications %>");
13
12
  notiflyLastNotification = <%= last_notification %>;
14
13
 
14
+ $('#notifly-notifications-content').scrollTop($('<%= "#notifly-notification-#{first_notification}" %>').offset().top);
15
15
  <% else %>
16
16
  $('#notifly-notifications-content').prepend("<%= j rendered_notifications %>");
17
17
  notiflyFirstNotification = <%= first_notification %>;
@@ -1,3 +1,3 @@
1
1
  <!-- This partial requires the locals: notification -->
2
2
 
3
- <%= notification.receiver.name %>, you receive a new notification at <%= notification.created_at %>
3
+ <%= notification.receiver.try(:name) %>, you receive a new notification at <%= notification.created_at %>
@@ -1,7 +1,7 @@
1
1
  Notifly::Engine.routes.draw do
2
- resources :notifications, only: [:index] do
3
- put :toggle_read
2
+ put '/notifications/(:notification_id)/toggle_read', to: 'notifications#toggle_read', as: :notification_toggle_read
4
3
 
4
+ resources :notifications, only: [:index] do
5
5
  collection do
6
6
  put :read
7
7
  put :seen
@@ -13,4 +13,7 @@ Notifly.setup do |config|
13
13
 
14
14
  # Define your mailer sender
15
15
  config.mailer_sender = 'change-me-at-config-initializers-notifly@exemple.com'
16
+
17
+ # Active websocket
18
+ config.websocket = false
16
19
  end
@@ -1,6 +1,9 @@
1
1
  require 'notifly/engine'
2
2
  require 'notifly/railtie'
3
+ require 'services/notification_channel'
4
+ require 'services/action_view_helper'
3
5
  require 'font-awesome-rails'
6
+ require 'websocket-rails'
4
7
 
5
8
  module Notifly
6
9
  # How many notifications per page.
@@ -19,9 +22,14 @@ module Notifly
19
22
  mattr_accessor :mailer_sender
20
23
  @@mailer_sender = 'change-me-at-config-initializers-notifly@exemple.com'
21
24
 
25
+ # Timeout used when notifly get new notifications per request
22
26
  mattr_accessor :timeout
23
27
  @@timeout = 10000
24
28
 
29
+ # Active websocket
30
+ mattr_accessor :websocket
31
+ @@websocket = false
32
+
25
33
  # Default way to setup Notifly. Run rails generate notifly:install to create
26
34
  # a fresh initializer with all configuration values.
27
35
  def self.setup
@@ -0,0 +1,19 @@
1
+ class ActionViewHelper
2
+ attr_reader :action_view
3
+ delegate *ActionView::Base.instance_methods, to: :action_view
4
+
5
+ def initialize
6
+ notifly_path = File.expand_path(File.dirname(File.dirname(__FILE__))) + '../../app/views/notifly'
7
+ ActionController::Base.prepend_view_path(notifly_path)
8
+ @action_view = ActionView::Base.new(ActionController::Base.view_paths)
9
+ @action_view.extend ApplicationHelper
10
+
11
+ @action_view.class_eval do
12
+ include Notifly::Engine.routes.url_helpers
13
+
14
+ def protect_against_forgery?
15
+ false
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module Notifly
2
+ class NotificationChannel
3
+ def initialize(user_id)
4
+ @user_id = user_id
5
+ @channel = WebsocketRails.users[@user_id.to_s]
6
+ @action_view = ActionViewHelper.new
7
+ end
8
+
9
+ def trigger(notification)
10
+ @channel.send_message 'notifly.notifications.new',
11
+ { message: render(notification), id: notification.id }
12
+ end
13
+
14
+ def render(notification)
15
+ @action_view.render partial: 'layouts/notification',
16
+ locals: { notification: notification }
17
+ end
18
+ end
19
+ 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.3.4
4
+ version: 0.3.5
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: 2015-01-19 00:00:00.000000000 Z
12
+ date: 2015-02-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -59,6 +59,20 @@ dependencies:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 4.2.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: coffee-rails
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 4.0.0
69
+ type: :runtime
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 4.0.0
62
76
  - !ruby/object:Gem::Dependency
63
77
  name: pg
64
78
  requirement: !ruby/object:Gem::Requirement
@@ -115,6 +129,34 @@ dependencies:
115
129
  - - ">="
116
130
  - !ruby/object:Gem::Version
117
131
  version: '0'
132
+ - !ruby/object:Gem::Dependency
133
+ name: websocket-rails
134
+ requirement: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.7.0
139
+ type: :development
140
+ prerelease: false
141
+ version_requirements: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.7.0
146
+ - !ruby/object:Gem::Dependency
147
+ name: thin
148
+ requirement: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ type: :development
154
+ prerelease: false
155
+ version_requirements: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
118
160
  - !ruby/object:Gem::Dependency
119
161
  name: better_errors
120
162
  requirement: !ruby/object:Gem::Requirement
@@ -170,10 +212,11 @@ files:
170
212
  - MIT-LICENSE
171
213
  - README.md
172
214
  - Rakefile
173
- - app/assets/javascripts/notifly.js
215
+ - app/assets/javascripts/notifly.js.erb
174
216
  - app/assets/javascripts/notifly/get_notifications.js.erb
175
217
  - app/assets/javascripts/notifly/more_notifications.js.erb
176
218
  - app/assets/javascripts/notifly/read_notifications.js.erb
219
+ - app/assets/javascripts/notifly/real_time.js.erb
177
220
  - app/assets/javascripts/notifly/seen_notifications.js.erb
178
221
  - app/assets/javascripts/notifly_dropdown.js
179
222
  - app/assets/stylesheets/notifly.css
@@ -215,6 +258,8 @@ files:
215
258
  - lib/notifly/models/notifiable.rb
216
259
  - lib/notifly/models/options/fly.rb
217
260
  - lib/notifly/railtie.rb
261
+ - lib/services/action_view_helper.rb
262
+ - lib/services/notification_channel.rb
218
263
  - lib/tasks/notifly_tasks.rake
219
264
  - vendor/assets/javascripts/tinycon.js
220
265
  - vendor/assets/javascripts/twitter/bootstrap/dropdown.js