simple_notification 0.0.4
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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +86 -0
- data/Rakefile +1 -0
- data/lib/generators/simple_notification/USAGE +8 -0
- data/lib/generators/simple_notification/simple_notification_generator.rb +39 -0
- data/lib/generators/simple_notification/templates/create_notifications.rb.erb +14 -0
- data/lib/generators/simple_notification/templates/notification.rb +6 -0
- data/lib/generators/simple_notification/templates/notifications_controller.rb +38 -0
- data/lib/generators/simple_notification/templates/notifications_css.css.scss +0 -0
- data/lib/generators/simple_notification/templates/notifications_index.html.haml +4 -0
- data/lib/generators/simple_notification/templates/notifications_partial.html.haml +31 -0
- data/lib/simple_notification.rb +6 -0
- data/lib/simple_notification/can_be_tracked.rb +21 -0
- data/lib/simple_notification/has_notifications.rb +6 -0
- data/lib/simple_notification/version.rb +3 -0
- data/simple_notification.gemspec +23 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3da0d1777ac57828b42209ff9c547d53e3434942
|
4
|
+
data.tar.gz: 75169ab04af06a1c0bd1f7120cc1c9b987ccbc83
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a88d7348f157926aebf3a2a57a699699fd6146b61d1f2b328cbf4f29506b9a979f890816a0a2845e758ce01d40c4590de746b8636e480d8feefc6e8d1ced251b
|
7
|
+
data.tar.gz: 5ec6d6c2dd426ce4308ed125b047ae259a705d0a8179b58c8e6bb8f7dccc4ee09bbaa9a6fc65fa74c3e626043ce563d7f71afc8f1dde05976491bdb0854ee5d1
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Cong Wang
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# SimpleNotification
|
2
|
+
|
3
|
+
SimpleNotification is a user notification system using tracked events. Event has a owner, recipient, trackable source, and a message.
|
4
|
+
|
5
|
+
## Assumptions
|
6
|
+
|
7
|
+
SimpleNotification assumes a user systems is in place which uses the User model and has the current_user helper.
|
8
|
+
|
9
|
+
SimpleNotification also assumes that the Notification model is not taken.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'simple_notification'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install simple_notification
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
SimpleNotification generates the files in the /app folder, feel free to tweak it to fit specific needs.
|
28
|
+
|
29
|
+
First generate the necessary files:
|
30
|
+
|
31
|
+
$ rails g simple_notification
|
32
|
+
|
33
|
+
Add the routes for the view in /config/routes.rb:
|
34
|
+
|
35
|
+
resources :notifications
|
36
|
+
|
37
|
+
Add the following to /app/models/user.rb:
|
38
|
+
|
39
|
+
has_notifications
|
40
|
+
|
41
|
+
Add the following to any models that needs to be tracked:
|
42
|
+
|
43
|
+
can_be_tracked
|
44
|
+
|
45
|
+
And finally, add the following to /app/controllers/application_controller.rb:
|
46
|
+
|
47
|
+
include SimpleNotification::Methods
|
48
|
+
|
49
|
+
Now the setup is complete, the following are available:
|
50
|
+
|
51
|
+
Notifications generated by a user's activities:
|
52
|
+
|
53
|
+
@user.own_notifications
|
54
|
+
|
55
|
+
Notifications meant for a user:
|
56
|
+
|
57
|
+
@user.received_notifications
|
58
|
+
|
59
|
+
one can add .where(read: false) to obtain unread notifications
|
60
|
+
|
61
|
+
Method to **notify** a user of a tracked event:
|
62
|
+
|
63
|
+
notify(recipient, trackable, message)
|
64
|
+
|
65
|
+
which will generate a notification from current_user (guest if undefined) to the recipient with the message and trackable source.
|
66
|
+
|
67
|
+
Notifications are now working and can be displayed however desired, sample usages are:
|
68
|
+
|
69
|
+
render "notifications/show", notification_id: notification.id, read: false
|
70
|
+
render "notifications/show", notification_id: notification.id, read: true
|
71
|
+
|
72
|
+
Will will generate the following by default:
|
73
|
+
|
74
|
+
**Owner** message **Trackable** | This notification was sent to **Recipient** | Destroy
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
1. Fork it ( http://github.com/<my-github-username>/simple_notification/fork )
|
79
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
80
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
81
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
82
|
+
5. Create new Pull Request
|
83
|
+
|
84
|
+
## License
|
85
|
+
|
86
|
+
MIT License
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
require 'rails/generators/active_record'
|
4
|
+
|
5
|
+
class SimpleNotificationGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
|
9
|
+
def generate_migration
|
10
|
+
migration_template "create_notifications.rb.erb", "db/migrate/create_notifications.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
def generate_model
|
14
|
+
copy_file "notification.rb", "app/models/notification.rb"
|
15
|
+
end
|
16
|
+
|
17
|
+
def generate_controller
|
18
|
+
copy_file "notifications_controller.rb", "app/controllers/notifications_controller.rb"
|
19
|
+
end
|
20
|
+
|
21
|
+
def generate_view
|
22
|
+
empty_directory "app/views/notifications"
|
23
|
+
copy_file "notifications_index.html.haml", "app/views/notifications/index.html.haml"
|
24
|
+
copy_file "notifications_partial.html.haml", "app/views/notifications/_show.html.haml"
|
25
|
+
copy_file "notifications_css.css.scss", "app/assets/stylesheets/notifications.css.scss"
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def self.next_migration_number( dirname )
|
31
|
+
next_migration_number = current_migration_number(dirname) + 1
|
32
|
+
if ActiveRecord::Base.timestamped_migrations
|
33
|
+
[Time.now.utc.strftime("%Y%m%d%H%M%S%6N"), "%.20d" % next_migration_number].max
|
34
|
+
else
|
35
|
+
"%.3d" % next_migration_number
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CreateNotifications < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :notifications do |t|
|
4
|
+
t.integer :owner_id
|
5
|
+
t.integer :recipient_id
|
6
|
+
t.integer :trackable_id
|
7
|
+
t.string :trackable_type
|
8
|
+
t.string :message
|
9
|
+
t.boolean :read, default: false
|
10
|
+
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class NotificationsController < ApplicationController
|
2
|
+
before_action :set_notification, only: [:show, :edit, :update, :destroy]
|
3
|
+
|
4
|
+
# GET /notifications
|
5
|
+
# GET /notifications.json
|
6
|
+
def index
|
7
|
+
@notifications = Notification.all
|
8
|
+
end
|
9
|
+
|
10
|
+
# POST /notifications
|
11
|
+
# POST /notifications.json
|
12
|
+
def create
|
13
|
+
@notification = Notification.new(notification_params)
|
14
|
+
@notification.save
|
15
|
+
redirect_to :back
|
16
|
+
end
|
17
|
+
|
18
|
+
# DELETE /notifications/1
|
19
|
+
# DELETE /notifications/1.json
|
20
|
+
def destroy
|
21
|
+
@notification.destroy
|
22
|
+
respond_to do |format|
|
23
|
+
format.html { redirect_to :back }
|
24
|
+
format.json { head :no_content }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
# Use callbacks to share common setup or constraints between actions.
|
30
|
+
def set_notification
|
31
|
+
@notification = Notification.find(params[:id])
|
32
|
+
end
|
33
|
+
|
34
|
+
# Never trust parameters from the scary internet, only allow the white list through.
|
35
|
+
def notification_params
|
36
|
+
params.require(:notification).permit(:owner_id, :recipient_id, :trackable_id, :trackable_type, :message, :read)
|
37
|
+
end
|
38
|
+
end
|
File without changes
|
@@ -0,0 +1,31 @@
|
|
1
|
+
- @notification = Notification.find(notification_id)
|
2
|
+
- if read
|
3
|
+
- @notification.read = true
|
4
|
+
- @notification.save
|
5
|
+
.notification
|
6
|
+
%p
|
7
|
+
- if @notification.owner == nil
|
8
|
+
%b Guest
|
9
|
+
- else
|
10
|
+
%b
|
11
|
+
= link_to @notification.owner do
|
12
|
+
= @notification.owner.username
|
13
|
+
|
14
|
+
= @notification.message
|
15
|
+
|
16
|
+
- if @notification.trackable == nil
|
17
|
+
%b which has since been deleted
|
18
|
+
- else
|
19
|
+
%b
|
20
|
+
= link_to @notification.trackable do
|
21
|
+
= @notification.trackable_type
|
22
|
+
\|
|
23
|
+
This notification was sent to
|
24
|
+
- if @notification.recipient == nil
|
25
|
+
%b Deleted User
|
26
|
+
- else
|
27
|
+
%b
|
28
|
+
= link_to @notification.recipient do
|
29
|
+
= @notification.recipient.username
|
30
|
+
\|
|
31
|
+
= link_to 'Destroy', @notification, :method => :delete, :data => { :confirm => 'Are you sure?' }
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module SimpleNotification
|
2
|
+
module Methods
|
3
|
+
def notify(recipient, trackable, message)
|
4
|
+
owner = current_user
|
5
|
+
owner = User.new if owner == nil
|
6
|
+
|
7
|
+
if owner == recipient
|
8
|
+
return
|
9
|
+
end
|
10
|
+
|
11
|
+
notification = Notification.create(owner_id: owner.id, recipient_id: recipient.id, message: message)
|
12
|
+
trackable.notifications << notification
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class << ActiveRecord::Base
|
18
|
+
def can_be_tracked
|
19
|
+
has_many :notifications, as: :trackable
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple_notification/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simple_notification"
|
8
|
+
spec.version = SimpleNotification::VERSION
|
9
|
+
spec.authors = ["Cong Wang"]
|
10
|
+
spec.email = ["findcongwang@gmail.com"]
|
11
|
+
spec.summary = %q{Simple user notification module.}
|
12
|
+
spec.description = %q{User notification system assuming user systems is built using the User model and has current_user helper. This gem uses the Notification model so must not be taken.}
|
13
|
+
spec.homepage = "https://github.com/findcongwang"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake", "~> 0"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_notification
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cong Wang
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: User notification system assuming user systems is built using the User
|
42
|
+
model and has current_user helper. This gem uses the Notification model so must
|
43
|
+
not be taken.
|
44
|
+
email:
|
45
|
+
- findcongwang@gmail.com
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".gitignore"
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- lib/generators/simple_notification/USAGE
|
56
|
+
- lib/generators/simple_notification/simple_notification_generator.rb
|
57
|
+
- lib/generators/simple_notification/templates/create_notifications.rb.erb
|
58
|
+
- lib/generators/simple_notification/templates/notification.rb
|
59
|
+
- lib/generators/simple_notification/templates/notifications_controller.rb
|
60
|
+
- lib/generators/simple_notification/templates/notifications_css.css.scss
|
61
|
+
- lib/generators/simple_notification/templates/notifications_index.html.haml
|
62
|
+
- lib/generators/simple_notification/templates/notifications_partial.html.haml
|
63
|
+
- lib/simple_notification.rb
|
64
|
+
- lib/simple_notification/can_be_tracked.rb
|
65
|
+
- lib/simple_notification/has_notifications.rb
|
66
|
+
- lib/simple_notification/version.rb
|
67
|
+
- simple_notification.gemspec
|
68
|
+
homepage: https://github.com/findcongwang
|
69
|
+
licenses:
|
70
|
+
- MIT
|
71
|
+
metadata: {}
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.2.1
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: Simple user notification module.
|
92
|
+
test_files: []
|