hoc_notifications 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 62be5823a8756c4bf02ede76c304d15078c549f8
4
+ data.tar.gz: 4294a7886a6259785182cfba2c92258ad96d501c
5
+ SHA512:
6
+ metadata.gz: b6ae70fdc2eb7c4a79d3f031627ab431aab1b3b098c0de33e4854e8a22a90a94a664563b3875a53d477fe25920b10071e1bbb373d66d57aa4aea3b5c62a25e75
7
+ data.tar.gz: 563d2003c56c0a58107f7ab94a7f5f0e666bec028cf2a23908ed8728def91262674bba493e348b2266dcd9a2de65e44fac2a3d9647ad9e4b40313041d43f752e
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in hoc_notifications.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Gert Lavsen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # HocNotifications
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hoc_notifications`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hoc_notifications'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hoc_notifications
22
+
23
+ ## Usage
24
+
25
+ ### Make application ready for hoc_notifications
26
+ Install by running:
27
+
28
+ $ rails generate hoc_notifications:install
29
+
30
+
31
+ $ rake db:migrate
32
+ This will create, models and migrations
33
+
34
+ ### Extend models
35
+
36
+ #### Receiver
37
+ Add the `acts_as_hoc_notications_receiver` to models you want to be able to receive notifications and override `handle_received_notification(hoc_notification)` to get notified in the model when a notifications is received. This can be used to eg. send a push notification with acts_s_hoc_pushable
38
+
39
+ ```ruby
40
+ #app/models/user.rb
41
+ class User < ApplicationRecord
42
+ acts_as_hoc_notifications_receiver
43
+ #acts_as_hoc_pushable
44
+
45
+ def handle_received_notification(hoc_notification)
46
+ # handle notification here. eg send push notification
47
+ #send_push_notification(....)
48
+ end
49
+ end
50
+ ```
51
+ The model now has a has_many `received_notifications` relation.
52
+ The model also have a method called `mark_as_seen(id)` which can be used to mark a notification as seen.
53
+
54
+
55
+ #### Sender
56
+ Add the `acts_as_hoc_notications_sender` to models you want to be able to be used as sender for notifications
57
+ ```ruby
58
+ #app/models/user.rb
59
+ class User < ApplicationRecord
60
+ acts_as_hoc_notifications_receiver
61
+ acts_as_hoc_notifications_sender
62
+ #acts_as_hoc_pushable
63
+
64
+ def handle_received_notification(hoc_notification)
65
+ # handle notification here. eg send push notification
66
+ #send_push_notification(....)
67
+ end
68
+ end
69
+ ```
70
+ As seen the receiver and sender can be the same model.
71
+ This extend the model with a has_many `sent_notifications` relation.
72
+
73
+ #### Notifiable
74
+ Add the `acts_as_hoc_notifications_notifiable` to models you want to be able to notify about.
75
+ ```ruby
76
+ class Comment < ApplicationRecord
77
+ acts_as_hoc_notifications_notifiable
78
+ belongs_to :user
79
+ belongs_to :post
80
+
81
+ after_create :create_notification
82
+ def create_notification
83
+ Comment.create_notification(
84
+ sender: user,
85
+ recipients: (post.commented_users.to_a + [post.user] - [user]).uniq,
86
+ notifiable: self,
87
+ action: :create,
88
+ title: "Created comment",
89
+ message: "#{user.name} commented on #{post.title}",
90
+ data: nil
91
+ )
92
+ end
93
+ end
94
+ class Post < ApplicationRecord
95
+ belongs_to :user
96
+ has_many :comments
97
+ has_many :commented_users, through: :comments, source: :user
98
+
99
+ acts_as_hoc_notifications_notifiable
100
+ end
101
+
102
+
103
+ ```
104
+ This extend the model with a has_many `notifications` relation and the `create_notification` method
105
+ ActiveRecords callbacks, like `after_create` can be used to make a notification.
106
+
107
+ ## License
108
+
109
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hoc_notifications"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "hoc_notifications/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hoc_notifications"
8
+ spec.version = HocNotifications::VERSION
9
+ spec.authors = ["Gert Lavsen"]
10
+ spec.email = ["gert@houseofcode.io"]
11
+
12
+ spec.summary = "Notifications made easy"
13
+ spec.description = "Notifications made easy"
14
+ spec.homepage = "https://github.com/house-of-code/hoc_notifications"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16.a"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ end
@@ -0,0 +1,28 @@
1
+ require 'rails/generators/migration'
2
+
3
+ module HocNotifications
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ #def copy_initializer_file
11
+ # template "initializer.rb", "config/initializers/hoc_notifications.rb"
12
+ #end
13
+
14
+ def copy_migration_files
15
+ migration_template "migration/create_hoc_notifications.rb", "db/migrate/create_hoc_notifications.rb"
16
+ end
17
+
18
+ def self.next_migration_number(dirname)
19
+ if ActiveRecord::Base.timestamped_migrations
20
+ sleep 1 # make sure each time we get a different timestamp
21
+ Time.new.utc.strftime("%Y%m%d%H%M%S")
22
+ else
23
+ "%.3d" % (current_migration_number(dirname) + 1)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ class CreateHocNotifications < ActiveRecord::Migration
2
+ def change
3
+ create_table :hoc_notifications do |t|
4
+ t.integer :recipient_id
5
+ t.string :recipient_type
6
+
7
+ t.integer :sender_id
8
+ t.string :sender_type
9
+
10
+ t.integer :notifiable_id
11
+ t.string :notifiable_type
12
+
13
+ t.string :title, null: false, default: ""
14
+ t.string :message, null: false, default: ""
15
+
16
+ t.string :action, null: false, default: ""
17
+
18
+ t.text :data, null: false, default: ""
19
+
20
+ t.datetime :seen_at
21
+
22
+ t.timestamps
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,36 @@
1
+ module HocNotifications
2
+ class HocNotification < ActiveRecord::Base
3
+ belongs_to :recipient, polymorphic: true
4
+ belongs_to :sender, polymorphic: true
5
+ belongs_to :notifiable, polymorphic: true
6
+
7
+ scope :unread, -> { where(seen_at: nil) }
8
+
9
+ serialize :data, Hash
10
+
11
+ after_create -> {
12
+ return if recipient.nil?
13
+ if recipient.class.method_defined? :handle_received_notification
14
+ recipient.handle_received_notification(self)
15
+ end
16
+ }
17
+
18
+ def self.create_notifications(sender:, recipients:, notifiable:, action:, title:, message:, **data)
19
+ recipients.each do |recipient|
20
+ create(sender: sender,
21
+ recipient: recipient,
22
+ notifiable: notifiable,
23
+ action: action,
24
+ data: data,
25
+ seen_at: nil,
26
+ title: title,
27
+ message: message
28
+ )
29
+ end
30
+ end
31
+
32
+ def mark_as_seen
33
+ update_attributes(seen_at: Time.current)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,32 @@
1
+ module HocNotifications
2
+ module ActsAsHocNotificationsNotifiable
3
+ extend ActiveSupport::Concern
4
+ included do
5
+ end
6
+
7
+
8
+ module ClassMethods
9
+ def acts_as_hoc_notifications_notifiable(**options)
10
+ has_many :notifications,
11
+ as: :notifiable,
12
+ class_name: 'HocNotifications::HocNotification',
13
+ dependent: :nullify
14
+
15
+ end
16
+
17
+ def create_notification(sender:, recipients:, notifiable:, action:, title:, message:, **data)
18
+ HocNotifications::HocNotification.create_notifications(
19
+ sender: sender,
20
+ recipients: recipients,
21
+ notifiable: notifiable,
22
+ action: action,
23
+ title: title,
24
+ message: message,
25
+ **data
26
+ )
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+ ActiveRecord::Base.send :include, HocNotifications::ActsAsHocNotificationsNotifiable
@@ -0,0 +1,27 @@
1
+ module HocNotifications
2
+ module ActsAsHocNotificationsReceiver
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_hoc_notifications_receiver(_options = {})
10
+ has_many :received_notifications,
11
+ as: :recipient,
12
+ class_name: 'HocNotifications::HocNotification',
13
+ dependent: :destroy
14
+ end
15
+ end
16
+
17
+ def handle_received_notification(hoc_notification)
18
+ end
19
+
20
+ def mark_notification_as_read(id)
21
+ received_notifications.find(id).mark_as_seen
22
+ end
23
+
24
+ end
25
+ end
26
+ ActiveRecord::Base.send :include,
27
+ HocNotifications::ActsAsHocNotificationsReceiver
@@ -0,0 +1,20 @@
1
+ module HocNotifications
2
+ module ActsAsHocNotificationsSender
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+
9
+ module ClassMethods
10
+ def acts_as_hoc_notifications_sender(_options = {})
11
+ has_many :sent_notifications,
12
+ as: :sender,
13
+ class_name: 'HocNotifications::HocNotification',
14
+ dependent: :destroy
15
+ end
16
+ end
17
+ end
18
+ end
19
+ ActiveRecord::Base.send :include,
20
+ HocNotifications::ActsAsHocNotificationsSender
@@ -0,0 +1,8 @@
1
+ module HocNotifications
2
+ class HocNotificationAction
3
+ def initialize(action:, title:, message:, sender:, recipients:, notifiable:, data: )
4
+
5
+ end
6
+ attr_accessor :action, :message, :title, :sender, :recipients, :notifiable, :data
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module HocNotifications
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,9 @@
1
+ require "hoc_notifications/version"
2
+ require "hoc_notifications/acts_as_hoc_notifications_receiver"
3
+ require "hoc_notifications/acts_as_hoc_notifications_sender"
4
+ require "hoc_notifications/acts_as_hoc_notifications_notifiable"
5
+ require "hoc_notifications/hoc_notification_action"
6
+ require "hoc_notifications/active_record/hoc_notification.rb"
7
+ module HocNotifications
8
+ # Your code goes here...
9
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hoc_notifications
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Gert Lavsen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-04 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.16.a
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.16.a
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Notifications made easy
42
+ email:
43
+ - gert@houseofcode.io
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - bin/console
54
+ - bin/setup
55
+ - hoc_notifications.gemspec
56
+ - lib/generators/hoc_notifications/install_generator.rb
57
+ - lib/generators/hoc_notifications/templates/migration/create_hoc_notifications.rb
58
+ - lib/hoc_notifications.rb
59
+ - lib/hoc_notifications/active_record/hoc_notification.rb
60
+ - lib/hoc_notifications/acts_as_hoc_notifications_notifiable.rb
61
+ - lib/hoc_notifications/acts_as_hoc_notifications_receiver.rb
62
+ - lib/hoc_notifications/acts_as_hoc_notifications_sender.rb
63
+ - lib/hoc_notifications/hoc_notification_action.rb
64
+ - lib/hoc_notifications/version.rb
65
+ homepage: https://github.com/house-of-code/hoc_notifications
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.6.13
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Notifications made easy
89
+ test_files: []