Module: EdgeRider::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/edge_rider/util.rb

Instance Method Summary collapse

Instance Method Details

#active_record_versionObject



59
60
61
# File 'lib/edge_rider/util.rb', line 59

def active_record_version
  ActiveRecord::VERSION::MAJOR
end

#define_association(owner, association, target, options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/edge_rider/util.rb', line 46

def define_association(owner, association, target, options)
  if active_record_version < 4
    owner.send association, target, options
  else
    # Reduce the options hash to the given keys and store the remainder in
    # other_options. Using Hash#extract! would be easier to unterstand,
    # but Rails 2 does not have it.
    other_options = options.slice!(:conditions)
    scope = lambda { |*args| scoped(options) }
    owner.send association, target, scope, **other_options
  end
end

#define_scope(klass, name, lambda) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/edge_rider/util.rb', line 31

def define_scope(klass, name, lambda)
  if ActiveRecord::VERSION::MAJOR == 3
    klass.send :scope, name, lambda
  else
    klass.send :scope, name, lambda { |*args|
      options = lambda.call(*args)
      if ActiveRecord::VERSION::MAJOR < 6
        klass.scoped(options.slice :conditions)
      else
        scoped(options.slice :conditions)
      end
    }
  end      
end

#exclusive_query(model, conditions) ⇒ Object



23
24
25
# File 'lib/edge_rider/util.rb', line 23

def exclusive_query(model, conditions)
  model.unscoped.where(conditions)
end

#qualify_column_name(model, column_name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/edge_rider/util.rb', line 7

def qualify_column_name(model, column_name)
  column_name = column_name.to_s
  unless column_name.include?('.')
    column_name = if ActiveRecord::VERSION::MAJOR < 4
      quoted_table_name = model.connection.quote_table_name(model.table_name)
      quoted_column_name = model.connection.quote_column_name(column_name)
      "#{quoted_table_name}.#{quoted_column_name}"
    else
      # Rails 4+ will quote correctly and Rails 5.2 will print
      # deprecation warnings if there are surplus quotes
      "#{model.table_name}.#{column_name}"
    end
  end
  column_name
end

#rspec_pathObject



80
81
82
83
84
85
86
# File 'lib/edge_rider/util.rb', line 80

def rspec_path
  if rspec_version == 1
    'spec'
  else
    'rspec'
  end
end

#rspec_rootObject



72
73
74
75
76
77
78
# File 'lib/edge_rider/util.rb', line 72

def rspec_root
  if rspec_version == 1
    Spec
  else
    RSpec
  end
end

#rspec_versionObject



63
64
65
66
67
68
69
70
# File 'lib/edge_rider/util.rb', line 63

def rspec_version
  if defined?(Spec)
    1
  else
    require 'rspec/version'
    RSpec::Version::STRING.to_i
  end
end

#scope?(object) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/edge_rider/util.rb', line 27

def scope?(object)
  object.respond_to?(:scoped)
end