Skip to content

Commit

Permalink
Split out the BabySqueel::ActiveRecord namespace into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Zane committed Sep 2, 2016
1 parent d47f9b7 commit cf681fb
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 99 deletions.
4 changes: 3 additions & 1 deletion lib/baby_squeel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
require 'active_record/relation'
require 'baby_squeel/version'
require 'baby_squeel/errors'
require 'baby_squeel/active_record'
require 'baby_squeel/active_record/base'
require 'baby_squeel/active_record/query_methods'
require 'baby_squeel/active_record/where_chain'

module BabySqueel
class << self
Expand Down
98 changes: 0 additions & 98 deletions lib/baby_squeel/active_record.rb

This file was deleted.

16 changes: 16 additions & 0 deletions lib/baby_squeel/active_record/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'baby_squeel/dsl'

module BabySqueel
module ActiveRecord
module Base
delegate :joining, :joining!, :selecting, :ordering,
:grouping, :when_having, to: :all

def sifter(name, &block)
define_singleton_method "sift_#{name}" do |*args|
DSL.evaluate_sifter(self, *args, &block)
end
end
end
end
end
39 changes: 39 additions & 0 deletions lib/baby_squeel/active_record/joins_values.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module BabySqueel
module ActiveRecord
# This allow us to prevent duplicate nodes from being joined
# Normal association names like :author can be compared to
# BabySqueel::Nodes::InnerJoin and deduped.
class JoinsValues < Array
def +(additions)
values = JoinsValues.new(to_a)
values.add!(additions)
values
end

def add!(additions, &block)
additions.each do |addition|
if index = find_same_index(addition)
# This addition is a duplicate, replace the existing
# item if it isn't a BabySqueel join node
self[index] = addition unless node?(self[index])
else
self << addition
end
end
self
end

private

def find_same_index(a)
find_index do |b|
node?(a) && a.eql?(b) || node?(b) && b.eql?(a)
end
end

def node?(item)
item.respond_to? :same_association?
end
end
end
end
44 changes: 44 additions & 0 deletions lib/baby_squeel/active_record/query_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'baby_squeel/dsl'
require 'baby_squeel/active_record/joins_values'

module BabySqueel
module ActiveRecord
module QueryMethods
# Constructs Arel for ActiveRecord::Base#joins using the DSL.
def joining(&block)
exprs, binds = DSL.evaluate_joins(self, &block)
spawn.joining! exprs, binds
end

def joining!(exprs, binds = [])
unless joins_values.kind_of? JoinsValues
self.joins_values = JoinsValues.new(joins_values)
end

joins! exprs
self.bind_values += binds
self
end

# Constructs Arel for ActiveRecord::Base#select using the DSL.
def selecting(&block)
select DSL.evaluate(self, &block)
end

# Constructs Arel for ActiveRecord::Base#order using the DSL.
def ordering(&block)
order DSL.evaluate(self, &block)
end

# Constructs Arel for ActiveRecord::Base#group using the DSL.
def grouping(&block)
group DSL.evaluate(self, &block)
end

# Constructs Arel for ActiveRecord::Base#having using the DSL.
def when_having(&block)
having DSL.evaluate(self, &block)
end
end
end
end
13 changes: 13 additions & 0 deletions lib/baby_squeel/active_record/where_chain.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'baby_squeel/dsl'

module BabySqueel
module ActiveRecord
module WhereChain
# Constructs Arel for ActiveRecord::Base#where using the DSL.
def has(&block)
@scope.where! DSL.evaluate(@scope, &block)
@scope
end
end
end
end

0 comments on commit cf681fb

Please sign in to comment.