Module: Followability::Followable::Actions::Block

Defined in:
lib/followability/followable/actions/block.rb

Constant Summary collapse

I18N_SCOPE =
'followability.errors.block'

Instance Method Summary collapse

Instance Method Details

#block(record) ⇒ Object



9
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
35
36
37
38
39
40
41
# File 'lib/followability/followable/actions/block.rb', line 9

def block(record)
  if myself?(record)
    errors.add(:base, I18n.t('block.myself', scope: I18N_SCOPE, klass: record.class))

    return false
  end

  if blocked_by?(record)
    errors.add(:base, I18n.t('block.blocked_by', scope: I18N_SCOPE, klass: record.class))

    return false
  end

  if blocked?(record)
    errors.add(:base, I18n.t('block.already_blocked', scope: I18N_SCOPE, klass: record.class))

    return false
  end

  relation = followerable_relationships.blocked.new(followable: record,
                                                    status: Followability::Relationship.statuses[:blocked])

  if relation.save
    run_callback(self, affected: record, callback: :followable_blocked_by_me)
    run_callback(record, affected: self, callback: :followable_blocked_by_someone)

    true
  else
    errors.add(:base, relation.errors.full_messages.to_sentence)

    false
  end
end

#blocked?(record) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/followability/followable/actions/block.rb', line 70

def blocked?(record)
  followerable_relationships.blocked.exists?(followable: record)
end

#blocked_by?(record) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/followability/followable/actions/block.rb', line 74

def blocked_by?(record)
  record.followerable_relationships.blocked.exists?(followable: self)
end

#unblock(record) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/followability/followable/actions/block.rb', line 43

def unblock(record)
  if myself?(record)
    errors.add(:base, I18n.t('unblock.myself', scope: I18N_SCOPE, klass: record.class))

    return false
  end

  unless blocked?(record)
    errors.add(:base, I18n.t(:not_blocked_for_blocking, scope: I18N_SCOPE, klass: record.class))

    return false
  end

  relation = followerable_relationships.blocked.find_by(followable: record)

  if relation.destroy
    run_callback(self, affected: record, callback: :followable_unblocked_by_me)
    run_callback(record, affected: self, callback: :followable_unblocked_by_someone)

    true
  else
    errors.add(:base, relation.errors.full_messages.to_sentence)

    false
  end
end