Module: AdminBits::Scopes::ClassMethods

Defined in:
lib/admin_bits/scopes.rb

Instance Method Summary collapse

Instance Method Details

#admin_extension_scopesObject



9
10
11
# File 'lib/admin_bits/scopes.rb', line 9

def admin_extension_scopes
  @@admin_extension_scopes || {}
end

#declare_text_search_scope(name, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/admin_bits/scopes.rb', line 36

def declare_text_search_scope(name, options = {})
  @@admin_extension_scopes ||= {}
  @@admin_extension_scopes[name] = :text

  scope name, ->(text) do
    if text.present?
      columns = options.fetch(:on)
      where(AdminExtension::Utils.create_search_conditions(text, columns))
    else
      where(nil)
    end
  end
end

#declare_time_range_scope(name, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/admin_bits/scopes.rb', line 13

def declare_time_range_scope(name, options = {})
  @@admin_extension_scopes ||= {}
  @@admin_extension_scopes[name] = :time_range

  scope name, ->(date_from, date_to) do
    column = options.fetch(:on)
    date_from = Time.parse(date_from) rescue nil
    date_to   = Time.parse(date_to) rescue nil

    result_scope = where(nil)

    if date_from
      result_scope = result_scope.where(arel_table[column].gteq(date_from))
    end

    if date_to
      result_scope = result_scope.where(arel_table[column].lteq(date_to))
    end

    result_scope
  end
end