Module: Followability::Followable::Actions::Follow
- Defined in:
- lib/followability/followable/actions/follow.rb
Constant Summary collapse
- I18N_SCOPE =
'followability.errors.follow'
Instance Method Summary collapse
- #accept_follow_request_of(record) ⇒ Object
- #decline_follow_request_of(record) ⇒ Object
-
#following?(record) ⇒ Boolean
rubocop:enable Metrics/AbcSize, Metrics/MethodLength.
- #mutual_following_with?(record) ⇒ Boolean
- #remove_follow_request_for(record) ⇒ Object
-
#send_follow_request_to(record) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
- #sent_follow_request_to?(record) ⇒ Boolean
- #unfollow(record) ⇒ Object
Instance Method Details
#accept_follow_request_of(record) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/followability/followable/actions/follow.rb', line 65 def accept_follow_request_of(record) if myself?(record) errors.add(:base, I18n.t('accept_follow_request_of.myself', scope: I18N_SCOPE, klass: record.class)) return false end relation = follow_requests.find_by(followerable_id: record.id, followerable_type: record.class.name) if relation.blank? errors.add(:base, I18n.t('accept_follow_request_of.empty_relation', scope: I18N_SCOPE, klass: record.class.name)) false elsif relation.update(status: Followability::Relationship.statuses[:following]) run_callback(record, affected: record, callback: :follow_request_accepted_by_someone) run_callback(self, affected: self, callback: :follow_request_accepted_by_me) true else errors.add(:base, relation.errors..to_sentence) false end end |
#decline_follow_request_of(record) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/followability/followable/actions/follow.rb', line 10 def decline_follow_request_of(record) if myself?(record) errors.add(:base, I18n.t('decline_follow_request_of.myself', scope: I18N_SCOPE, klass: record.class)) return false end relation = follow_requests.find_by(followerable_id: record.id, followerable_type: record.class.name) if relation.blank? errors.add(:base, I18n.t('decline_follow_request_of.empty_relation', scope: I18N_SCOPE, klass: record.class.name)) false elsif relation.destroy run_callback(self, affected: record, callback: :follow_request_declined_by_me) run_callback(record, affected: self, callback: :follow_request_declined_by_someone) true else errors.add(:base, relation.errors..to_sentence) false end end |
#following?(record) ⇒ Boolean
rubocop:enable Metrics/AbcSize, Metrics/MethodLength
168 169 170 |
# File 'lib/followability/followable/actions/follow.rb', line 168 def following?(record) following.exists?(id: record.id) end |
#mutual_following_with?(record) ⇒ Boolean
172 173 174 |
# File 'lib/followability/followable/actions/follow.rb', line 172 def mutual_following_with?(record) following.exists?(id: record.id) && followers.exists?(id: record.id) end |
#remove_follow_request_for(record) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/followability/followable/actions/follow.rb', line 91 def remove_follow_request_for(record) if myself?(record) errors.add(:base, I18n.t('remove_follow_request_for.myself', scope: I18N_SCOPE, klass: record.class)) return false end relation = pending_requests.find_by(followable_id: record.id, followable_type: record.class.name) if relation.blank? errors.add(:base, I18n.t('remove_follow_request_for.empty_relation', scope: I18N_SCOPE, klass: record.class.name)) false elsif relation.destroy run_callback(self, affected: record, callback: :follow_request_removed_by_me) run_callback(record, affected: self, callback: :follow_request_removed_by_someone) true else errors.add(:base, relation.errors..to_sentence) false end end |
#send_follow_request_to(record) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/followability/followable/actions/follow.rb', line 118 def send_follow_request_to(record) if myself?(record) errors.add(:base, I18n.t('send_follow_request_to.myself', scope: I18N_SCOPE, klass: record.class)) return false end if blocked_by?(record) errors.add(:base, I18n.t('send_follow_request_to.blocked_by', scope: I18N_SCOPE, klass: record.class.name)) return false end if following?(record) errors.add(:base, I18n.t('send_follow_request_to.following', scope: I18N_SCOPE, klass: pluralize(record.class.name))) return false end if sent_follow_request_to?(record) errors.add(:base, I18n.t('send_follow_request_to.already_sent', scope: I18N_SCOPE, klass: record.class.name)) return false end if blocked?(record) errors.add(:base, I18n.t('send_follow_request_to.blocked', scope: I18N_SCOPE, klass: record.class.name)) return false end relation = pending_requests.new(followable: record, status: Followability::Relationship.statuses[:requested]) if relation.save run_callback(self, affected: record, callback: :follow_request_sent_to_someone) run_callback(record, affected: self, callback: :follow_request_sent_to_me) true else errors.add(:base, relation.errors..to_sentence) false end end |
#sent_follow_request_to?(record) ⇒ Boolean
176 177 178 |
# File 'lib/followability/followable/actions/follow.rb', line 176 def sent_follow_request_to?(record) record.follow_requests.exists?(followerable_id: id, followerable_type: self.class.name) end |
#unfollow(record) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/followability/followable/actions/follow.rb', line 36 def unfollow(record) if myself?(record) errors.add(:base, I18n.t('unfollow.myself', scope: I18N_SCOPE, klass: record.class)) return false end unless following?(record) errors.add(:base, I18n.t('unfollow.not_following', scope: I18N_SCOPE, klass: record.class)) return false end relation = followerable_relationships.find_by(followable_id: record.id, followable_type: record.class.name, status: Followable::Relationship.statuses[:following]) if relation.destroy run_callback(self, affected: record, callback: :unfollowed_by_me) run_callback(record, affected: self, callback: :unfollowed_by_someone) true else errors.add(:base, relation.errors.) false end end |