undelivered 0.1.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
+ SHA256:
3
+ metadata.gz: cfdd3d769843215ea5a756fe31707fbb7f42f227de98a88b335cfd954d0b407d
4
+ data.tar.gz: 2c58a33b51bad5f506b2a468782bc8d3980c8000c55d0604cab4e99821c83721
5
+ SHA512:
6
+ metadata.gz: 6a9ea3e9d99c715ea7ca1fd88e6fe3dee78c875eef27f230c133d1d1d95b7d54298d943211e21b3958fc20eee5d86bc2852b00a5fee1612792a2281bff0b41bb
7
+ data.tar.gz: 2c7c946cfbfce390e5b291577a8116627e7dea6654372aa27684b5a28da980500dd100034ffdb71ffba9b2dbd1feeadb3c4aad05a2f9242c000f64bc45fbff41
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in undelivered.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 TODO: Write your name
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,115 @@
1
+ # Undelivered
2
+
3
+ Ruby gem to manage undelivered/ read status of ActiveRecord objects
4
+
5
+ Most of the logic shamelessly stolen from `unread` gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'undelivered'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Install migration yourself (as of now):
20
+
21
+ ```ruby
22
+ class CreateUndeliveredReadMarks < ActiveRecord::Migration[6.1]
23
+ def change
24
+ create_table :undelivered_read_marks do |t|
25
+ t.references :reader, polymorphic: { null: false }
26
+ t.references :readable, polymorphic: { null: false }
27
+ t.integer :status, index: true
28
+ t.datetime :timestamp
29
+
30
+ t.timestamps
31
+ end
32
+
33
+ add_index :undelivered_read_marks, [:reader_id, :reader_type, :readable_type, :readable_id, :status], name: 'undelivered_read_marks_reader_readable_status_index', unique: true
34
+ end
35
+ end
36
+ ```
37
+
38
+ Run migration:
39
+
40
+ $ rails db:migrate
41
+
42
+ ## Usage
43
+
44
+ By following idea from [`this`](https://github.com/ledermann/unread/issues/99) issue of `unread` gem.
45
+
46
+ ```ruby
47
+ class User < ApplicationRecord
48
+ acts_as_reader
49
+ end
50
+
51
+ class Conversation < ApplicationRecord
52
+ acts_as_readable on: :updated_at
53
+
54
+ has_many :messages, dependent: :destroy
55
+
56
+ def undelivered_messages(reader)
57
+ chain = messages
58
+ rm = find_read_mark(reader, :delivered) # this method comes from `undelivered` gem
59
+ if rm.present?
60
+ chain = chain.where('created_at > ?', rm.timestamp)
61
+ end
62
+ return chain
63
+ end
64
+
65
+ def unread_messages(reader)
66
+ chain = messages
67
+ rm = find_read_mark(reader, :read)
68
+ if rm.present?
69
+ chain = chain.where('created_at > ?', rm.timestamp)
70
+ end
71
+ return chain
72
+ end
73
+ end
74
+
75
+ class Message < ApplicationRecord
76
+ belongs_to :conversation, touch: true
77
+ end
78
+ ```
79
+
80
+ ```ruby
81
+ # Suppose we got 1 users and 1 conversation
82
+ current_user = User.find(1)
83
+
84
+ conversation = Conversation.find(1)
85
+
86
+ message1 = conversation.messages.create
87
+ message2 = conversation.messages.create
88
+
89
+ # Get undelivered messages for current_user, using method of conversation class
90
+ conversation.undelivered_messages(current_user)
91
+ # => [ message1, message2 ]
92
+
93
+ # Mark them as delivered for current_user
94
+ conversation.mark_as_delivered_for!(current_user)
95
+
96
+
97
+ # Get unread messages for current_user
98
+ conversation.unread_messages(current_user)
99
+ # => [ message1, message2 ]
100
+
101
+ # Mark them as read for current_user
102
+ conversation.mark_as_read_for!(current_user)
103
+ ```
104
+
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vivekmiyani/undelivered. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/vivekmiyani/undelivered/blob/master/CODE_OF_CONDUCT.md).
108
+
109
+ ## License
110
+
111
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
112
+
113
+ ## Code of Conduct
114
+
115
+ Everyone interacting in the Undelivered project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/vivekmiyani/undelivered/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "undelivered"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ 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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "undelivered/version"
4
+ require_relative "undelivered/read_mark"
5
+
6
+ module Undelivered
7
+ class Error < StandardError; end
8
+ end
9
+
10
+ ActiveSupport.on_load(:active_record) do
11
+ extend Undelivered::ReadMark::ClassMethods
12
+ end
@@ -0,0 +1,64 @@
1
+ module Undelivered
2
+ class ReadMark < ActiveRecord::Base
3
+ belongs_to :reader, polymorphic: true
4
+ belongs_to :readable, polymorphic: true
5
+
6
+ enum status: { delivered: 0, read: 1 }
7
+
8
+ validates :status, uniqueness: { scope: [ :reader, :readable ] }
9
+
10
+ module InstanceMethods
11
+
12
+ def mark_as_delivered_for!(reader)
13
+ ReadMark.transaction do
14
+ rm = find_or_build_read_mark(reader, :delivered)
15
+ rm.timestamp = self.send(read_mark_options[:on])
16
+ rm.save!
17
+ end
18
+ end
19
+
20
+ def mark_as_read_for!(reader)
21
+ ReadMark.transaction do
22
+ delivered_rm = find_or_build_read_mark(reader, :delivered)
23
+ delivered_rm.timestamp = self.send(read_mark_options[:on])
24
+ delivered_rm.save!
25
+ read_rm = find_or_build_read_mark(reader, :read)
26
+ read_rm.timestamp = self.send(read_mark_options[:on])
27
+ read_rm.save!
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def find_or_build_read_mark(reader, status)
34
+ read_marks.find_or_initialize_by(reader: reader, status: status)
35
+ end
36
+
37
+ def find_read_mark(reader, status)
38
+ read_marks.find_by(reader: reader, status: status)
39
+ end
40
+ end
41
+
42
+ module ClassMethods
43
+
44
+ def acts_as_reader
45
+ has_many :read_marks, class_name: 'Undelivered::ReadMark', as: :reader, dependent: :destroy
46
+ end
47
+
48
+ def acts_as_readable(options)
49
+ raise Error, '`on` must be included in readable options' unless options.include?(:on)
50
+
51
+ include ReadMark::InstanceMethods
52
+
53
+ class_attribute :read_mark_options
54
+ self.read_mark_options = options
55
+
56
+ has_many :read_marks, class_name: 'Undelivered::ReadMark', as: :readable, dependent: :destroy
57
+ end
58
+ end
59
+ end
60
+
61
+ def self.table_name_prefix
62
+ 'undelivered_'
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undelivered
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/undelivered/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "undelivered"
7
+ spec.version = Undelivered::VERSION
8
+ spec.authors = ["Vivek Miyani"]
9
+ spec.email = ["vivekmiyaniofficial@gmail.com"]
10
+
11
+ spec.summary = "Manages undelivered, delivered and read messages."
12
+ spec.description = "Manages undelivered, delivered and read messages."
13
+ spec.homepage = "https://github.com/vivekmiyani/undelivered"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ # Uncomment to register a new dependency of your gem
27
+ # spec.add_dependency "example-gem", "~> 1.0"
28
+
29
+ # For more information and examples about making a new gem, checkout our
30
+ # guide at: https://bundler.io/guides/creating_gem.html
31
+
32
+ spec.add_dependency "activerecord", '> 3'
33
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: undelivered
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vivek Miyani
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-02-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ description: Manages undelivered, delivered and read messages.
28
+ email:
29
+ - vivekmiyaniofficial@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - CODE_OF_CONDUCT.md
36
+ - Gemfile
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - bin/console
41
+ - bin/setup
42
+ - lib/undelivered.rb
43
+ - lib/undelivered/read_mark.rb
44
+ - lib/undelivered/version.rb
45
+ - undelivered.gemspec
46
+ homepage: https://github.com/vivekmiyani/undelivered
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.3.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.2.3
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Manages undelivered, delivered and read messages.
69
+ test_files: []