Class: ActiveRecordSpatial::SpatialFunction

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-spatial/spatial_function.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  column: ActiveRecordSpatial.default_column_name,
  use_index: true
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ SpatialFunction

Returns a new instance of SpatialFunction.



10
11
12
# File 'lib/activerecord-spatial/spatial_function.rb', line 10

def initialize(klass)
  @klass = klass
end

Class Method Details

.additional_ordering(*args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/activerecord-spatial/spatial_function.rb', line 76

def additional_ordering(*args)
  options = args.extract_options!

  desc = if args.first == :desc
    true
  else
    options[:desc]
  end

  ''.dup.tap do |ret|
    ret << ' DESC' if desc
    ret << " NULLS #{options[:nulls].to_s.upcase}" if options[:nulls]
  end
end

.build!(klass, *args) ⇒ Object



14
15
16
# File 'lib/activerecord-spatial/spatial_function.rb', line 14

def self.build!(klass, *args)
  new(klass).build_function_call(*args)
end

Instance Method Details

#build_function_call(function, *args) ⇒ Object



18
19
20
21
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/activerecord-spatial/spatial_function.rb', line 18

def build_function_call(function, *args)
  options = default_options(args.extract_options!)

  geom = options.fetch(:geom_arg, args.first)
  args = Array.wrap(options.fetch(:args, args.from(1))).collect do |arg|
    arel_quoted_value(arg)
  end

  column_name = column_name(options[:column])
  first_geom_arg = wrap_column_or_geometry(
    @klass.arel_table[column_name],
    options[:column]
  )
  geom_args = [first_geom_arg]

  if geom.present?
    column_type = @klass.spatial_column_by_name(column_name).spatial_type
    column_srid = @klass.srid_for(column_name)

    if !geom.is_a?(Hash)
      geom_arg = read_geos(geom, column_srid)
      geom_srid = read_geom_srid(geom_arg, column_type)
      geom_args << set_srid_or_transform(column_srid, geom_srid, geom_arg, column_type)
    else
      klass = if geom[:class]
        geom[:class]
      elsif geom[:class_name]
        geom[:class_name].classify.constantize
      else
        raise ArgumentError, 'Need either a :class or :class_name option to determine the class.'
      end

      if geom[:value]
        geom_arg = read_geos(geom[:value], column_srid)
        geom_srid = read_geom_srid(geom_arg, column_type)
      else
        geom_arg = geom
        geom_srid = klass.srid_for(column_name(geom[:column]))
      end

      transformed_geom = set_srid_or_transform(column_srid, geom_srid, geom_arg, column_type)
      geom_args << wrap_column_or_geometry(transformed_geom, geom)
    end
  end

  geom_args.reverse! if options[:invert] && geom_args.length > 1

  ret = Arel::Nodes::NamedFunction.new(
    function_name(function, options[:use_index]),
    geom_args + args
  )

  ret = ret.or(first_geom_arg.eq(nil)) if options[:allow_null]

  ret
end