Module: ActiveRecord::DeprecatedFinders

Included in:
Base
Defined in:
lib/active_record/deprecated_finders/base.rb,
lib/active_record/deprecated_finders/version.rb

Defined Under Namespace

Classes: ScopeWrapper

Constant Summary collapse

VERSION =
"1.0.4"

Instance Method Summary collapse

Instance Method Details

#all(options = nil) ⇒ Object



66
67
68
# File 'lib/active_record/deprecated_finders/base.rb', line 66

def all(options = nil)
  options ? super().all(options) : super()
end

#default_scope(scope = {}, &block) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/active_record/deprecated_finders/base.rb', line 53

def default_scope(scope = {}, &block)
  if block_given?
    super ScopeWrapper.new(self, block), &nil
  else
    super ScopeWrapper.wrap(self, scope)
  end
end

#scope(name, body = {}, &block) ⇒ Object



70
71
72
# File 'lib/active_record/deprecated_finders/base.rb', line 70

def scope(name, body = {}, &block)
  super(name, ScopeWrapper.wrap(self, body), &block)
end

#scoped(options = nil) ⇒ Object



61
62
63
64
# File 'lib/active_record/deprecated_finders/base.rb', line 61

def scoped(options = nil)
  ActiveSupport::Deprecation.warn("Model.scoped is deprecated. Please use Model.all instead.")
  options ? all.apply_finder_options(options, true) : all
end

#with_scope(scope = {}, action = :merge) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
116
117
118
119
# File 'lib/active_record/deprecated_finders/base.rb', line 74

def with_scope(scope = {}, action = :merge)
  ActiveSupport::Deprecation.warn(
    "ActiveRecord::Base#with_scope and #with_exclusive_scope are deprecated. " \
    "Please use ActiveRecord::Relation#scoping instead. (You can use #merge " \
    "to merge multiple scopes together.)"
  )

  # If another Active Record class has been passed in, get its current scope
  scope = scope.current_scope if !scope.is_a?(Relation) && scope.respond_to?(:current_scope)

  previous_scope = self.current_scope

  if scope.is_a?(Hash)
    # Dup first and second level of hash (method and params).
    scope = scope.dup
    scope.each do |method, params|
      scope[method] = params.dup unless params == true
    end

    scope.assert_valid_keys([ :find, :create ])
    relation = construct_finder_arel(scope[:find] || {})

    if relation.respond_to?(:default_scoped=)
      relation.default_scoped = true unless action == :overwrite
    end

    if previous_scope && previous_scope.create_with_value && scope[:create]
      scope_for_create = if action == :merge
        previous_scope.create_with_value.merge(scope[:create])
      else
        scope[:create]
      end

      relation = relation.create_with(scope_for_create)
    else
      scope_for_create = scope[:create]
      scope_for_create ||= previous_scope.create_with_value if previous_scope
      relation = relation.create_with(scope_for_create) if scope_for_create
    end

    scope = relation
  end

  scope = previous_scope.merge(scope) if previous_scope && action == :merge
  scope.scoping { yield }
end