Module: ActsAsMessageable::Scopes::ClassMethods

Extended by:
T::Helpers, T::Sig
Includes:
Kernel
Defined in:
lib/acts_as_messageable/scopes.rb

Instance Method Summary collapse

Instance Method Details

#define_base_scopesObject (private)



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/acts_as_messageable/scopes.rb', line 53

def define_base_scopes
  scope :are_from, lambda { |*args|
    where(sent_messageable_id: args.first, sent_messageable_type: args.first.class.name)
  }
  scope :are_to, lambda { |*args|
    where(received_messageable_id: args.first, received_messageable_type: args.first.class.name)
  }
  define_connected_with_scope
  scope :readed, -> { where('opened_at is not null OR opened = ?', true) }
  scope :unreaded, -> { where('opened_at is null OR opened = ?', false) }
  scope :deleted, -> { where(recipient_delete: true, sender_delete: true) }
end

#define_connected_with_scopeObject (private)



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/acts_as_messageable/scopes.rb', line 67

def define_connected_with_scope
  scope :connected_with, lambda { |*args|
    where("(sent_messageable_type = :sent_type and
                  sent_messageable_id = :sent_id and
                  sender_delete = :s_delete and sender_permanent_delete = :s_perm_delete) or
                  (received_messageable_type = :received_type and
                  received_messageable_id = :received_id and
                  recipient_delete = :r_delete and recipient_permanent_delete = :r_perm_delete)",
          sent_type: args.first.class.resolve_active_record_ancestor.to_s,
          sent_id: args.first.id,
          received_type: args.first.class.resolve_active_record_ancestor.to_s,
          received_id: args.first.id,
          r_delete: args.last,
          s_delete: args.last,
          r_perm_delete: false,
          s_perm_delete: false)
  }
end

#define_search_scope(search_scope) ⇒ Object (private)



87
88
89
90
91
# File 'lib/acts_as_messageable/scopes.rb', line 87

def define_search_scope(search_scope)
  scope search_scope, lambda { |*args|
    where('body like :search_txt or topic like :search_txt', search_txt: "%#{args.first}%")
  }
end

#initialize_scope_trackingObject (private)



33
34
35
36
# File 'lib/acts_as_messageable/scopes.rb', line 33

def initialize_scope_tracking
  @initialized_search_scopes ||= T.let(nil, T.nilable(T::Array[T.any(String, Symbol)]))
  @initialized_search_scopes = [] if @initialized_search_scopes.nil?
end

#initialize_scopes(search_scope) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/acts_as_messageable/scopes.rb', line 21

def initialize_scopes(search_scope)
  initialize_scope_tracking
  return if scope_already_initialized?(search_scope)

  mark_scope_as_initialized(search_scope)
  define_base_scopes unless respond_to?(:are_from)
  define_search_scope(search_scope)
end

#mark_scope_as_initialized(search_scope) ⇒ Object (private)



46
47
48
49
50
# File 'lib/acts_as_messageable/scopes.rb', line 46

def mark_scope_as_initialized(search_scope)
  return if @initialized_search_scopes.nil?

  @initialized_search_scopes << search_scope unless @initialized_search_scopes.include?(search_scope)
end

#scope_already_initialized?(search_scope) ⇒ Boolean (private)

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/acts_as_messageable/scopes.rb', line 39

def scope_already_initialized?(search_scope)
  return false if @initialized_search_scopes.nil?

  respond_to?(:are_from) && @initialized_search_scopes.include?(search_scope)
end