acts-as-messageable 0.5.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +8 -0
- data/.github/workflows/test.yml +152 -0
- data/.rubocop.yml +14 -1
- data/.rubocop_todo.yml +49 -7
- data/.ruby-version +1 -0
- data/Appraisals +11 -0
- data/Dockerfile +2 -1
- data/Gemfile +19 -13
- data/Gemfile.lock +186 -97
- data/README.md +2 -3
- data/Rakefile +6 -1
- data/VERSION +1 -1
- data/acts-as-messageable.gemspec +193 -40
- data/bin/tapioca +29 -0
- data/docker-compose.yml +82 -71
- data/gemfiles/rails_3.2.gemfile +14 -9
- data/gemfiles/rails_4.2.gemfile +14 -9
- data/gemfiles/rails_5.2.gemfile +14 -9
- data/gemfiles/rails_6.0.gemfile +14 -9
- data/gemfiles/rails_7.0.gemfile +29 -0
- data/gemfiles/rails_master.gemfile +15 -9
- data/lib/acts-as-messageable.rb +1 -0
- data/lib/acts_as_messageable/message.rb +62 -5
- data/lib/acts_as_messageable/model.rb +71 -20
- data/lib/acts_as_messageable/rails3.rb +27 -2
- data/lib/acts_as_messageable/rails4.rb +31 -4
- data/lib/acts_as_messageable/rails6.rb +34 -2
- data/lib/acts_as_messageable/railtie.rb +1 -0
- data/lib/acts_as_messageable/relation.rb +18 -2
- data/lib/acts_as_messageable/scopes.rb +11 -0
- data/lib/acts_as_messageable.rb +18 -0
- data/lib/generators/acts_as_messageable/migration/migration_generator.rb +4 -0
- data/lib/generators/acts_as_messageable/migration/templates/migration.rb +1 -0
- data/lib/generators/acts_as_messageable/migration/templates/migration_indexes.rb +1 -0
- data/lib/generators/acts_as_messageable/migration/templates/migration_opened_as_datetime.rb +1 -0
- data/lib/generators/acts_as_messageable/migration/templates/migration_permanent.rb +1 -0
- data/sorbet/config +4 -0
- data/sorbet/rbi/annotations/actionpack.rbi +428 -0
- data/sorbet/rbi/annotations/actionview.rbi +77 -0
- data/sorbet/rbi/annotations/activerecord.rbi +18 -0
- data/sorbet/rbi/annotations/activesupport.rbi +52 -0
- data/sorbet/rbi/annotations/faraday.rbi +17 -0
- data/sorbet/rbi/annotations/railties.rbi +58 -0
- data/sorbet/rbi/annotations/rainbow.rbi +269 -0
- data/sorbet/rbi/dsl/abstract_controller/caching/fragments.rbi +23 -0
- data/sorbet/rbi/dsl/abstract_controller/caching.rbi +30 -0
- data/sorbet/rbi/dsl/abstract_controller/callbacks.rbi +23 -0
- data/sorbet/rbi/dsl/abstract_controller/helpers.rbi +23 -0
- data/sorbet/rbi/dsl/abstract_controller/rendering.rbi +9 -0
- data/sorbet/rbi/dsl/abstract_controller/url_for.rbi +23 -0
- data/sorbet/rbi/dsl/action_controller/caching.rbi +29 -0
- data/sorbet/rbi/dsl/action_controller/conditional_get.rbi +23 -0
- data/sorbet/rbi/dsl/action_controller/content_security_policy.rbi +31 -0
- data/sorbet/rbi/dsl/action_controller/data_streaming.rbi +9 -0
- data/sorbet/rbi/dsl/action_controller/etag_with_flash.rbi +24 -0
- data/sorbet/rbi/dsl/action_controller/etag_with_template_digest.rbi +30 -0
- data/sorbet/rbi/dsl/action_controller/flash.rbi +19 -0
- data/sorbet/rbi/dsl/action_controller/form_builder.rbi +19 -0
- data/sorbet/rbi/dsl/action_controller/helpers.rbi +36 -0
- data/sorbet/rbi/dsl/action_controller/params_wrapper.rbi +23 -0
- data/sorbet/rbi/dsl/action_controller/redirecting.rbi +24 -0
- data/sorbet/rbi/dsl/action_controller/renderers/all.rbi +24 -0
- data/sorbet/rbi/dsl/action_controller/renderers.rbi +23 -0
- data/sorbet/rbi/dsl/action_controller/request_forgery_protection.rbi +31 -0
- data/sorbet/rbi/dsl/action_controller/rescue.rbi +24 -0
- data/sorbet/rbi/dsl/action_controller/test_case/behavior.rbi +24 -0
- data/sorbet/rbi/dsl/action_controller/url_for.rbi +24 -0
- data/sorbet/rbi/dsl/action_dispatch/routing/url_for.rbi +23 -0
- data/sorbet/rbi/dsl/action_view/helpers/form_helper.rbi +10 -0
- data/sorbet/rbi/dsl/action_view/helpers/form_tag_helper.rbi +10 -0
- data/sorbet/rbi/dsl/action_view/helpers/text_helper.rbi +9 -0
- data/sorbet/rbi/dsl/action_view/helpers.rbi +10 -0
- data/sorbet/rbi/dsl/action_view/layouts.rbi +24 -0
- data/sorbet/rbi/dsl/action_view/rendering.rbi +9 -0
- data/sorbet/rbi/dsl/active_model/attribute_methods.rbi +27 -0
- data/sorbet/rbi/dsl/active_model/attributes.rbi +34 -0
- data/sorbet/rbi/dsl/active_model/dirty.rbi +28 -0
- data/sorbet/rbi/dsl/active_model/serializers/json.rbi +22 -0
- data/sorbet/rbi/dsl/active_model/validations/callbacks.rbi +22 -0
- data/sorbet/rbi/dsl/active_model/validations.rbi +27 -0
- data/sorbet/rbi/dsl/active_record/attribute_methods/dirty.rbi +38 -0
- data/sorbet/rbi/dsl/active_record/attribute_methods/time_zone_conversion.rbi +32 -0
- data/sorbet/rbi/dsl/active_record/attribute_methods.rbi +53 -0
- data/sorbet/rbi/dsl/active_record/attributes.rbi +19 -0
- data/sorbet/rbi/dsl/active_record/callbacks.rbi +22 -0
- data/sorbet/rbi/dsl/active_record/core.rbi +55 -0
- data/sorbet/rbi/dsl/active_record/encryption/encryptable_record.rbi +23 -0
- data/sorbet/rbi/dsl/active_record/inheritance.rbi +27 -0
- data/sorbet/rbi/dsl/active_record/integration.rbi +32 -0
- data/sorbet/rbi/dsl/active_record/locking/optimistic.rbi +22 -0
- data/sorbet/rbi/dsl/active_record/model_schema.rbi +52 -0
- data/sorbet/rbi/dsl/active_record/nested_attributes.rbi +22 -0
- data/sorbet/rbi/dsl/active_record/readonly_attributes.rbi +19 -0
- data/sorbet/rbi/dsl/active_record/reflection.rbi +32 -0
- data/sorbet/rbi/dsl/active_record/scoping/default.rbi +23 -0
- data/sorbet/rbi/dsl/active_record/scoping.rbi +23 -0
- data/sorbet/rbi/dsl/active_record/serialization.rbi +22 -0
- data/sorbet/rbi/dsl/active_record/signed_id.rbi +22 -0
- data/sorbet/rbi/dsl/active_record/test_fixtures.rbi +58 -0
- data/sorbet/rbi/dsl/active_record/timestamp.rbi +23 -0
- data/sorbet/rbi/dsl/active_record/validations.rbi +28 -0
- data/sorbet/rbi/dsl/active_support/actionable_error.rbi +23 -0
- data/sorbet/rbi/dsl/active_support/callbacks.rbi +22 -0
- data/sorbet/rbi/dsl/active_support/rescuable.rbi +23 -0
- data/sorbet/rbi/dsl/active_support/testing/file_fixtures.rbi +22 -0
- data/sorbet/rbi/gems/actionpack@7.0.4.3.rbi +19265 -0
- data/sorbet/rbi/gems/actionview@7.0.4.3.rbi +15482 -0
- data/sorbet/rbi/gems/activemodel@7.0.4.3.rbi +6025 -0
- data/sorbet/rbi/gems/activerecord@7.0.4.3.rbi +37852 -0
- data/sorbet/rbi/gems/activesupport@7.0.4.3.rbi +18788 -0
- data/sorbet/rbi/gems/addressable@2.4.0.rbi +8 -0
- data/sorbet/rbi/gems/appraisal@2.4.1.rbi +584 -0
- data/sorbet/rbi/gems/ast@2.4.2.rbi +584 -0
- data/sorbet/rbi/gems/builder@3.2.4.rbi +8 -0
- data/sorbet/rbi/gems/coderay@1.1.3.rbi +3426 -0
- data/sorbet/rbi/gems/commander@4.6.0.rbi +8 -0
- data/sorbet/rbi/gems/concurrent-ruby@1.2.2.rbi +11545 -0
- data/sorbet/rbi/gems/coveralls_reborn@0.27.0.rbi +8 -0
- data/sorbet/rbi/gems/crass@1.0.6.rbi +622 -0
- data/sorbet/rbi/gems/descendants_tracker@0.0.4.rbi +8 -0
- data/sorbet/rbi/gems/diff-lcs@1.5.0.rbi +1079 -0
- data/sorbet/rbi/gems/docile@1.4.0.rbi +8 -0
- data/sorbet/rbi/gems/erubi@1.12.0.rbi +146 -0
- data/sorbet/rbi/gems/faraday@0.9.2.rbi +964 -0
- data/sorbet/rbi/gems/git@1.11.0.rbi +2700 -0
- data/sorbet/rbi/gems/github_api@0.16.0.rbi +8 -0
- data/sorbet/rbi/gems/hashie@5.0.0.rbi +8 -0
- data/sorbet/rbi/gems/highline@2.0.3.rbi +8 -0
- data/sorbet/rbi/gems/i18n@1.12.0.rbi +2296 -0
- data/sorbet/rbi/gems/jeweler@2.3.9.rbi +1591 -0
- data/sorbet/rbi/gems/json@2.6.3.rbi +1541 -0
- data/sorbet/rbi/gems/jwt@2.5.0.rbi +8 -0
- data/sorbet/rbi/gems/loofah@2.19.1.rbi +904 -0
- data/sorbet/rbi/gems/method_source@1.0.0.rbi +272 -0
- data/sorbet/rbi/gems/mime-types@2.99.3.rbi +8 -0
- data/sorbet/rbi/gems/minitest@5.18.0.rbi +1491 -0
- data/sorbet/rbi/gems/multi_json@1.15.0.rbi +8 -0
- data/sorbet/rbi/gems/multi_xml@0.6.0.rbi +8 -0
- data/sorbet/rbi/gems/multipart-post@2.2.3.rbi +8 -0
- data/sorbet/rbi/gems/netrc@0.11.0.rbi +158 -0
- data/sorbet/rbi/gems/nokogiri@1.14.2.rbi +7244 -0
- data/sorbet/rbi/gems/oauth2@1.4.8.rbi +8 -0
- data/sorbet/rbi/gems/parallel@1.22.1.rbi +277 -0
- data/sorbet/rbi/gems/pg@1.4.6.rbi +2574 -0
- data/sorbet/rbi/gems/polyfill@1.9.0.rbi +8 -0
- data/sorbet/rbi/gems/pry@0.14.2.rbi +10081 -0
- data/sorbet/rbi/gems/psych@4.0.6.rbi +1819 -0
- data/sorbet/rbi/gems/racc@1.6.2.rbi +155 -0
- data/sorbet/rbi/gems/rack-test@2.0.2.rbi +943 -0
- data/sorbet/rbi/gems/rack@2.2.6.4.rbi +5659 -0
- data/sorbet/rbi/gems/rails-dom-testing@2.0.3.rbi +455 -0
- data/sorbet/rbi/gems/rails-html-sanitizer@1.5.0.rbi +685 -0
- data/sorbet/rbi/gems/railties@7.0.4.3.rbi +4553 -0
- data/sorbet/rbi/gems/rainbow@3.1.1.rbi +8 -0
- data/sorbet/rbi/gems/rake@13.0.6.rbi +2959 -0
- data/sorbet/rbi/gems/rbi@0.0.16.rbi +3008 -0
- data/sorbet/rbi/gems/rbs@2.8.0.rbi +8 -0
- data/sorbet/rbi/gems/rchardet@1.8.0.rbi +1078 -0
- data/sorbet/rbi/gems/rdoc@6.5.0.rbi +12441 -0
- data/sorbet/rbi/gems/regexp_parser@2.7.0.rbi +8 -0
- data/sorbet/rbi/gems/rexml@3.2.5.rbi +8 -0
- data/sorbet/rbi/gems/rspec-core@3.12.0.rbi +10798 -0
- data/sorbet/rbi/gems/rspec-expectations@3.12.0.rbi +8090 -0
- data/sorbet/rbi/gems/rspec-mocks@3.12.0.rbi +5296 -0
- data/sorbet/rbi/gems/rspec-support@3.12.0.rbi +1617 -0
- data/sorbet/rbi/gems/rspec@3.12.0.rbi +88 -0
- data/sorbet/rbi/gems/ruby-progressbar@1.13.0.rbi +8 -0
- data/sorbet/rbi/gems/safe_type@1.1.1.rbi +8 -0
- data/sorbet/rbi/gems/semver2@3.4.2.rbi +8 -0
- data/sorbet/rbi/gems/simplecov-html@0.12.3.rbi +8 -0
- data/sorbet/rbi/gems/simplecov@0.22.0.rbi +8 -0
- data/sorbet/rbi/gems/simplecov_json_formatter@0.1.4.rbi +8 -0
- data/sorbet/rbi/gems/sorbet-coerce@0.7.0.rbi +8 -0
- data/sorbet/rbi/gems/stringio@3.0.2.rbi +8 -0
- data/sorbet/rbi/gems/sync@0.5.0.rbi +8 -0
- data/sorbet/rbi/gems/term-ansicolor@1.7.1.rbi +8 -0
- data/sorbet/rbi/gems/thor@1.2.1.rbi +3956 -0
- data/sorbet/rbi/gems/thread_safe@0.3.6.rbi +8 -0
- data/sorbet/rbi/gems/timecop@0.9.6.rbi +361 -0
- data/sorbet/rbi/gems/tins@1.32.1.rbi +8 -0
- data/sorbet/rbi/gems/tzinfo@2.0.6.rbi +5917 -0
- data/sorbet/rbi/gems/unicode-display_width@2.4.2.rbi +8 -0
- data/sorbet/rbi/gems/unparser@0.6.7.rbi +4515 -0
- data/sorbet/rbi/gems/webrick@1.7.0.rbi +2553 -0
- data/sorbet/rbi/gems/yard@0.9.28.rbi +17954 -0
- data/sorbet/rbi/gems/zeitwerk@2.6.7.rbi +966 -0
- data/sorbet/rbi/models/acts-as-messageable/message.rbi +668 -0
- data/sorbet/rbi/models/acts-as-messageable/user.rbi +343 -0
- data/sorbet/rbi/rails-rbi/active_record_base.rbi +119 -0
- data/sorbet/rbi/rails-rbi/active_record_relation.rbi +180 -0
- data/sorbet/rbi/shims/activerecord.rbi +4 -0
- data/sorbet/rbi/shims/model.rbi +9 -0
- data/sorbet/tapioca/config.yml +10 -0
- data/sorbet/tapioca/pre_require.rb +5 -0
- data/sorbet/tapioca/require.rb +10 -0
- data/spec/acts_as_messageable_spec.rb +7 -4
- data/spec/custom_class_spec.rb +1 -0
- data/spec/custom_required_spec.rb +8 -4
- data/spec/group_messages_spec.rb +1 -0
- data/spec/migrations_spec.rb +2 -1
- data/spec/spec_helper.rb +19 -9
- data/spec/support/admin.rb +3 -0
- data/spec/support/custom_message.rb +3 -0
- data/spec/support/custom_message_uuid.rb +1 -0
- data/spec/support/custom_search_user.rb +3 -0
- data/spec/support/men.rb +1 -0
- data/spec/support/send_message.rb +8 -1
- data/spec/support/table_schema.rb +7 -6
- data/spec/support/user.rb +3 -0
- data/spec/support/uuid_user.rb +3 -0
- data/tasks/types.rake +46 -0
- metadata +261 -11
- data/.travis.yml +0 -55
@@ -6,18 +6,24 @@ source 'http://rubygems.org'
|
|
6
6
|
|
7
7
|
gem 'activerecord', git: 'https://github.com/rails/rails.git'
|
8
8
|
gem 'activesupport', git: 'https://github.com/rails/rails.git'
|
9
|
-
gem 'ancestry'
|
9
|
+
gem 'ancestry', require: false
|
10
10
|
gem 'railties', git: 'https://github.com/rails/rails.git'
|
11
|
+
gem 'sorbet-rails', require: false
|
12
|
+
gem 'sorbet-static-and-runtime', require: false
|
11
13
|
|
12
14
|
group :development do
|
13
|
-
gem 'appraisal'
|
14
|
-
gem '
|
15
|
-
gem 'jeweler'
|
16
|
-
gem 'pg'
|
17
|
-
gem 'pry'
|
18
|
-
gem 'rspec'
|
15
|
+
gem 'appraisal', require: false
|
16
|
+
gem 'coveralls_reborn', require: false
|
17
|
+
gem 'jeweler', require: false
|
18
|
+
gem 'pg', git: 'https://github.com/ged/ruby-pg.git'
|
19
|
+
gem 'pry', require: false
|
20
|
+
gem 'rspec', require: false
|
19
21
|
gem 'rubocop', require: false
|
22
|
+
gem 'rubocop-sorbet', require: false
|
23
|
+
gem 'sord', require: false
|
20
24
|
gem 'sqlite3'
|
21
|
-
gem '
|
22
|
-
gem '
|
25
|
+
gem 'tapioca', github: 'Shopify/tapioca', require: false
|
26
|
+
gem 'timecop', require: false
|
27
|
+
gem 'unparser', require: false
|
28
|
+
gem 'yard', require: false
|
23
29
|
end
|
data/lib/acts-as-messageable.rb
CHANGED
@@ -1,34 +1,53 @@
|
|
1
|
+
# typed: strict
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
|
-
require 'ancestry'
|
4
|
-
|
5
4
|
module ActsAsMessageable
|
6
5
|
class Message < ::ActiveRecord::Base
|
6
|
+
extend T::Sig
|
7
|
+
|
7
8
|
include ActsAsMessageable::Scopes
|
9
|
+
include Ancestry::InstanceMethods
|
8
10
|
|
9
11
|
has_ancestry
|
10
12
|
|
11
13
|
belongs_to :received_messageable, polymorphic: true
|
12
14
|
belongs_to :sent_messageable, polymorphic: true
|
13
15
|
|
16
|
+
sig { returns(T.nilable(T::Boolean)) }
|
14
17
|
attr_accessor :removed, :restored
|
15
18
|
|
16
19
|
cattr_accessor :required
|
17
20
|
|
21
|
+
sig { params(args: T.untyped, kwargs: T.untyped).void }
|
22
|
+
def initialize(*args, **kwargs)
|
23
|
+
@removed = T.let(false, T.nilable(T::Boolean))
|
24
|
+
@restored = T.let(false, T.nilable(T::Boolean))
|
25
|
+
|
26
|
+
super
|
27
|
+
end
|
28
|
+
|
18
29
|
ActsAsMessageable.rails_api.new(self).attr_accessible(
|
19
30
|
:topic, :body, :opened, :opened_at, :recipient_permanent_delete,
|
20
31
|
:recipient_delete, :sender_permanent_delete, :sender_delete
|
21
32
|
)
|
22
33
|
ActsAsMessageable.rails_api.new(self).default_scope('created_at desc')
|
23
34
|
|
35
|
+
# @return [Boolean] whether the message has been read
|
36
|
+
sig { returns(T::Boolean) }
|
24
37
|
def open?
|
25
38
|
opened?
|
26
39
|
end
|
27
40
|
|
41
|
+
# @return [Boolean] whether the message has been read
|
42
|
+
# @see open?
|
43
|
+
sig { returns(T::Boolean) }
|
28
44
|
def opened?
|
29
45
|
opened_at.present? || super
|
30
46
|
end
|
31
47
|
|
48
|
+
# Method open message (will mark message as read)
|
49
|
+
# @return [Boolean] whether the message has been open
|
50
|
+
sig { returns(T::Boolean) }
|
32
51
|
def open
|
33
52
|
ActsAsMessageable.rails_api.new(self).update_attributes!(opened_at: DateTime.now)
|
34
53
|
ActsAsMessageable.rails_api.new(self).update_attributes!(opened: true)
|
@@ -37,6 +56,9 @@ module ActsAsMessageable
|
|
37
56
|
alias mark_as_read open
|
38
57
|
alias read open
|
39
58
|
|
59
|
+
# Method close message (will mark message as unread)
|
60
|
+
# @return [Boolean] whether the message has been closed
|
61
|
+
sig { returns(T::Boolean) }
|
40
62
|
def close
|
41
63
|
ActsAsMessageable.rails_api.new(self).update_attributes!(opened_at: nil)
|
42
64
|
ActsAsMessageable.rails_api.new(self).update_attributes!(opened: false)
|
@@ -45,33 +67,68 @@ module ActsAsMessageable
|
|
45
67
|
alias mark_as_unread close
|
46
68
|
alias unread close
|
47
69
|
|
48
|
-
|
49
|
-
|
70
|
+
sig { returns(ActiveRecord::Base) }
|
71
|
+
def from
|
72
|
+
sent_messageable
|
73
|
+
end
|
74
|
+
|
75
|
+
sig { returns(ActiveRecord::Base) }
|
76
|
+
def to
|
77
|
+
received_messageable
|
78
|
+
end
|
50
79
|
|
80
|
+
# @param [ActiveRecord::Base] user
|
81
|
+
# @return [ActiveRecord::Base] real receiver of the mssage
|
82
|
+
sig { params(user: ActiveRecord::Base).returns(ActiveRecord::Base) }
|
51
83
|
def real_receiver(user)
|
52
84
|
user == from ? to : from
|
53
85
|
end
|
54
86
|
|
87
|
+
# @return [Boolean] whether user is participant of group message
|
88
|
+
# @param [ActiveRecord::Base] user
|
89
|
+
sig { params(user: T.untyped).returns(T::Boolean) }
|
55
90
|
def participant?(user)
|
56
91
|
user.class.group_messages || (to == user) || (from == user)
|
57
92
|
end
|
58
93
|
|
94
|
+
# @return [Object] conversation tree
|
95
|
+
sig { returns(T.untyped) }
|
59
96
|
def conversation
|
60
97
|
root.subtree
|
61
98
|
end
|
62
99
|
|
100
|
+
# Method will mark message as removed
|
101
|
+
# @return [TrueClass]
|
102
|
+
sig { returns(TrueClass) }
|
63
103
|
def delete
|
64
104
|
self.removed = true
|
65
105
|
end
|
66
106
|
|
107
|
+
# Method will mark message as removed
|
108
|
+
# @return [TrueClass]
|
109
|
+
sig { returns(TrueClass) }
|
67
110
|
def restore
|
68
111
|
self.restored = true
|
69
112
|
end
|
70
113
|
|
114
|
+
# Reply to given message
|
115
|
+
# @param [Hash] args
|
116
|
+
# @option args [String] topic Topic of the message
|
117
|
+
# @option args [String] body Body of the message
|
118
|
+
# @return [ActsAsMessageable::Message] a message that is a response to a given message
|
119
|
+
# @return [Boolean] when user is not participant of the message
|
120
|
+
# @see ActsAsMessageable::Model::InstanceMethods#reply_to
|
121
|
+
sig do
|
122
|
+
params(args: T.any(T::Hash[String, String],
|
123
|
+
String)).returns(T.any(ActsAsMessageable::Message, T::Boolean, ActiveRecord::Base))
|
124
|
+
end
|
71
125
|
def reply(*args)
|
72
|
-
to.reply_to(self, *args)
|
126
|
+
T.unsafe(to).reply_to(self, *args)
|
73
127
|
end
|
74
128
|
|
129
|
+
# Method will return list of users in the conversation
|
130
|
+
# @return [Array<ActiveRecord::Base>] users
|
131
|
+
sig { returns(T::Array[ActiveRecord::Base]) }
|
75
132
|
def people
|
76
133
|
conversation.map(&:from).uniq!
|
77
134
|
end
|
@@ -1,18 +1,36 @@
|
|
1
|
+
# typed: strict
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module ActsAsMessageable
|
4
5
|
module Model
|
6
|
+
extend T::Sig
|
7
|
+
|
8
|
+
# @return [Object]
|
9
|
+
# @param [Object] base
|
10
|
+
sig { params(base: Module).returns(T.untyped) }
|
5
11
|
def self.included(base)
|
6
12
|
base.extend ClassMethods
|
7
13
|
end
|
8
14
|
|
9
15
|
module ClassMethods
|
16
|
+
extend T::Helpers
|
17
|
+
extend T::Sig
|
18
|
+
|
19
|
+
requires_ancestor { T.class_of(ActiveRecord::Base) }
|
20
|
+
|
10
21
|
# Method make ActiveRecord::Base object messageable
|
11
|
-
# @
|
12
|
-
# @
|
13
|
-
# @
|
14
|
-
# @
|
15
|
-
# @
|
22
|
+
# @option options [Symbol] :table_name table name for messages
|
23
|
+
# @option options [String] :class_name message class name
|
24
|
+
# @option options [Array, Symbol] :required required fields in message
|
25
|
+
# @option options [Symbol] :dependent dependent option from ActiveRecord has_many method
|
26
|
+
# @option options [Symbol] :search_scope name of a scope for a full text search
|
27
|
+
# @param [Hash] options
|
28
|
+
# @return [Object]
|
29
|
+
sig do
|
30
|
+
params(options: T::Hash[Symbol,
|
31
|
+
T.any(String, Symbol, T::Array[Symbol],
|
32
|
+
T::Array[String])]).returns(ActsAsMessageable::Model::ClassMethods)
|
33
|
+
end
|
16
34
|
def acts_as_messageable(options = {})
|
17
35
|
default_options = {
|
18
36
|
table_name: 'messages',
|
@@ -50,13 +68,22 @@ module ActsAsMessageable
|
|
50
68
|
|
51
69
|
# Method recognize real object class
|
52
70
|
# @return [ActiveRecord::Base] class or relation object
|
71
|
+
sig { returns(T.class_of(ActiveRecord::Base)) }
|
53
72
|
def resolve_active_record_ancestor
|
54
73
|
reflect_on_association(:received_messages_relation).active_record
|
55
74
|
end
|
56
75
|
end
|
57
76
|
|
58
77
|
module InstanceMethods
|
78
|
+
extend T::Helpers
|
79
|
+
extend T::Sig
|
80
|
+
|
81
|
+
requires_ancestor { Kernel }
|
82
|
+
requires_ancestor { CustomSearchUser }
|
83
|
+
|
59
84
|
# @return [ActiveRecord::Relation] all messages connected with user
|
85
|
+
# @param [Boolean] trash Show deleted messages
|
86
|
+
sig { params(trash: T::Boolean).returns(ActiveRecord::Relation) }
|
60
87
|
def messages(trash = false)
|
61
88
|
result = self.class.messages_class_name.connected_with(self, trash)
|
62
89
|
result.relation_context = self
|
@@ -65,34 +92,41 @@ module ActsAsMessageable
|
|
65
92
|
end
|
66
93
|
|
67
94
|
# @return [ActiveRecord::Relation] returns all messages from inbox
|
95
|
+
sig { returns(ActiveRecord::Relation) }
|
68
96
|
def received_messages
|
69
97
|
result = ActsAsMessageable.rails_api.new(received_messages_relation)
|
70
|
-
result = result.scoped.where(recipient_delete: false)
|
98
|
+
result = T.unsafe(result).scoped.where(recipient_delete: false)
|
71
99
|
result.relation_context = self
|
72
100
|
|
73
101
|
result
|
74
102
|
end
|
75
103
|
|
76
104
|
# @return [ActiveRecord::Relation] returns all messages from outbox
|
105
|
+
sig { returns(ActiveRecord::Relation) }
|
77
106
|
def sent_messages
|
78
107
|
result = ActsAsMessageable.rails_api.new(sent_messages_relation)
|
79
|
-
result = result.scoped.where(sender_delete: false)
|
108
|
+
result = T.unsafe(result).scoped.where(sender_delete: false)
|
80
109
|
result.relation_context = self
|
81
110
|
|
82
111
|
result
|
83
112
|
end
|
84
113
|
|
85
114
|
# @return [ActiveRecord::Relation] returns all messages from trash
|
115
|
+
sig { returns(ActiveRecord::Relation) }
|
86
116
|
def deleted_messages
|
87
|
-
messages
|
117
|
+
messages(true)
|
88
118
|
end
|
89
119
|
|
90
|
-
# Method
|
120
|
+
# Method sends message to another user
|
91
121
|
# @param [ActiveRecord::Base] to
|
92
|
-
# @param [
|
93
|
-
# @
|
94
|
-
#
|
122
|
+
# @param [Hash] args
|
123
|
+
# @option args [String] topic Topic of the message
|
124
|
+
# @option args [String] body Body of the message
|
95
125
|
# @return [ActsAsMessageable::Message] the message object
|
126
|
+
sig do
|
127
|
+
params(to: ActiveRecord::Base,
|
128
|
+
args: T.any(String, T::Hash[T.any(String, Symbol), String])).returns(ActsAsMessageable::Message)
|
129
|
+
end
|
96
130
|
def send_message(to, *args)
|
97
131
|
message_attributes = {}
|
98
132
|
|
@@ -108,7 +142,7 @@ module ActsAsMessageable
|
|
108
142
|
message = self.class.messages_class_name.new message_attributes
|
109
143
|
message.received_messageable = to
|
110
144
|
message.sent_messageable = self
|
111
|
-
message.save
|
145
|
+
message.save!
|
112
146
|
|
113
147
|
message
|
114
148
|
end
|
@@ -116,26 +150,37 @@ module ActsAsMessageable
|
|
116
150
|
# Method send message to another user
|
117
151
|
# and raise exception in case of validation errors
|
118
152
|
# @param [ActiveRecord::Base] to
|
119
|
-
# @param [
|
120
|
-
# @
|
153
|
+
# @param [Hash] args
|
154
|
+
# @option [String] topic Topic of the message
|
155
|
+
# @option [String] body Body of the message
|
121
156
|
#
|
122
157
|
# @return [ActsAsMessageable::Message] the message object
|
158
|
+
sig do
|
159
|
+
params(to: ActiveRecord::Base,
|
160
|
+
args: T.any(String, T::Hash[T.any(String, Symbol), String])).returns(ActsAsMessageable::Message)
|
161
|
+
end
|
123
162
|
def send_message!(to, *args)
|
124
|
-
send_message(to, *args).tap(&:save!)
|
163
|
+
T.unsafe(self).send_message(to, *T.unsafe(args)).tap(&:save!)
|
125
164
|
end
|
126
165
|
|
127
166
|
# Reply to given message
|
128
167
|
# @param [ActsAsMessageable::Message] message
|
129
|
-
# @param [
|
130
|
-
# @
|
168
|
+
# @param [Hash] args
|
169
|
+
# @option args [String] topic Topic of the message
|
170
|
+
# @option args [String] body Body of the message
|
131
171
|
#
|
132
172
|
# @return [ActsAsMessageable::Message] a message that is a response to a given message
|
173
|
+
sig do
|
174
|
+
params(message: ActsAsMessageable::Message,
|
175
|
+
args: T.any(String,
|
176
|
+
T::Hash[T.any(String, Symbol), String])).returns(T.nilable(ActsAsMessageable::Message))
|
177
|
+
end
|
133
178
|
def reply_to(message, *args)
|
134
|
-
current_user = self
|
179
|
+
current_user = T.cast(self, ActiveRecord::Base)
|
135
180
|
|
136
181
|
return unless message.participant?(current_user)
|
137
182
|
|
138
|
-
reply_message = send_message(message.real_receiver(current_user), *args)
|
183
|
+
reply_message = T.unsafe(self).send_message(message.real_receiver(current_user), *args)
|
139
184
|
reply_message.parent = message
|
140
185
|
reply_message.save
|
141
186
|
|
@@ -143,6 +188,9 @@ module ActsAsMessageable
|
|
143
188
|
end
|
144
189
|
|
145
190
|
# Mark message as deleted
|
191
|
+
# @param [ActsAsMessageable::Message] message to delete
|
192
|
+
# @return [ActsAsMessageable::Message] deleted message
|
193
|
+
sig { params(message: ActsAsMessageable::Message).returns(T::Boolean) }
|
146
194
|
def delete_message(message)
|
147
195
|
current_user = self
|
148
196
|
|
@@ -159,6 +207,9 @@ module ActsAsMessageable
|
|
159
207
|
end
|
160
208
|
|
161
209
|
# Mark message as restored
|
210
|
+
# @param [ActsAsMessageable::Message] message to restore
|
211
|
+
# @return [ActsAsMessageable::Message] restored message
|
212
|
+
sig { params(message: ActsAsMessageable::Message).returns(T::Boolean) }
|
162
213
|
def restore_message(message)
|
163
214
|
current_user = self
|
164
215
|
|
@@ -1,19 +1,44 @@
|
|
1
|
+
# typed: strict
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module ActsAsMessageable
|
4
5
|
class Rails3
|
6
|
+
extend T::Sig
|
7
|
+
|
8
|
+
delegate :attr_accessible, :update_attributes!, to: :@subject
|
9
|
+
|
10
|
+
# @return [ActsAsMessageable::Rails3] api wrapper object
|
11
|
+
# @param [ActiveRecord::Base] subject
|
12
|
+
sig { params(subject: T.untyped).void }
|
5
13
|
def initialize(subject)
|
6
14
|
@subject = subject
|
7
15
|
end
|
8
16
|
|
17
|
+
# @return [Object]
|
18
|
+
# @param [String, Symbol] order_by
|
19
|
+
# @see ActiveRecord::Base#default_scope
|
20
|
+
sig { params(order_by: T.any(String, Symbol)).void }
|
9
21
|
def default_scope(order_by)
|
10
|
-
|
22
|
+
# T.unsafe is needed here because the default_scope method is defined in
|
23
|
+
# different ways in different versions of Rails. In Rails 3, it is a
|
24
|
+
# class method, but in Rails 4, it is an instance method. We can't
|
25
|
+
# statically determine which version of Rails is being used, so we have
|
26
|
+
# to use T.unsafe here.
|
27
|
+
@subject.send(:default_scope, T.unsafe(@subject).order(order_by))
|
11
28
|
end
|
12
29
|
|
30
|
+
# @return [Object]
|
31
|
+
# @param [Symbol] name
|
32
|
+
# @param [Array] args
|
33
|
+
sig { params(name: Symbol, args: T.untyped).returns(T.untyped) }
|
13
34
|
def method_missing(name, *args)
|
14
|
-
@subject.send(name, *args) || super
|
35
|
+
T.unsafe(@subject).send(name, *args) || super
|
15
36
|
end
|
16
37
|
|
38
|
+
# @return [Boolean]
|
39
|
+
# @param [Object] method_name
|
40
|
+
# @param [FalseClass] include_private
|
41
|
+
sig { params(method_name: Symbol, include_private: T::Boolean).returns(T::Boolean) }
|
17
42
|
def respond_to_missing?(method_name, include_private = false)
|
18
43
|
method_name.to_s == 'default_scope' || super
|
19
44
|
end
|
@@ -1,25 +1,52 @@
|
|
1
|
+
# typed: strict
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module ActsAsMessageable
|
4
5
|
class Rails4
|
6
|
+
extend T::Sig
|
7
|
+
|
8
|
+
delegate :update_attributes!, to: :@subject
|
9
|
+
|
10
|
+
# @return [ActsAsMessageable::Rails4] api wrapper object
|
11
|
+
# @param [ActiveRecord::Base] subject
|
12
|
+
sig { params(subject: T.untyped).void }
|
5
13
|
def initialize(subject)
|
6
14
|
@subject = subject
|
7
15
|
end
|
8
16
|
|
9
|
-
|
17
|
+
# Empty method for Rails 4.x
|
18
|
+
# @return [NilClass]
|
19
|
+
# @param [Array] _args
|
20
|
+
sig { params(_args: T.any(String, Symbol)).void }
|
21
|
+
def attr_accessible(*_args); end
|
10
22
|
|
23
|
+
# Default scope for Rails 4.x with block support
|
24
|
+
# @return [Object]
|
25
|
+
# @param [String, Symbol] order_by
|
26
|
+
sig { params(order_by: T.any(String, Symbol)).returns(Object) }
|
11
27
|
def default_scope(order_by)
|
12
|
-
@subject.send(:default_scope) { order(order_by) }
|
28
|
+
@subject.send(:default_scope) { T.unsafe(self).order(order_by) }
|
13
29
|
end
|
14
30
|
|
31
|
+
# Rename of the method
|
32
|
+
# @return [Object]
|
33
|
+
sig { returns(Object) }
|
15
34
|
def scoped
|
16
|
-
@subject.scope
|
35
|
+
T.unsafe(@subject).scope
|
17
36
|
end
|
18
37
|
|
38
|
+
# @return [Object]
|
39
|
+
# @param [Symbol] name
|
40
|
+
# @param [Array] args
|
41
|
+
sig { params(name: Symbol, args: T.untyped).returns(T.untyped) }
|
19
42
|
def method_missing(name, *args)
|
20
|
-
@subject.send(name, *args) || super
|
43
|
+
T.unsafe(@subject).send(name, *args) || super
|
21
44
|
end
|
22
45
|
|
46
|
+
# @return [Boolean]
|
47
|
+
# @param [String] method_name
|
48
|
+
# @param [Boolean] include_private
|
49
|
+
sig { params(method_name: Symbol, include_private: T::Boolean).returns(T::Boolean) }
|
23
50
|
def respond_to_missing?(method_name, include_private = false)
|
24
51
|
%w[default_scope scoped attr_accessible].include?(method_name) || super
|
25
52
|
end
|
@@ -1,29 +1,61 @@
|
|
1
|
+
# typed: strict
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module ActsAsMessageable
|
4
5
|
class Rails6
|
6
|
+
extend T::Sig
|
7
|
+
|
8
|
+
# @return [ActsAsMessageable::Rails4] api wrapper object
|
9
|
+
# @param [ActiveRecord::Base] subject
|
10
|
+
sig { params(subject: T.untyped).void }
|
5
11
|
def initialize(subject)
|
6
12
|
@subject = subject
|
7
13
|
end
|
8
14
|
|
9
|
-
|
15
|
+
# Empty method from Rails 3.x
|
16
|
+
# @return [NilClass]
|
17
|
+
# @param [Array] _args
|
18
|
+
sig { params(_args: T.any(String, Symbol)).void }
|
19
|
+
def attr_accessible(*_args); end
|
10
20
|
|
21
|
+
# Default scope for Rails 6.x with block support
|
22
|
+
# @return [Object]
|
23
|
+
# @param [String, Symbol] order_by
|
24
|
+
sig { params(order_by: T.any(String, Symbol)).returns(Object) }
|
11
25
|
def default_scope(order_by)
|
12
|
-
@subject.send(:default_scope)
|
26
|
+
@subject.send(:default_scope) do
|
27
|
+
T.bind(self, ActiveRecord::Relation)
|
28
|
+
order(order_by)
|
29
|
+
end
|
13
30
|
end
|
14
31
|
|
32
|
+
# Rename of the method
|
33
|
+
# @return [Object]
|
34
|
+
sig { returns(Object) }
|
15
35
|
def scoped
|
16
36
|
@subject.scope
|
17
37
|
end
|
18
38
|
|
39
|
+
# Use new method #update! in Rails 6.x
|
40
|
+
# @return [Object]
|
41
|
+
# @param [Array] args
|
42
|
+
sig { params(args: T::Hash[String, String]).returns(T::Boolean) }
|
19
43
|
def update_attributes!(*args)
|
20
44
|
@subject.update!(*args)
|
21
45
|
end
|
22
46
|
|
47
|
+
# @return [Object]
|
48
|
+
# @param [Symbol] name
|
49
|
+
# @param [Array] args
|
50
|
+
sig { params(name: Symbol, args: T.untyped).returns(T.untyped) }
|
23
51
|
def method_missing(name, *args)
|
24
52
|
@subject.send(name, *args) || super
|
25
53
|
end
|
26
54
|
|
55
|
+
# @return [Boolean]
|
56
|
+
# @param [String] method_name
|
57
|
+
# @param [Boolean] include_private
|
58
|
+
sig { params(method_name: Symbol, include_private: T::Boolean).returns(T::Boolean) }
|
27
59
|
def respond_to_missing?(method_name, include_private = false)
|
28
60
|
%w[attr_accessible default_scope scoped update_attributes!].include?(method_name) || super
|
29
61
|
end
|
@@ -1,19 +1,35 @@
|
|
1
|
+
# typed: true
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module ActsAsMessageable
|
4
5
|
module Relation
|
6
|
+
extend T::Sig
|
7
|
+
extend T::Helpers
|
8
|
+
|
9
|
+
sig { returns(ActsAsMessageable::Model::InstanceMethods) }
|
5
10
|
attr_accessor :relation_context
|
6
11
|
|
12
|
+
requires_ancestor { ActiveRecord::Relation }
|
13
|
+
|
14
|
+
# @yield [ActsAsMessageable::Message] message
|
15
|
+
# @param [ActiveRecord::Base] context of relation (most of the time current_user object)
|
16
|
+
# @return [ActiveRecord::Relation]
|
17
|
+
sig { params(context: ActsAsMessageable::Model::InstanceMethods).returns(T.untyped) }
|
7
18
|
def process(context = relation_context)
|
8
|
-
|
19
|
+
relation = T.cast(self, ActiveRecord::Relation)
|
20
|
+
|
21
|
+
relation.each do |message|
|
9
22
|
yield(message) if block_given?
|
10
23
|
context.delete_message(message) if message.removed
|
11
24
|
context.restore_message(message) if message.restored
|
12
25
|
end
|
13
26
|
end
|
14
27
|
|
28
|
+
# @return [Array<ActsAsMessageable::Message>]
|
29
|
+
# @return [ActiveRecord::Relation]
|
15
30
|
def conversations
|
16
|
-
|
31
|
+
relation = T.cast(self, ActiveRecord::Relation)
|
32
|
+
relation.map { |message| message.root.subtree.order('id desc').first }.uniq
|
17
33
|
end
|
18
34
|
end
|
19
35
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# typed: strict
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
require 'active_support/concern'
|
@@ -7,6 +8,16 @@ module ActsAsMessageable
|
|
7
8
|
extend ActiveSupport::Concern
|
8
9
|
|
9
10
|
module ClassMethods
|
11
|
+
# @return [Object]
|
12
|
+
# @param [String, Symbol] search_scope
|
13
|
+
extend T::Helpers
|
14
|
+
extend T::Sig
|
15
|
+
|
16
|
+
requires_ancestor { T.class_of(ActiveRecord::Base) }
|
17
|
+
|
18
|
+
include Kernel
|
19
|
+
|
20
|
+
sig { params(search_scope: T.any(String, Symbol)).void }
|
10
21
|
def initialize_scopes(search_scope)
|
11
22
|
scope :are_from, lambda { |*args|
|
12
23
|
where(sent_messageable_id: args.first, sent_messageable_type: args.first.class.name)
|
data/lib/acts_as_messageable.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
|
+
# typed: strong
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
4
|
+
require 'sorbet-runtime'
|
5
|
+
require 'sorbet-rails'
|
6
|
+
require 'ancestry'
|
7
|
+
|
3
8
|
module ActsAsMessageable
|
4
9
|
autoload :Model, 'acts_as_messageable/model'
|
5
10
|
autoload :Scopes, 'acts_as_messageable/scopes'
|
@@ -9,6 +14,19 @@ module ActsAsMessageable
|
|
9
14
|
autoload :Rails4, 'acts_as_messageable/rails4'
|
10
15
|
autoload :Rails6, 'acts_as_messageable/rails6'
|
11
16
|
|
17
|
+
extend T::Sig
|
18
|
+
|
19
|
+
# @return [Class<ActsAsMessageable::Rails4>, Class<ActsAsMessageable::Rails6>, Class<ActsAsMessageable::Rails3>]
|
20
|
+
# API wrapper
|
21
|
+
sig do
|
22
|
+
returns(
|
23
|
+
T.any(
|
24
|
+
T.class_of(ActsAsMessageable::Rails4),
|
25
|
+
T.class_of(ActsAsMessageable::Rails6),
|
26
|
+
T.class_of(ActsAsMessageable::Rails3)
|
27
|
+
)
|
28
|
+
)
|
29
|
+
end
|
12
30
|
def self.rails_api
|
13
31
|
if Rails::VERSION::MAJOR >= 6
|
14
32
|
Rails6
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# typed: strict
|
1
2
|
# frozen_string_literal: true
|
2
3
|
|
3
4
|
require 'rails/generators/migration'
|
@@ -5,6 +6,7 @@ require 'rails/generators/active_record'
|
|
5
6
|
|
6
7
|
module ActsAsMessageable
|
7
8
|
class MigrationGenerator < Rails::Generators::Base
|
9
|
+
extend T::Sig
|
8
10
|
include Rails::Generators::Migration
|
9
11
|
|
10
12
|
namespace 'acts_as_messageable:migration'
|
@@ -13,10 +15,12 @@ module ActsAsMessageable
|
|
13
15
|
argument :table_name, type: :string, default: 'messages'
|
14
16
|
class_option :uuid, type: :boolean, default: false
|
15
17
|
|
18
|
+
sig { params(dirname: String).returns(String) }
|
16
19
|
def self.next_migration_number(dirname)
|
17
20
|
ActiveRecord::Generators::Base.next_migration_number(dirname)
|
18
21
|
end
|
19
22
|
|
23
|
+
sig { returns(String) }
|
20
24
|
def create_migration_file
|
21
25
|
begin
|
22
26
|
migration_template 'migration.rb', 'db/migrate/create_messages_table.rb'
|