Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/annotate/overrides.rb

Class Method Summary collapse

Class Method Details

.orig_scopeObject



7
# File 'lib/rails/annotate/overrides.rb', line 7

alias orig_scope scope

.scope(*args, **kwargs, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails/annotate/overrides.rb', line 9

def scope(*args, **kwargs, &block)
  file_path, scope_line_number = caller.first.split(':')
  scope_line_number = scope_line_number.to_i
  scope_name = args.first
  scope_proc = args[1]
  proc_parameters = scope_proc.respond_to?(:parameters) ? scope_proc.parameters.map(&:last) : []
  scope_definition = ::String.new
  scope_model_class = self
  scope_lines = 0
  scope_indentation = nil

  ::File.open(file_path) do |file|
    file.each_line.with_index(1) do |line, current_line_number|
      next if current_line_number < scope_line_number
      break if scope_lines > 50

      scope_indentation ||= line.length - line.lstrip.length
      scope_definition << "#{line.rstrip[scope_indentation..]}\n"
      scope_lines += 1

      break if ::Parser::CurrentRuby.new.parse ::Parser::Source::Buffer.new('(string)', source: scope_definition)
    end
  end

  ::Rails::Annotate::Solargraph::Model.add_scope(scope_name.to_sym, scope_model_class, proc_parameters, scope_definition)

  orig_scope(*args, **kwargs, &block)
rescue ::StandardError
  orig_scope(*args, **kwargs, &block)
end