module EdgeRider::Util

Public Instance Methods

active_record_version() click to toggle source
# File lib/edge_rider/util.rb, line 59
def active_record_version
  ActiveRecord::VERSION::MAJOR
end
define_association(owner, association, target, options) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/edge_rider/util.rb, line 23
def exclusive_query(model, conditions)
  model.unscoped.where(conditions)
end
qualify_column_name(model, column_name) click to toggle source
# 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_path() click to toggle source
# File lib/edge_rider/util.rb, line 80
def rspec_path
  if rspec_version == 1
    'spec'
  else
    'rspec'
  end
end
rspec_root() click to toggle source
# File lib/edge_rider/util.rb, line 72
def rspec_root
  if rspec_version == 1
    Spec
  else
    RSpec
  end
end
rspec_version() click to toggle source
# 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) click to toggle source
# File lib/edge_rider/util.rb, line 27
def scope?(object)
  object.respond_to?(:scoped)
end