Module: ActAsDisabled::Query

Defined in:
lib/act_as_disabled.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



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

def self.extended(base)
  base.define_callbacks :disable
end

Instance Method Details

#aad_columnObject



57
58
59
# File 'lib/act_as_disabled.rb', line 57

def aad_column
  aad_config[:column].to_sym
end

#aad_column_referenceObject



64
65
66
# File 'lib/act_as_disabled.rb', line 64

def aad_column_reference
  "#{table_name}.#{aad_column}"
end

#aad_column_typeObject



60
61
62
# File 'lib/act_as_disabled.rb', line 60

def aad_column_type
  aad_config[:column_type].to_sym
end

#aad_default_scopeObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/act_as_disabled.rb', line 12

def aad_default_scope
  scope = all
  if string_type_with_deleted_value?
    scope.where(aad_column => nil).or(scope.where.not(aad_column => aad_config[:deleted_value]))
  elsif boolean_type_not_nullable?
    scope.where(aad_column => false)
  else
    scope.where(aad_column => nil)
  end
end

#boolean_type_not_nullable?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/act_as_disabled.rb', line 53

def boolean_type_not_nullable?
  aad_column_type == 'boolean'.to_sym && !aad_config[:allow_nulls]
end

#delete_now_valueObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/act_as_disabled.rb', line 68

def delete_now_value
  case aad_config[:column_type]
  when "time" then Time.now
  when "boolean" then true
  when "string" then aad_config[:deleted_value]
  else
    raise ArgumentError, "'time', 'boolean' or 'string' expected" \
    " for :column_type option, got #{aad_config[:column_type]}"
  end
end

#only_disabledObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/act_as_disabled.rb', line 38

def only_disabled
  if string_type_with_deleted_value?
    without_aad_default_scope
        .where(aad_column => aad_config[:deleted_value])
  elsif boolean_type_not_nullable?
    without_aad_default_scope.where(aad_column => true)
  else
    without_aad_default_scope.where.not(aad_column => nil)
  end
end

#string_type_with_deleted_value?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/act_as_disabled.rb', line 49

def string_type_with_deleted_value?
  aad_column_type == 'string'.to_sym && !aad_config[:deleted_value].nil?
end

#with_disabledObject

remove remove every thing from the query select * from models



34
35
36
# File 'lib/act_as_disabled.rb', line 34

def with_disabled
  without_aad_default_scope
end

#without_aad_default_scopeObject



23
24
25
26
27
28
29
# File 'lib/act_as_disabled.rb', line 23

def without_aad_default_scope
  scope = all
  scope = scope.unscope(where: aad_column)
  # Fix problems with unscope group chain
  scope = scope.unscoped if scope.to_sql.include? aad_default_scope.to_sql
  scope
end