message_train 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +20 -0
  3. data/.ruby-gemset +1 -1
  4. data/.scss-lint.yml +7 -0
  5. data/.simplecov +1 -1
  6. data/.travis.yml +1 -1
  7. data/Gemfile +6 -2
  8. data/LICENSE.txt +2 -2
  9. data/README.rdoc +5 -5
  10. data/Rakefile +22 -21
  11. data/VERSION +1 -1
  12. data/app/assets/javascripts/message_train.js +1 -1
  13. data/app/assets/stylesheets/message_train.scss +29 -19
  14. data/app/controllers/concerns/message_train_support.rb +41 -37
  15. data/app/controllers/message_train/application_controller.rb +1 -0
  16. data/app/controllers/message_train/boxes_controller.rb +5 -4
  17. data/app/controllers/message_train/conversations_controller.rb +8 -7
  18. data/app/controllers/message_train/messages_controller.rb +25 -22
  19. data/app/controllers/message_train/participants_controller.rb +27 -23
  20. data/app/controllers/message_train/unsubscribes_controller.rb +37 -23
  21. data/app/helpers/message_train/application_helper.rb +6 -8
  22. data/app/helpers/message_train/attachments_helper.rb +12 -4
  23. data/app/helpers/message_train/boxes_helper.rb +38 -11
  24. data/app/helpers/message_train/collectives_helper.rb +38 -12
  25. data/app/helpers/message_train/conversations_helper.rb +43 -35
  26. data/app/helpers/message_train/messages_helper.rb +32 -25
  27. data/app/mailers/message_train/application_mailer.rb +7 -4
  28. data/app/mailers/message_train/previews/receipt_mailer_preview.rb +2 -3
  29. data/app/mailers/message_train/receipt_mailer.rb +14 -4
  30. data/app/models/message_train/attachment.rb +41 -25
  31. data/app/models/message_train/box.rb +159 -122
  32. data/app/models/message_train/conversation.rb +84 -41
  33. data/app/models/message_train/ignore.rb +8 -2
  34. data/app/models/message_train/message.rb +72 -42
  35. data/app/models/message_train/receipt.rb +30 -17
  36. data/app/models/message_train/unsubscribe.rb +1 -0
  37. data/app/views/application/404.html.haml +1 -1
  38. data/app/views/layouts/mailer.html.haml +2 -2
  39. data/app/views/layouts/mailer.text.haml +1 -1
  40. data/app/views/message_train/application/_attachment_fields.html.haml +1 -1
  41. data/app/views/message_train/application/_widget.html.haml +1 -1
  42. data/app/views/message_train/application/results.json.jbuilder +1 -1
  43. data/app/views/message_train/boxes/_dropdown_list.html.haml +1 -1
  44. data/app/views/message_train/boxes/_list_item.html.haml +1 -1
  45. data/app/views/message_train/boxes/show.html.haml +1 -1
  46. data/app/views/message_train/collectives/_dropdown_list.html.haml +1 -1
  47. data/app/views/message_train/collectives/_list_item.html.haml +1 -1
  48. data/app/views/message_train/conversations/_deleted_toggle.html.haml +1 -1
  49. data/app/views/message_train/conversations/_ignored_toggle.html.haml +2 -2
  50. data/app/views/message_train/conversations/_read_toggle.html.haml +1 -1
  51. data/app/views/message_train/conversations/_toggle.html.haml +1 -1
  52. data/app/views/message_train/conversations/_trashed_toggle.html.haml +1 -1
  53. data/app/views/message_train/conversations/show.html.haml +2 -2
  54. data/app/views/message_train/conversations/show.json.jbuilder +5 -1
  55. data/app/views/message_train/messages/_form.html.haml +1 -1
  56. data/app/views/message_train/messages/_message.html.haml +6 -6
  57. data/app/views/message_train/messages/_toggle.html.haml +1 -1
  58. data/app/views/message_train/messages/show.json.jbuilder +5 -1
  59. data/app/views/message_train/participants/_participant.json.jbuilder +1 -1
  60. data/app/views/message_train/participants/index.json.jbuilder +1 -1
  61. data/app/views/message_train/participants/show.json.jbuilder +1 -1
  62. data/app/views/message_train/receipt_mailer/notification_email.html.haml +1 -1
  63. data/app/views/message_train/receipt_mailer/notification_email.text.haml +1 -1
  64. data/app/views/message_train/unsubscribes/index.html.haml +1 -1
  65. data/config/environment.rb +1 -1
  66. data/config/initializers/date_time.rb +2 -2
  67. data/config/locales/en.yml +1 -1
  68. data/config/routes.rb +37 -15
  69. data/lib/generators/message_train/install/install_generator.rb +26 -11
  70. data/lib/generators/message_train/install/templates/initializer.rb +0 -2
  71. data/lib/generators/message_train/utils.rb +11 -4
  72. data/lib/message_train/configuration.rb +4 -5
  73. data/lib/message_train/engine.rb +1 -7
  74. data/lib/message_train/localization.rb +14 -18
  75. data/lib/message_train/mixin.rb +211 -150
  76. data/lib/message_train/version.rb +1 -0
  77. data/lib/message_train.rb +7 -6
  78. data/message_train.gemspec +24 -11
  79. data/spec/controllers/message_train/boxes_controller_spec.rb +63 -15
  80. data/spec/controllers/message_train/concerns_spec.rb +17 -13
  81. data/spec/controllers/message_train/conversations_controller_spec.rb +44 -12
  82. data/spec/controllers/message_train/messages_controller_spec.rb +87 -40
  83. data/spec/controllers/message_train/participants_controller_spec.rb +10 -4
  84. data/spec/controllers/message_train/unsubscribes_controller_spec.rb +43 -13
  85. data/spec/dummy/Rakefile +2 -1
  86. data/spec/dummy/app/assets/stylesheets/application.css.scss +7 -23
  87. data/spec/dummy/app/controllers/application_controller.rb +7 -7
  88. data/spec/dummy/app/models/group.rb +1 -1
  89. data/spec/dummy/app/models/role.rb +11 -7
  90. data/spec/dummy/app/models/user.rb +6 -5
  91. data/spec/dummy/app/views/layouts/_top_navigation.html.haml +1 -1
  92. data/spec/dummy/app/views/layouts/application.html.haml +1 -1
  93. data/spec/dummy/app/views/pages/index.html.haml +1 -1
  94. data/spec/dummy/config/application.rb +23 -17
  95. data/spec/dummy/config/environments/development.rb +5 -4
  96. data/spec/dummy/config/environments/production.rb +10 -6
  97. data/spec/dummy/config/environments/test.rb +4 -3
  98. data/spec/dummy/config/initializers/assets.rb +2 -1
  99. data/spec/dummy/config/initializers/backtrace_silencers.rb +4 -2
  100. data/spec/dummy/config/initializers/bootstrap_leather.rb +1 -1
  101. data/spec/dummy/config/initializers/devise.rb +63 -44
  102. data/spec/dummy/config/initializers/high_voltage.rb +1 -1
  103. data/spec/dummy/config/initializers/message_train.rb +5 -2
  104. data/spec/dummy/config/initializers/mime_types.rb +1 -1
  105. data/spec/dummy/config/initializers/paperclip.rb +5 -2
  106. data/spec/dummy/config/initializers/rolify.rb +3 -2
  107. data/spec/dummy/config/initializers/wrap_parameters.rb +2 -1
  108. data/spec/dummy/config/routes.rb +1 -3
  109. data/spec/dummy/config/settings.yml +1 -1
  110. data/spec/dummy/db/schema.rb +98 -98
  111. data/spec/dummy/db/seeds/conversations.seeds.rb +160 -160
  112. data/spec/dummy/db/seeds/development/conversations.seeds.rb +6 -2
  113. data/spec/dummy/db/seeds/groups.seeds.rb +11 -12
  114. data/spec/dummy/db/seeds/test/attachments.seeds.rb +13 -3
  115. data/spec/dummy/db/seeds/test/conversations.seeds.rb +6 -2
  116. data/spec/dummy/db/seeds/unsubscribes.seeds.rb +1 -1
  117. data/spec/dummy/db/seeds/users.seeds.rb +47 -42
  118. data/spec/dummy/db/test.sqlite3 +0 -0
  119. data/spec/factories/attachment.rb +6 -2
  120. data/spec/factories/group.rb +1 -4
  121. data/spec/factories/message.rb +29 -23
  122. data/spec/factories/user.rb +6 -4
  123. data/spec/features/boxes_spec.rb +76 -24
  124. data/spec/features/conversations_spec.rb +19 -7
  125. data/spec/features/messages_spec.rb +24 -5
  126. data/spec/features/unsubscribes_spec.rb +36 -9
  127. data/spec/helpers/message_train/application_helper_spec.rb +1 -1
  128. data/spec/helpers/message_train/attachment_helper_spec.rb +27 -12
  129. data/spec/helpers/message_train/boxes_helper_spec.rb +1 -1
  130. data/spec/helpers/message_train/collectives_helper_spec.rb +25 -9
  131. data/spec/helpers/message_train/conversations_helper_spec.rb +239 -120
  132. data/spec/helpers/message_train/messages_helper_spec.rb +137 -81
  133. data/spec/message_train_spec.rb +3 -2
  134. data/spec/models/group_spec.rb +12 -9
  135. data/spec/models/message_train/attachment_spec.rb +33 -30
  136. data/spec/models/message_train/box_spec.rb +243 -60
  137. data/spec/models/message_train/conversation_spec.rb +16 -12
  138. data/spec/models/message_train/ignore_spec.rb +3 -1
  139. data/spec/models/message_train/message_spec.rb +22 -7
  140. data/spec/models/message_train/receipt_spec.rb +14 -4
  141. data/spec/models/role_spec.rb +10 -6
  142. data/spec/models/user_spec.rb +74 -22
  143. data/spec/rails_helper.rb +9 -4
  144. data/spec/spec_helper.rb +6 -54
  145. data/spec/support/controller_behaviors.rb +8 -5
  146. data/spec/support/controller_macros.rb +2 -2
  147. data/spec/support/factory_girl.rb +1 -1
  148. data/spec/support/feature_behaviors.rb +42 -13
  149. data/spec/support/loaded_site/attachments.rb +8 -0
  150. data/spec/support/loaded_site/conversations.rb +73 -0
  151. data/spec/support/{groups.rb → loaded_site/groups.rb} +1 -1
  152. data/spec/support/{loaded_site.rb → loaded_site/loaded_site.rb} +1 -1
  153. data/spec/support/{messages.rb → loaded_site/messages.rb} +13 -5
  154. data/spec/support/{roles.rb → loaded_site/roles.rb} +1 -1
  155. data/spec/support/{users.rb → loaded_site/users.rb} +1 -1
  156. data/spec/support/shared_connection.rb +8 -6
  157. data/spec/support/utilities.rb +10 -10
  158. metadata +67 -10
  159. data/spec/dummy/config/unused/temporary_i18n_debugging.rb +0 -12
  160. data/spec/support/attachments.rb +0 -4
  161. 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 :attachment, styles: lambda { |attachment|
5
- attachment.instance.image? ? {
6
- thumb: '235x',
7
- large: '800x'
8
- } : {}
9
- },
10
- path: ':rails_root/public/system/:rails_env/:class/:attachment/:id_partition/:style_prefix:filename',
11
- url: '/system/:rails_env/:class/:attachment/:id_partition/:style_prefix:filename',
12
- convert_options: {
13
- large: '-quality 75 -interlace Plane -strip',
14
- thumb: '-quality 75 -strip',
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 :attachment, content_type: [
18
- 'application/pdf',
19
- 'application/vnd.ms-excel',
20
- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
21
- 'application/msword',
22
- 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
23
- 'application/rtf',
24
- 'text/plain',
25
- /^(image|(x-)?application)\/(bmp|gif|jpeg|jpg|pjpeg|png|x-png)$/
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
- (attachment_content_type =~ /^(image|(x-)?application)\/(bmp|gif|jpeg|jpg|pjpeg|png|x-png)$/) ? true : false
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.to_s}/" : ''
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
- alias_method :id, :division
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
- found = found.with_drafts_by(participant)
42
- else
43
- found = found.with_ready_for(participant)
44
- end
45
- if division == :ignored
46
- found = found.ignored(participant)
47
- else
48
- found = found.unignored(participant)
49
- end
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><p>&nbsp;</p>"
65
+ message.body = "<blockquote>#{previous_message.body}</blockquote>"\
66
+ '<p>&nbsp;</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(MessageTrain.configuration.slug_columns[table_name.to_sym])
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(message_to_send, :invalid_sender_for_thing.l(thing: "#{parent.class.name} #{parent.id}"))
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(message_to_send, message_to_send.errors.full_messages.to_sentence)
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(message_to_update, attributes)
109
+ def update_message(message, attributes)
104
110
  attributes.delete(:sender)
105
- if message_to_update.sender == participant && parent.valid_senders.include?(participant)
106
- message_to_update.update(attributes)
107
- message_to_update.reload
108
- if message_to_update.errors.empty?
109
- if message_to_update.draft
110
- results.add(message_to_update, :message_saved_as_draft.l)
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(message_to_update, :message_sent.l)
120
+ results.add(message, :message_sent.l)
113
121
  end
114
122
  else
115
- errors.add(message_to_update, message_to_update.errors.full_messages.to_sentence)
123
+ errors.add(
124
+ message,
125
+ message.errors.full_messages.to_sentence
126
+ )
116
127
  end
117
- message_to_update
128
+ message
118
129
  else
119
- errors.add(message_to_update, :access_to_message_id_denied.l(id: message_to_update.id))
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
- when 'Hash'
127
- ignore object.values
128
- when 'Array'
129
- object.collect { |item| ignore(item) }.uniq == [true]
130
- when 'String', 'Fixnum'
131
- ignore(find_conversation(object.to_i))
132
- when 'MessageTrain::Conversation'
133
- if authorize(object)
134
- object.set_ignored(participant)
135
- # We can assume the previous line has succeeded at this point, because set_ignored raises
136
- # an ActiveRecord error otherwise. Therefore we simply report success, since we got here.
137
- results.add(object, :update_successful.l)
138
- else
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
- when 'Hash'
149
- unignore object.values
150
- when 'Array'
151
- object.collect { |item| unignore(item) }.uniq == [true]
152
- when 'String', 'Fixnum'
153
- unignore(find_conversation(object.to_i))
154
- when 'MessageTrain::Conversation'
155
- if authorize(object)
156
- object.set_unignored(participant)
157
- # We can assume the previous line has succeeded at this point, because set_unignored raises
158
- # an ActiveRecord error otherwise. Therefore we simply report success, since we got here.
159
- results.add(object, :update_successful.l)
160
- else
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.to_s}".to_sym.l
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
- when 'Hash'
190
- mark(mark_to_set, { key => object.values } )
191
- when 'Array'
192
- object.collect { |item| mark(mark_to_set, key => item) }.uniq == [true]
193
- when 'String', 'Fixnum'
194
- model_name = "MessageTrain::#{key.to_s.classify}"
195
- model = model_name.constantize
196
- mark(mark_to_set, key => model.find_by_id!(object.to_i))
197
- when 'MessageTrain::Conversation', 'MessageTrain::Message'
198
- if authorize(object)
199
- object.mark(mark_to_set, participant)
200
- # We can assume the previous line has succeeded at this point, because mark raises
201
- # an ActiveRecord error otherwise. Therefore we simply report success, since we got here.
202
- results.add(object, :update_successful.l)
203
- else
204
- false
205
- end
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
- errors.add(self, :cannot_mark_with_data_type.l(data_type: data_type))
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
- when 'MessageTrain::Conversation'
218
- if object.includes_receipts_for? participant
219
- true
220
- else
221
- errors.add(object, :access_to_conversation_id_denied.l(id: object.id))
222
- end
223
- when 'MessageTrain::Message'
224
- if object.receipts.for(participant).any?
225
- true
226
- else
227
- errors.add(object, :access_to_message_id_denied.l(id: object.id))
228
- end
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, :cannot_authorize_type.l(type: object.class.name))
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
- when 'MessageTrain::Box'
246
- item[:css_id] = 'box'
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: 'message_train/boxes',
297
+ controller: object.class.table_name
298
+ .gsub('message_train_', 'message_train/'),
249
299
  action: :show,
250
- division: object.division
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(MessageTrain.configuration.slug_columns[collective.class.table_name.to_sym])
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
- when 'MessageTrain::Conversation', 'MessageTrain::Message'
260
- if object.new_record?
261
- item[:css_id] = "#{object.class.table_name.singularize}"
262
- item[:path] = nil
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, ->(participant) { where.not(id: ignored_ids_for(participant)) }
12
- scope :with_drafts_by, ->(participant) { joins(:messages).where(message_train_messages: { id: messages.drafts.with_receipts_by(participant) }) }
13
- scope :with_ready_for, ->(participant) { joins(:messages).where(message_train_messages: { id: messages.ready.with_receipts_for(participant) }) }
14
- scope :with_messages_for, ->(participant) { joins(:messages).where(message_train_messages: { id: messages.with_receipts_for(participant) }) }
15
- scope :with_messages_through, ->(participant) { joins(:messages).where(message_train_messages: { id: messages.with_receipts_through(participant) }) }
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 set_ignored(participant)
67
+ def participant_ignore(participant)
29
68
  ignores.first_or_create!(participant: participant)
30
69
  end
31
70
 
32
- def set_unignored(participant)
71
+ def participant_unignore(participant)
33
72
  ignores.where(participant: participant).destroy_all
34
73
  end
35
74
 
36
- def is_ignored?(participant)
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 pattern match
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 $2
60
- when 'ready', 'drafts'
61
- if $3 == 'by'
62
- !messages.send($2).by(arguments.first).empty?
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
- !receipts.send($1.to_sym, arguments.first).empty?
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 pattern match
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 $2
78
- when 'ready', 'drafts'
79
- self.messages.send($2).filter_by_receipt_method("receipts_#{$3}".to_sym, arguments.first).conversations
80
- when 'messages'
81
- self.messages.filter_by_receipt_method("receipts_#{$3}".to_sym, arguments.first).conversations
82
- else
83
- self.filter_by_receipt_method($1.to_sym, arguments.first)
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: the method to check, and whether to include private methods
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