Class: Discussion
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Discussion
- Defined in:
- app/models/discussion.rb
Instance Attribute Summary collapse
-
#recipient_ids ⇒ Object
Returns the value of attribute recipient_ids.
-
#recipient_tokens ⇒ Object
Returns the value of attribute recipient_tokens.
Class Method Summary collapse
-
.find_between_users(user, user2) ⇒ Object
don’t allow to create discussion with user, if discussion with this user already exists TODO move to separated method and return boolean value.
Instance Method Summary collapse
- #add_recipient_token(id) ⇒ Object
- #add_speaker(user) ⇒ Object
- #can_participate?(user) ⇒ Boolean
- #find_speaker_by_user(user) ⇒ Object
- #mark_as_read_for(user) ⇒ Object
-
#private? ⇒ Boolean
private/group discussion.
- #remove_speaker(user) ⇒ Object
- #unread_for?(user) ⇒ Boolean
-
#unread_messages_count_for(user) ⇒ Object
return amount of unreaded messages for current discussion.
- #user_invited_at(user) ⇒ Object
Instance Attribute Details
#recipient_ids ⇒ Object
Returns the value of attribute recipient_ids.
2 3 4 |
# File 'app/models/discussion.rb', line 2 def recipient_ids @recipient_ids end |
#recipient_tokens ⇒ Object
Returns the value of attribute recipient_tokens.
2 3 4 |
# File 'app/models/discussion.rb', line 2 def recipient_tokens @recipient_tokens end |
Class Method Details
.find_between_users(user, user2) ⇒ Object
don’t allow to create discussion with user, if discussion with this user already exists TODO move to separated method and return boolean value
66 67 68 69 70 71 72 73 74 |
# File 'app/models/discussion.rb', line 66 def self.find_between_users(user, user2) dialog = nil discussions = self.joins(:speakers).includes(:users).where("speakers.user_id = ?", user.id) Rails.logger.info "Searching for ids: #{user.id}, #{user2.id}" discussions.each do |discussion| dialog = discussion if discussion.private? && ([discussion.users.first, discussion.users.last] - [user, user2]).empty? end dialog end |
Instance Method Details
#add_recipient_token(id) ⇒ Object
42 43 44 |
# File 'app/models/discussion.rb', line 42 def add_recipient_token id self.recipient_ids << id if self.recipient_ids end |
#add_speaker(user) ⇒ Object
46 47 48 49 |
# File 'app/models/discussion.rb', line 46 def add_speaker(user) raise ArgumentError, "You can add speaker only to existing Discussion. Save your the Discussion object firstly" if new_record? Speaker.create(:discussion => self, :user => user, :updated_at => self.updated_at) end |
#can_participate?(user) ⇒ Boolean
60 61 62 |
# File 'app/models/discussion.rb', line 60 def can_participate?(user) !!find_speaker_by_user(user) end |
#find_speaker_by_user(user) ⇒ Object
101 102 103 |
# File 'app/models/discussion.rb', line 101 def find_speaker_by_user user Speaker.find_by_discussion_id_and_user_id(self.id, user.id) end |
#mark_as_read_for(user) ⇒ Object
96 97 98 99 |
# File 'app/models/discussion.rb', line 96 def mark_as_read_for(user) speaker = Speaker.find_or_create_by_user_id_and_discussion_id(user.id, self.id) speaker.touch end |
#private? ⇒ Boolean
private/group discussion
77 78 79 |
# File 'app/models/discussion.rb', line 77 def private? self.users.size <= 2 end |
#remove_speaker(user) ⇒ Object
51 52 53 54 |
# File 'app/models/discussion.rb', line 51 def remove_speaker(user) speaker = find_speaker_by_user(user) speaker.destroy if speaker end |
#unread_for?(user) ⇒ Boolean
81 82 83 84 85 86 87 88 |
# File 'app/models/discussion.rb', line 81 def unread_for?(user) speaker = find_speaker_by_user(user) if speaker self.updated_at >= speaker.updated_at else true end end |
#unread_messages_count_for(user) ⇒ Object
return amount of unreaded messages for current discussion
91 92 93 94 |
# File 'app/models/discussion.rb', line 91 def (user) speaker = find_speaker_by_user(user) .where("updated_at > ?", speaker.updated_at ).where("user_id != ?", speaker.id ).count end |
#user_invited_at(user) ⇒ Object
56 57 58 |
# File 'app/models/discussion.rb', line 56 def user_invited_at(user) find_speaker_by_user(user).created_at end |