message_train 0.5.2 → 0.5.3
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 +4 -4
- data/.rubocop.yml +20 -0
- data/.ruby-gemset +1 -1
- data/.scss-lint.yml +7 -0
- data/.simplecov +1 -1
- data/.travis.yml +1 -1
- data/Gemfile +6 -2
- data/LICENSE.txt +2 -2
- data/README.rdoc +5 -5
- data/Rakefile +22 -21
- data/VERSION +1 -1
- data/app/assets/javascripts/message_train.js +1 -1
- data/app/assets/stylesheets/message_train.scss +29 -19
- data/app/controllers/concerns/message_train_support.rb +41 -37
- data/app/controllers/message_train/application_controller.rb +1 -0
- data/app/controllers/message_train/boxes_controller.rb +5 -4
- data/app/controllers/message_train/conversations_controller.rb +8 -7
- data/app/controllers/message_train/messages_controller.rb +25 -22
- data/app/controllers/message_train/participants_controller.rb +27 -23
- data/app/controllers/message_train/unsubscribes_controller.rb +37 -23
- data/app/helpers/message_train/application_helper.rb +6 -8
- data/app/helpers/message_train/attachments_helper.rb +12 -4
- data/app/helpers/message_train/boxes_helper.rb +38 -11
- data/app/helpers/message_train/collectives_helper.rb +38 -12
- data/app/helpers/message_train/conversations_helper.rb +43 -35
- data/app/helpers/message_train/messages_helper.rb +32 -25
- data/app/mailers/message_train/application_mailer.rb +7 -4
- data/app/mailers/message_train/previews/receipt_mailer_preview.rb +2 -3
- data/app/mailers/message_train/receipt_mailer.rb +14 -4
- data/app/models/message_train/attachment.rb +41 -25
- data/app/models/message_train/box.rb +159 -122
- data/app/models/message_train/conversation.rb +84 -41
- data/app/models/message_train/ignore.rb +8 -2
- data/app/models/message_train/message.rb +72 -42
- data/app/models/message_train/receipt.rb +30 -17
- data/app/models/message_train/unsubscribe.rb +1 -0
- data/app/views/application/404.html.haml +1 -1
- data/app/views/layouts/mailer.html.haml +2 -2
- data/app/views/layouts/mailer.text.haml +1 -1
- data/app/views/message_train/application/_attachment_fields.html.haml +1 -1
- data/app/views/message_train/application/_widget.html.haml +1 -1
- data/app/views/message_train/application/results.json.jbuilder +1 -1
- data/app/views/message_train/boxes/_dropdown_list.html.haml +1 -1
- data/app/views/message_train/boxes/_list_item.html.haml +1 -1
- data/app/views/message_train/boxes/show.html.haml +1 -1
- data/app/views/message_train/collectives/_dropdown_list.html.haml +1 -1
- data/app/views/message_train/collectives/_list_item.html.haml +1 -1
- data/app/views/message_train/conversations/_deleted_toggle.html.haml +1 -1
- data/app/views/message_train/conversations/_ignored_toggle.html.haml +2 -2
- data/app/views/message_train/conversations/_read_toggle.html.haml +1 -1
- data/app/views/message_train/conversations/_toggle.html.haml +1 -1
- data/app/views/message_train/conversations/_trashed_toggle.html.haml +1 -1
- data/app/views/message_train/conversations/show.html.haml +2 -2
- data/app/views/message_train/conversations/show.json.jbuilder +5 -1
- data/app/views/message_train/messages/_form.html.haml +1 -1
- data/app/views/message_train/messages/_message.html.haml +6 -6
- data/app/views/message_train/messages/_toggle.html.haml +1 -1
- data/app/views/message_train/messages/show.json.jbuilder +5 -1
- data/app/views/message_train/participants/_participant.json.jbuilder +1 -1
- data/app/views/message_train/participants/index.json.jbuilder +1 -1
- data/app/views/message_train/participants/show.json.jbuilder +1 -1
- data/app/views/message_train/receipt_mailer/notification_email.html.haml +1 -1
- data/app/views/message_train/receipt_mailer/notification_email.text.haml +1 -1
- data/app/views/message_train/unsubscribes/index.html.haml +1 -1
- data/config/environment.rb +1 -1
- data/config/initializers/date_time.rb +2 -2
- data/config/locales/en.yml +1 -1
- data/config/routes.rb +37 -15
- data/lib/generators/message_train/install/install_generator.rb +26 -11
- data/lib/generators/message_train/install/templates/initializer.rb +0 -2
- data/lib/generators/message_train/utils.rb +11 -4
- data/lib/message_train/configuration.rb +4 -5
- data/lib/message_train/engine.rb +1 -7
- data/lib/message_train/localization.rb +14 -18
- data/lib/message_train/mixin.rb +211 -150
- data/lib/message_train/version.rb +1 -0
- data/lib/message_train.rb +7 -6
- data/message_train.gemspec +24 -11
- data/spec/controllers/message_train/boxes_controller_spec.rb +63 -15
- data/spec/controllers/message_train/concerns_spec.rb +17 -13
- data/spec/controllers/message_train/conversations_controller_spec.rb +44 -12
- data/spec/controllers/message_train/messages_controller_spec.rb +87 -40
- data/spec/controllers/message_train/participants_controller_spec.rb +10 -4
- data/spec/controllers/message_train/unsubscribes_controller_spec.rb +43 -13
- data/spec/dummy/Rakefile +2 -1
- data/spec/dummy/app/assets/stylesheets/application.css.scss +7 -23
- data/spec/dummy/app/controllers/application_controller.rb +7 -7
- data/spec/dummy/app/models/group.rb +1 -1
- data/spec/dummy/app/models/role.rb +11 -7
- data/spec/dummy/app/models/user.rb +6 -5
- data/spec/dummy/app/views/layouts/_top_navigation.html.haml +1 -1
- data/spec/dummy/app/views/layouts/application.html.haml +1 -1
- data/spec/dummy/app/views/pages/index.html.haml +1 -1
- data/spec/dummy/config/application.rb +23 -17
- data/spec/dummy/config/environments/development.rb +5 -4
- data/spec/dummy/config/environments/production.rb +10 -6
- data/spec/dummy/config/environments/test.rb +4 -3
- data/spec/dummy/config/initializers/assets.rb +2 -1
- data/spec/dummy/config/initializers/backtrace_silencers.rb +4 -2
- data/spec/dummy/config/initializers/bootstrap_leather.rb +1 -1
- data/spec/dummy/config/initializers/devise.rb +63 -44
- data/spec/dummy/config/initializers/high_voltage.rb +1 -1
- data/spec/dummy/config/initializers/message_train.rb +5 -2
- data/spec/dummy/config/initializers/mime_types.rb +1 -1
- data/spec/dummy/config/initializers/paperclip.rb +5 -2
- data/spec/dummy/config/initializers/rolify.rb +3 -2
- data/spec/dummy/config/initializers/wrap_parameters.rb +2 -1
- data/spec/dummy/config/routes.rb +1 -3
- data/spec/dummy/config/settings.yml +1 -1
- data/spec/dummy/db/schema.rb +98 -98
- data/spec/dummy/db/seeds/conversations.seeds.rb +160 -160
- data/spec/dummy/db/seeds/development/conversations.seeds.rb +6 -2
- data/spec/dummy/db/seeds/groups.seeds.rb +11 -12
- data/spec/dummy/db/seeds/test/attachments.seeds.rb +13 -3
- data/spec/dummy/db/seeds/test/conversations.seeds.rb +6 -2
- data/spec/dummy/db/seeds/unsubscribes.seeds.rb +1 -1
- data/spec/dummy/db/seeds/users.seeds.rb +47 -42
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/factories/attachment.rb +6 -2
- data/spec/factories/group.rb +1 -4
- data/spec/factories/message.rb +29 -23
- data/spec/factories/user.rb +6 -4
- data/spec/features/boxes_spec.rb +76 -24
- data/spec/features/conversations_spec.rb +19 -7
- data/spec/features/messages_spec.rb +24 -5
- data/spec/features/unsubscribes_spec.rb +36 -9
- data/spec/helpers/message_train/application_helper_spec.rb +1 -1
- data/spec/helpers/message_train/attachment_helper_spec.rb +27 -12
- data/spec/helpers/message_train/boxes_helper_spec.rb +1 -1
- data/spec/helpers/message_train/collectives_helper_spec.rb +25 -9
- data/spec/helpers/message_train/conversations_helper_spec.rb +239 -120
- data/spec/helpers/message_train/messages_helper_spec.rb +137 -81
- data/spec/message_train_spec.rb +3 -2
- data/spec/models/group_spec.rb +12 -9
- data/spec/models/message_train/attachment_spec.rb +33 -30
- data/spec/models/message_train/box_spec.rb +243 -60
- data/spec/models/message_train/conversation_spec.rb +16 -12
- data/spec/models/message_train/ignore_spec.rb +3 -1
- data/spec/models/message_train/message_spec.rb +22 -7
- data/spec/models/message_train/receipt_spec.rb +14 -4
- data/spec/models/role_spec.rb +10 -6
- data/spec/models/user_spec.rb +74 -22
- data/spec/rails_helper.rb +9 -4
- data/spec/spec_helper.rb +6 -54
- data/spec/support/controller_behaviors.rb +8 -5
- data/spec/support/controller_macros.rb +2 -2
- data/spec/support/factory_girl.rb +1 -1
- data/spec/support/feature_behaviors.rb +42 -13
- data/spec/support/loaded_site/attachments.rb +8 -0
- data/spec/support/loaded_site/conversations.rb +73 -0
- data/spec/support/{groups.rb → loaded_site/groups.rb} +1 -1
- data/spec/support/{loaded_site.rb → loaded_site/loaded_site.rb} +1 -1
- data/spec/support/{messages.rb → loaded_site/messages.rb} +13 -5
- data/spec/support/{roles.rb → loaded_site/roles.rb} +1 -1
- data/spec/support/{users.rb → loaded_site/users.rb} +1 -1
- data/spec/support/shared_connection.rb +8 -6
- data/spec/support/utilities.rb +10 -10
- metadata +67 -10
- data/spec/dummy/config/unused/temporary_i18n_debugging.rb +0 -12
- data/spec/support/attachments.rb +0 -4
- data/spec/support/conversations.rb +0 -23
@@ -1,35 +1,51 @@
|
|
1
1
|
module MessageTrain
|
2
|
+
# Attachment model
|
2
3
|
class Attachment < ActiveRecord::Base
|
3
4
|
belongs_to :message, foreign_key: :message_train_message_id
|
4
|
-
has_attached_file
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
5
|
+
has_attached_file(
|
6
|
+
:attachment,
|
7
|
+
styles: lambda do |attachment|
|
8
|
+
if attachment.instance.image?
|
9
|
+
{
|
10
|
+
thumb: '235x',
|
11
|
+
large: '800x'
|
12
|
+
}
|
13
|
+
else
|
14
|
+
{}
|
15
|
+
end
|
16
|
+
end,
|
17
|
+
path: ':rails_root/public/system/:rails_env/:class/:attachment/'\
|
18
|
+
':id_partition/:style_prefix:filename',
|
19
|
+
url: '/system/:rails_env/:class/:attachment/'\
|
20
|
+
':id_partition/:style_prefix:filename',
|
21
|
+
convert_options: {
|
22
|
+
large: '-quality 75 -interlace Plane -strip',
|
23
|
+
thumb: '-quality 75 -strip'
|
24
|
+
}
|
25
|
+
)
|
16
26
|
validates_attachment_presence :attachment
|
17
|
-
validates_attachment_content_type
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
+
validates_attachment_content_type(
|
28
|
+
:attachment,
|
29
|
+
content_type: [
|
30
|
+
'application/pdf',
|
31
|
+
'application/vnd.ms-excel',
|
32
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
33
|
+
'application/msword',
|
34
|
+
'application/'\
|
35
|
+
'vnd.openxmlformats-officedocument.wordprocessingml.document',
|
36
|
+
'application/rtf',
|
37
|
+
'text/plain',
|
38
|
+
%r{^(image|(x-)?application)/(bmp|gif|jpeg|jpg|pjpeg|png|x-png)$}
|
39
|
+
]
|
40
|
+
)
|
27
41
|
def image?
|
28
|
-
|
42
|
+
# rubocop:disable Style/LineLength
|
43
|
+
!(attachment_content_type =~ %r{^(image|(x-)?application)/(bmp|gif|jpeg|jpg|pjpeg|png|x-png)$}).nil?
|
44
|
+
# rubocop:enable Style/LineLength
|
29
45
|
end
|
30
46
|
|
31
47
|
Paperclip.interpolates :style_prefix do |attachment, style|
|
32
|
-
attachment.instance.image? ? "#{style
|
48
|
+
attachment.instance.image? ? "#{style}/" : ''
|
33
49
|
end
|
34
50
|
end
|
35
|
-
end
|
51
|
+
end
|
@@ -1,8 +1,9 @@
|
|
1
1
|
module MessageTrain
|
2
|
+
# Box model
|
2
3
|
class Box
|
3
4
|
include ActiveModel::Model
|
4
5
|
attr_accessor :parent, :division, :participant, :errors, :results
|
5
|
-
|
6
|
+
alias id division
|
6
7
|
|
7
8
|
def initialize(parent, division, participant = nil)
|
8
9
|
@parent = parent
|
@@ -18,12 +19,6 @@ module MessageTrain
|
|
18
19
|
|
19
20
|
def unread_count
|
20
21
|
found = conversations(unread: true)
|
21
|
-
# # This fixed a bug that only Travis seemed to pick up, but it's code smell.
|
22
|
-
# if found.nil?
|
23
|
-
# 0
|
24
|
-
# else
|
25
|
-
# found.count
|
26
|
-
# end
|
27
22
|
found.count
|
28
23
|
end
|
29
24
|
|
@@ -37,16 +32,16 @@ module MessageTrain
|
|
37
32
|
found = found.with_trashed_for(participant)
|
38
33
|
else
|
39
34
|
found = found.with_untrashed_for(participant)
|
40
|
-
if division == :drafts
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
if division == :ignored
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
35
|
+
found = if division == :drafts
|
36
|
+
found.with_drafts_by(participant)
|
37
|
+
else
|
38
|
+
found.with_ready_for(participant)
|
39
|
+
end
|
40
|
+
found = if division == :ignored
|
41
|
+
found.ignored(participant)
|
42
|
+
else
|
43
|
+
found.unignored(participant)
|
44
|
+
end
|
50
45
|
end
|
51
46
|
found
|
52
47
|
end
|
@@ -67,12 +62,15 @@ module MessageTrain
|
|
67
62
|
previous_message = conversation.messages.last
|
68
63
|
message = conversation.messages.build(args)
|
69
64
|
message.subject = "Re: #{conversation.subject}"
|
70
|
-
message.body = "<blockquote>#{previous_message.body}</blockquote
|
65
|
+
message.body = "<blockquote>#{previous_message.body}</blockquote>"\
|
66
|
+
'<p> </p>'
|
71
67
|
recipient_arrays = {}
|
72
68
|
conversation.default_recipients_for(parent).each do |recipient|
|
73
69
|
table_name = recipient.class.table_name
|
74
70
|
recipient_arrays[table_name] ||= []
|
75
|
-
recipient_arrays[table_name] << recipient.send(
|
71
|
+
recipient_arrays[table_name] << recipient.send(
|
72
|
+
MessageTrain.configuration.slug_columns[table_name.to_sym]
|
73
|
+
)
|
76
74
|
end
|
77
75
|
recipient_arrays.each do |key, array|
|
78
76
|
message.recipients_to_save[key] = array.join(', ')
|
@@ -85,7 +83,12 @@ module MessageTrain
|
|
85
83
|
message_to_send = MessageTrain::Message.new attributes
|
86
84
|
message_to_send.sender = participant
|
87
85
|
unless parent.valid_senders.include? participant
|
88
|
-
errors.add(
|
86
|
+
errors.add(
|
87
|
+
message_to_send,
|
88
|
+
:invalid_sender_for_thing.l(
|
89
|
+
thing: "#{parent.class.name} #{parent.id}"
|
90
|
+
)
|
91
|
+
)
|
89
92
|
return false
|
90
93
|
end
|
91
94
|
if message_to_send.save
|
@@ -95,78 +98,91 @@ module MessageTrain
|
|
95
98
|
results.add(message_to_send, :message_sent.l)
|
96
99
|
end
|
97
100
|
else
|
98
|
-
errors.add(
|
101
|
+
errors.add(
|
102
|
+
message_to_send,
|
103
|
+
message_to_send.errors.full_messages.to_sentence
|
104
|
+
)
|
99
105
|
end
|
100
106
|
message_to_send
|
101
107
|
end
|
102
108
|
|
103
|
-
def update_message(
|
109
|
+
def update_message(message, attributes)
|
104
110
|
attributes.delete(:sender)
|
105
|
-
if
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
+
if message.sender == participant && parent.valid_senders.include?(
|
112
|
+
participant
|
113
|
+
)
|
114
|
+
message.update(attributes)
|
115
|
+
message.reload
|
116
|
+
if message.errors.empty?
|
117
|
+
if message.draft
|
118
|
+
results.add(message, :message_saved_as_draft.l)
|
111
119
|
else
|
112
|
-
results.add(
|
120
|
+
results.add(message, :message_sent.l)
|
113
121
|
end
|
114
122
|
else
|
115
|
-
errors.add(
|
123
|
+
errors.add(
|
124
|
+
message,
|
125
|
+
message.errors.full_messages.to_sentence
|
126
|
+
)
|
116
127
|
end
|
117
|
-
|
128
|
+
message
|
118
129
|
else
|
119
|
-
errors.add(
|
130
|
+
errors.add(
|
131
|
+
message,
|
132
|
+
:access_to_message_id_denied.l(id: message.id)
|
133
|
+
)
|
120
134
|
false
|
121
135
|
end
|
122
136
|
end
|
123
137
|
|
124
138
|
def ignore(object)
|
125
139
|
case object.class.name
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
false
|
140
|
-
end
|
140
|
+
when 'Hash'
|
141
|
+
ignore object.values
|
142
|
+
when 'Array'
|
143
|
+
object.collect { |item| ignore(item) }.uniq == [true]
|
144
|
+
when 'String', 'Fixnum'
|
145
|
+
ignore(find_conversation(object.to_i))
|
146
|
+
when 'MessageTrain::Conversation'
|
147
|
+
if authorize(object)
|
148
|
+
object.participant_ignore(participant)
|
149
|
+
# We can assume the previous line has succeeded at this point,
|
150
|
+
# because participant_ignore raises an ActiveRecord error otherwise.
|
151
|
+
# Therefore we simply report success, since we got here.
|
152
|
+
results.add(object, :update_successful.l)
|
141
153
|
else
|
154
|
+
false
|
155
|
+
end
|
156
|
+
else
|
142
157
|
errors.add(self, :cannot_ignore_type.l(type: object.class.name))
|
143
158
|
end
|
144
159
|
end
|
145
160
|
|
146
161
|
def unignore(object)
|
147
162
|
case object.class.name
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
false
|
162
|
-
end
|
163
|
+
when 'Hash'
|
164
|
+
unignore object.values
|
165
|
+
when 'Array'
|
166
|
+
object.collect { |item| unignore(item) }.uniq == [true]
|
167
|
+
when 'String', 'Fixnum'
|
168
|
+
unignore(find_conversation(object.to_i))
|
169
|
+
when 'MessageTrain::Conversation'
|
170
|
+
if authorize(object)
|
171
|
+
object.participant_unignore(participant)
|
172
|
+
# We can assume the previous line has succeeded at this point,
|
173
|
+
# because participant_unignore raises an ActiveRecord error
|
174
|
+
# otherwise. Therefore we simply report success, since we got here.
|
175
|
+
results.add(object, :update_successful.l)
|
163
176
|
else
|
177
|
+
false
|
178
|
+
end
|
179
|
+
else
|
164
180
|
errors.add(self, :cannot_unignore_type.l(type: object.class.name))
|
165
181
|
end
|
166
182
|
end
|
167
183
|
|
168
184
|
def title
|
169
|
-
"box_title_#{division
|
185
|
+
"box_title_#{division}".to_sym.l
|
170
186
|
end
|
171
187
|
|
172
188
|
def message
|
@@ -186,25 +202,31 @@ module MessageTrain
|
|
186
202
|
elsif key.to_s =~ /^(conversations|messages)$/
|
187
203
|
data_type = object.class.name
|
188
204
|
case data_type
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
205
|
+
when 'Hash'
|
206
|
+
mark(mark_to_set, key => object.values)
|
207
|
+
when 'Array'
|
208
|
+
object.collect do |item|
|
209
|
+
mark(mark_to_set, key => item)
|
210
|
+
end.uniq == [true]
|
211
|
+
when 'String', 'Fixnum'
|
212
|
+
model_name = "MessageTrain::#{key.to_s.classify}"
|
213
|
+
model = model_name.constantize
|
214
|
+
mark(mark_to_set, key => model.find_by_id!(object.to_i))
|
215
|
+
when 'MessageTrain::Conversation', 'MessageTrain::Message'
|
216
|
+
if authorize(object)
|
217
|
+
object.mark(mark_to_set, participant)
|
218
|
+
# We can assume the previous line has succeeded at this point,
|
219
|
+
# because mark raises an ActiveRecord error otherwise.
|
220
|
+
# Therefore we simply report success, since we got here.
|
221
|
+
results.add(object, :update_successful.l)
|
206
222
|
else
|
207
|
-
|
223
|
+
false
|
224
|
+
end
|
225
|
+
else
|
226
|
+
errors.add(
|
227
|
+
self,
|
228
|
+
:cannot_mark_with_data_type.l(data_type: data_type)
|
229
|
+
)
|
208
230
|
end
|
209
231
|
else
|
210
232
|
errors.add(self, :cannot_mark_type.l(type: key.to_s))
|
@@ -214,23 +236,27 @@ module MessageTrain
|
|
214
236
|
|
215
237
|
def authorize(object)
|
216
238
|
case object.class.name
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
239
|
+
when 'MessageTrain::Conversation'
|
240
|
+
if object.includes_receipts_for? participant
|
241
|
+
true
|
242
|
+
else
|
243
|
+
errors.add(
|
244
|
+
object,
|
245
|
+
:access_to_conversation_id_denied.l(id: object.id)
|
246
|
+
)
|
247
|
+
end
|
248
|
+
when 'MessageTrain::Message'
|
249
|
+
if object.receipts.for(participant).any?
|
250
|
+
true
|
229
251
|
else
|
230
|
-
errors.add(object, :
|
252
|
+
errors.add(object, :access_to_message_id_denied.l(id: object.id))
|
253
|
+
end
|
254
|
+
else
|
255
|
+
errors.add(object, :cannot_authorize_type.l(type: object.class.name))
|
231
256
|
end
|
232
257
|
end
|
233
258
|
|
259
|
+
# Box::Results class
|
234
260
|
class Results
|
235
261
|
attr_accessor :items, :box
|
236
262
|
|
@@ -242,43 +268,53 @@ module MessageTrain
|
|
242
268
|
def add(object, message)
|
243
269
|
item = {}
|
244
270
|
case object.class.name
|
245
|
-
|
246
|
-
|
271
|
+
when 'MessageTrain::Box'
|
272
|
+
item[:css_id] = 'box'
|
273
|
+
route_args = {
|
274
|
+
controller: 'message_train/boxes',
|
275
|
+
action: :show,
|
276
|
+
division: object.division
|
277
|
+
}
|
278
|
+
if box.parent != box.participant
|
279
|
+
collective = box.parent
|
280
|
+
table_part = collective.class.table_name
|
281
|
+
slug_part = collective.send(
|
282
|
+
MessageTrain.configuration.slug_columns[
|
283
|
+
collective.class.table_name.to_sym
|
284
|
+
]
|
285
|
+
)
|
286
|
+
route_args[:collective_id] = "#{table_part}:#{slug_part}"
|
287
|
+
end
|
288
|
+
item[:path] = MessageTrain::Engine.routes.path_for(route_args)
|
289
|
+
when 'MessageTrain::Conversation', 'MessageTrain::Message'
|
290
|
+
if object.new_record?
|
291
|
+
item[:css_id] = "#{object.class.table_name.singularize}"
|
292
|
+
item[:path] = nil
|
293
|
+
else
|
294
|
+
item[:css_id] = "#{object.class.table_name.singularize}_"\
|
295
|
+
"#{object.id.to_s}"
|
247
296
|
route_args = {
|
248
|
-
controller:
|
297
|
+
controller: object.class.table_name
|
298
|
+
.gsub('message_train_', 'message_train/'),
|
249
299
|
action: :show,
|
250
|
-
|
300
|
+
box_division: box.division,
|
301
|
+
id: object.id
|
251
302
|
}
|
252
303
|
if box.parent != box.participant
|
253
304
|
collective = box.parent
|
254
305
|
table_part = collective.class.table_name
|
255
|
-
slug_part = collective.send(
|
306
|
+
slug_part = collective.send(
|
307
|
+
MessageTrain.configuration.slug_columns[
|
308
|
+
collective.class.table_name.to_sym
|
309
|
+
]
|
310
|
+
)
|
256
311
|
route_args[:collective_id] = "#{table_part}:#{slug_part}"
|
257
312
|
end
|
258
313
|
item[:path] = MessageTrain::Engine.routes.path_for(route_args)
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
else
|
264
|
-
item[:css_id] = "#{object.class.table_name.singularize}_#{object.id.to_s}"
|
265
|
-
route_args = {
|
266
|
-
controller: object.class.table_name.gsub('message_train_', 'message_train/'),
|
267
|
-
action: :show,
|
268
|
-
box_division: box.division,
|
269
|
-
id: object.id
|
270
|
-
}
|
271
|
-
if box.parent != box.participant
|
272
|
-
collective = box.parent
|
273
|
-
table_part = collective.class.table_name
|
274
|
-
slug_part = collective.send(MessageTrain.configuration.slug_columns[collective.class.table_name.to_sym])
|
275
|
-
route_args[:collective_id] = "#{table_part}:#{slug_part}"
|
276
|
-
end
|
277
|
-
item[:path] = MessageTrain::Engine.routes.path_for(route_args)
|
278
|
-
end
|
279
|
-
else
|
280
|
-
item[:css_id] = "#{object.class.name.singularize.downcase}"
|
281
|
-
item[:path] = nil
|
314
|
+
end
|
315
|
+
else
|
316
|
+
item[:css_id] = object.class.name.singularize.downcase
|
317
|
+
item[:path] = nil
|
282
318
|
end
|
283
319
|
item[:message] = message
|
284
320
|
items << item
|
@@ -289,6 +325,7 @@ module MessageTrain
|
|
289
325
|
items
|
290
326
|
end
|
291
327
|
end
|
328
|
+
# Box::Errors class
|
292
329
|
class Errors < Results
|
293
330
|
def add(object, message)
|
294
331
|
super object, message
|
@@ -1,4 +1,5 @@
|
|
1
1
|
module MessageTrain
|
2
|
+
# Conversation model
|
2
3
|
class Conversation < ActiveRecord::Base
|
3
4
|
has_many :messages, foreign_key: :message_train_conversation_id
|
4
5
|
has_many :ignores, foreign_key: :message_train_conversation_id
|
@@ -7,12 +8,50 @@ module MessageTrain
|
|
7
8
|
|
8
9
|
# Scopes
|
9
10
|
default_scope { order(updated_at: :desc) }
|
10
|
-
scope :ignored, ->(participant) { where(id: ignored_ids_for(participant))}
|
11
|
-
scope :unignored,
|
12
|
-
|
13
|
-
|
14
|
-
scope :
|
15
|
-
|
11
|
+
scope :ignored, ->(participant) { where(id: ignored_ids_for(participant)) }
|
12
|
+
scope :unignored, (lambda do |participant|
|
13
|
+
where.not(id: ignored_ids_for(participant))
|
14
|
+
end)
|
15
|
+
scope :with_drafts_by, (lambda do |participant|
|
16
|
+
joins(:messages).where(
|
17
|
+
message_train_messages: {
|
18
|
+
id: messages.drafts.with_receipts_by(participant)
|
19
|
+
}
|
20
|
+
)
|
21
|
+
end)
|
22
|
+
scope :with_ready_for, (lambda do |participant|
|
23
|
+
joins(:messages).where(
|
24
|
+
message_train_messages: {
|
25
|
+
id: messages.ready.with_receipts_for(participant)
|
26
|
+
}
|
27
|
+
)
|
28
|
+
end)
|
29
|
+
scope :with_messages_for, (lambda do |participant|
|
30
|
+
joins(:messages).where(
|
31
|
+
message_train_messages: {
|
32
|
+
id: messages.with_receipts_for(participant)
|
33
|
+
}
|
34
|
+
)
|
35
|
+
end)
|
36
|
+
scope :with_messages_through, (lambda do |participant|
|
37
|
+
joins(:messages).where(
|
38
|
+
message_train_messages: {
|
39
|
+
id: messages.with_receipts_through(participant)
|
40
|
+
}
|
41
|
+
)
|
42
|
+
end)
|
43
|
+
|
44
|
+
scope :filter_by_receipt_method, (lambda do |receipt_method, participant|
|
45
|
+
where(
|
46
|
+
id: where(nil).messages.receipts.send(receipt_method, participant)
|
47
|
+
.conversation_ids
|
48
|
+
)
|
49
|
+
end)
|
50
|
+
|
51
|
+
scope :ignored_ids_for, (lambda do |participant|
|
52
|
+
MessageTrain::Ignore.find_all_by_participant(participant)
|
53
|
+
.pluck(:message_train_conversation_id)
|
54
|
+
end)
|
16
55
|
|
17
56
|
def default_recipients_for(sender)
|
18
57
|
default_recipients = []
|
@@ -25,15 +64,15 @@ module MessageTrain
|
|
25
64
|
default_recipients.flatten.uniq
|
26
65
|
end
|
27
66
|
|
28
|
-
def
|
67
|
+
def participant_ignore(participant)
|
29
68
|
ignores.first_or_create!(participant: participant)
|
30
69
|
end
|
31
70
|
|
32
|
-
def
|
71
|
+
def participant_unignore(participant)
|
33
72
|
ignores.where(participant: participant).destroy_all
|
34
73
|
end
|
35
74
|
|
36
|
-
def
|
75
|
+
def participant_ignored?(participant)
|
37
76
|
if ignores.empty?
|
38
77
|
false
|
39
78
|
else
|
@@ -42,10 +81,6 @@ module MessageTrain
|
|
42
81
|
end
|
43
82
|
|
44
83
|
def mark(mark, participant)
|
45
|
-
# # This fixed a bug that only Travis seemed to pick up, but it's code smell.
|
46
|
-
# if messages.nil?
|
47
|
-
# return false
|
48
|
-
# end
|
49
84
|
messages.mark(mark, participant)
|
50
85
|
end
|
51
86
|
|
@@ -54,17 +89,21 @@ module MessageTrain
|
|
54
89
|
end
|
55
90
|
|
56
91
|
def method_missing(method_sym, *arguments, &block)
|
57
|
-
# the first argument is a Symbol, so you need to_s it if you want to
|
92
|
+
# the first argument is a Symbol, so you need to_s it if you want to
|
93
|
+
# pattern match
|
58
94
|
if method_sym.to_s =~ /^includes_((.*)_(by|to|for|through))\?$/
|
59
|
-
case
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
else
|
64
|
-
!messages.send($2).receipts.send("receipts_#{$3}".to_sym, arguments.first).empty?
|
65
|
-
end
|
95
|
+
case Regexp.last_match[2]
|
96
|
+
when 'ready', 'drafts'
|
97
|
+
if Regexp.last_match[3] == 'by'
|
98
|
+
messages.send(Regexp.last_match[2]).by(arguments.first).any?
|
66
99
|
else
|
67
|
-
|
100
|
+
messages.send(Regexp.last_match[2]).receipts.send(
|
101
|
+
"receipts_#{Regexp.last_match[3]}".to_sym,
|
102
|
+
arguments.first
|
103
|
+
).any?
|
104
|
+
end
|
105
|
+
else
|
106
|
+
receipts.send(Regexp.last_match[1].to_sym, arguments.first).any?
|
68
107
|
end
|
69
108
|
else
|
70
109
|
super
|
@@ -72,22 +111,35 @@ module MessageTrain
|
|
72
111
|
end
|
73
112
|
|
74
113
|
def self.method_missing(method_sym, *arguments, &block)
|
75
|
-
# the first argument is a Symbol, so you need to_s it if you want to
|
114
|
+
# the first argument is a Symbol, so you need to_s it if you want to
|
115
|
+
# pattern match
|
76
116
|
if method_sym.to_s =~ /^with_((.*)_(by|to|for|through))$/
|
77
|
-
case
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
117
|
+
case Regexp.last_match[2]
|
118
|
+
when 'ready', 'drafts'
|
119
|
+
messages.send(
|
120
|
+
Regexp.last_match[2]
|
121
|
+
).filter_by_receipt_method(
|
122
|
+
"receipts_#{Regexp.last_match[3]}".to_sym,
|
123
|
+
arguments.first
|
124
|
+
).conversations
|
125
|
+
when 'messages'
|
126
|
+
messages.filter_by_receipt_method(
|
127
|
+
"receipts_#{Regexp.last_match[3]}".to_sym,
|
128
|
+
arguments.first
|
129
|
+
).conversations
|
130
|
+
else
|
131
|
+
filter_by_receipt_method(
|
132
|
+
Regexp.last_match[1].to_sym,
|
133
|
+
arguments.first
|
134
|
+
)
|
84
135
|
end
|
85
136
|
else
|
86
137
|
super
|
87
138
|
end
|
88
139
|
end
|
89
140
|
|
90
|
-
# It's important to know Object defines respond_to to take two parameters:
|
141
|
+
# It's important to know Object defines respond_to to take two parameters:
|
142
|
+
# the method to check, and whether to include private methods
|
91
143
|
# http://www.ruby-doc.org/core/classes/Object.html#M000333
|
92
144
|
def respond_to?(method_sym, include_private = false)
|
93
145
|
if method_sym.to_s =~ /^includes_((.*)_(by|to|for|through))\?$/
|
@@ -104,14 +156,5 @@ module MessageTrain
|
|
104
156
|
super
|
105
157
|
end
|
106
158
|
end
|
107
|
-
|
108
|
-
private
|
109
|
-
scope :filter_by_receipt_method, ->(receipt_method, participant) {
|
110
|
-
where(id: where(nil).messages.receipts.send(receipt_method, participant).conversation_ids)
|
111
|
-
}
|
112
|
-
|
113
|
-
def self.ignored_ids_for(participant)
|
114
|
-
MessageTrain::Ignore.find_all_by_participant(participant).pluck(:message_train_conversation_id)
|
115
|
-
end
|
116
159
|
end
|
117
|
-
end
|
160
|
+
end
|