Class: Contrast::Framework::Rails::Rewrite::ActiveRecordNamed Deprecated

Inherits:
Object
  • Object
show all
Includes:
Components::Interface
Defined in:
lib/contrast/framework/rails/rewrite/active_record_named.rb,
ext/cs__assess_active_record_named/cs__active_record_named.c

Overview

Deprecated.

Changes to this class are discouraged as this approach is being phased out with support for those language versions.

Our patch into the ActiveRecord::Scoping::Named::ClassMethods Module, allowing for the runtime rewrite of interpolation calls defined in methods defined dynamically during application execution.

TODO: RUBY-714 remove w/ EOL of 2.5

Class Method Summary collapse

Methods included from Components::Interface

included

Class Method Details

.instrumentObject



59
60
61
62
63
64
65
66
67
# File 'lib/contrast/framework/rails/rewrite/active_record_named.rb', line 59

def instrument
  @_instrument_named_track ||= begin
    require 'cs__assess_active_record_named/cs__assess_active_record_named'
    true
  end
rescue StandardError, LoadError => e
  logger.error('Error loading active record named track patch', e)
  false
end

.location_available?(location) ⇒ Boolean

Good news, once we patch the body once, the source location becomes eval. We may need to fix this later though (so it may be bad news)

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/contrast/framework/rails/rewrite/active_record_named.rb', line 52

def location_available? location
  return false if location.nil?
  return false if location.empty? || location[0].empty? || location[0].include?('eval')

  true
end

.rewrite(mod, method_name, body) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/contrast/framework/rails/rewrite/active_record_named.rb', line 22

def rewrite mod, method_name, body
  return body unless AGENT.rewrite_interpolation?
  return body unless body.is_a?(Proc)

  location = body.source_location
  return body unless location_available?(location)

  opener = Contrast::Agent::ClassReopener.new(Contrast::Agent::ModuleData.new(mod))
  original_source_code = opener.source_code(location, method_name)
  return body unless original_source_code
  return body if Contrast::Agent::Rewriter.send(:unrepeatable?, original_source_code)
  return body unless Contrast::Agent::Rewriter.send(:interpolations?, original_source_code)

  # the code looks like 'source :some_method_name, ->lambda_literal'
  # we just need the lambda
  body_start = original_source_code.index(',') + 1
  original_source_code = original_source_code[body_start..-1]

  new_method_source = Contrast::Agent::Rewriter.send(:rewrite_method, original_source_code)
  return body unless Contrast::Agent::Rewriter.send(:valid_code?, new_method_source)

  unbound_eval(cs__name, new_method_source)
rescue SyntaxError, StandardError => e
  logger.debug('Can\'t parse method source in scoped method', e, method: method_name)
  body
end