Skip to content

Commit

Permalink
remove arel-extensions dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj committed Aug 10, 2022
1 parent 87a9968 commit 0a30872
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 65 deletions.
3 changes: 0 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ PATH
specs:
motor-admin (0.3.5)
ar_lazy_preload (~> 1.0)
arel-extensions (>= 6.0)
audited (~> 5.0)
cancancan (~> 3.0)
fugit (~> 1.0)
Expand Down Expand Up @@ -84,8 +83,6 @@ GEM
public_suffix (>= 2.0.2, < 5.0)
ar_lazy_preload (1.0.0)
rails (>= 5.2)
arel-extensions (6.1.0)
activerecord (>= 6.1.0)
ast (2.4.2)
audited (5.0.2)
activerecord (>= 5.0, < 7.1)
Expand Down
1 change: 0 additions & 1 deletion lib/motor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
require 'uri'
require 'net/http'
require 'net/https'
require 'arel/extensions'

module Motor
PATH = Pathname.new(__dir__)
Expand Down
120 changes: 60 additions & 60 deletions lib/motor/active_record_utils/active_record_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,44 @@ class UnkownFilterError < NoMethodError
end
end

ActiveRecord::QueryMethods.module_eval do
def build_arel_with_distinct_on(aliases = nil)
arel = build_arel_without_distinct_on(aliases)
arel.distinct_on(distinct_on_values) unless distinct_on_values.empty?
arel
module Arel
module Attributes
class Relation < Attribute
attr_accessor :collection, :for_write

def initialize(relation, name, collection = false, for_write = false)
self[:relation] = relation
self[:name] = name
@collection = collection
@for_write = for_write
end

delegate :able_to_type_cast?, to: :relation

def table_name
nil
end

def eql?(other)
self.class == other.class &&
relation == other.relation &&
name == other.name &&
collection == other.collection
end

delegate :type_cast_for_database, to: :relation
end
end
end

alias_method :build_arel, :build_arel_with_distinct_on
module Arel
module Visitors
class ToSql
def visit_Arel_Attributes_Relation(o, collector)
visit(o.relation, collector)
end
end
end
end

module ActiveRecord
Expand Down Expand Up @@ -64,29 +94,30 @@ def self.build_filter_joins(klass, filters, relations = [], custom = [])
end
elsif reflection = klass._reflections[key.to_s]
if value.is_a?(Hash)
relations << if reflection.polymorphic?
value = value.dup
join_klass = value.delete(:as).safe_constantize
right_table = join_klass.arel_table
left_table = reflection.active_record.arel_table

on = right_table[join_klass.primary_key]
.eq(left_table[reflection.foreign_key])
.and(left_table[reflection.foreign_type].eq(join_klass.name))

cross_boundry_joins = join_klass.left_outer_joins(ActiveRecord::PredicateBuilder.filter_joins(join_klass, value).flatten).send(
:build_joins, []
)

[
left_table.join(right_table, Arel::Nodes::OuterJoin).on(on).join_sources,
cross_boundry_joins
]
else
{
key => build_filter_joins(reflection.klass, value, [], custom)
}
end
relations <<
if reflection.polymorphic?
value = value.dup
join_klass = value.delete(:as).safe_constantize
right_table = join_klass.arel_table
left_table = reflection.active_record.arel_table

on = right_table[join_klass.primary_key]
.eq(left_table[reflection.foreign_key])
.and(left_table[reflection.foreign_type].eq(join_klass.name))

cross_boundry_joins = join_klass.left_outer_joins(ActiveRecord::PredicateBuilder.filter_joins(join_klass, value).flatten).send(
:build_joins, []
)

[
left_table.join(right_table, Arel::Nodes::OuterJoin).on(on).join_sources,
cross_boundry_joins
]
else
{
key => build_filter_joins(reflection.klass, value, [], custom)
}
end
elsif value.is_a?(Array)
value.each do |v|
relations << {
Expand Down Expand Up @@ -233,37 +264,6 @@ def expand_filter_for_arel_attribute(column, attribute, key, value)
attribute.has_any_key(*Array(value).map { |x| Arel::Nodes.build_quoted(x) })
when :in
attribute.in(value)
when :intersects
# geometry_value = if value.is_a?(Hash) # GeoJSON
# Arel::Nodes::NamedFunction.new('ST_GeomFromGeoJSON', [JSON.generate(value)])
# elsif # EWKB
# elsif # WKB
# elsif # EWKT
# elsif # WKT
# end

# TODO: us above if to determin if SRID sent
geometry_value = if value.is_a?(Hash)
Arel::Nodes::NamedFunction.new('ST_SetSRID',
[
Arel::Nodes::NamedFunction.new('ST_GeomFromGeoJSON',
[Arel::Nodes.build_quoted(JSON.generate(subvalue))]), 4326
])
elsif value[0, 1] == "\x00" || value[0, 1] == "\x01" || value[0, 4] =~ /[0-9a-fA-F]{4}/
Arel::Nodes::NamedFunction.new('ST_SetSRID',
[
Arel::Nodes::NamedFunction.new('ST_GeomFromEWKB',
[Arel::Nodes.build_quoted(subvalue)]), 4326
])
else
Arel::Nodes::NamedFunction.new('ST_SetSRID',
[
Arel::Nodes::NamedFunction.new('ST_GeomFromText',
[Arel::Nodes.build_quoted(subvalue)]), 4326
])
end

Arel::Nodes::NamedFunction.new('ST_Intersects', [attribute, geometry_value])
when :less_than, :lt
attribute.lt(value)
when :less_than_or_equal_to, :lteq, :lte
Expand Down
1 change: 0 additions & 1 deletion motor-admin.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = '>= 2.6'

spec.add_dependency 'arel-extensions', '>= 6.0'
spec.add_dependency 'ar_lazy_preload', '~> 1.0'
spec.add_dependency 'audited', '~> 5.0'
spec.add_dependency 'cancancan', '~> 3.0'
Expand Down

0 comments on commit 0a30872

Please sign in to comment.