Skip to content

Commit

Permalink
Merge branch 'master' into nql_array_node
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Naiman committed Dec 16, 2020
2 parents fc6f43d + 2a1ee8f commit 2ed4e78
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
26 changes: 11 additions & 15 deletions lib/rasti/db/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ def all_graph_attributes(*relations)
build_query relations_graph: relations_graph.with_all_attributes_for(relations)
end

def append_computed_attribute(name)
computed_attribute = collection_class.computed_attributes[name]
ds = computed_attribute.apply_join(dataset).select_append(computed_attribute.identifier.as(name))
def select_computed_attributes(*computed_attributes)
ds = computed_attributes.inject(dataset) do |ds, name|
computed_attribute = collection_class.computed_attributes[name]
computed_attribute.apply_join(ds).select_append(computed_attribute.identifier.as(name))
end
build_query dataset: ds
end

def all
with_graph(dataset.all).map do |row|
with_graph(dataset.all).map do |row|
collection_class.model.new row
end
end
Expand All @@ -74,13 +76,15 @@ def each(batch_size:nil, &block)
if batch_size.nil?
all.each(&block)
else
each_model_in_batches(size: batch_size, &block)
each_batch(size: batch_size) do |models|
models.each { |model| block.call model }
end
end
end

def each_batch(size:, &block)
dataset.each_page(size) do |page|
query = build_query dataset: page
primary_keys.each_slice(size) do |pks|
query = where(collection_class.primary_key => pks)
block.call query.all
end
end
Expand Down Expand Up @@ -175,14 +179,6 @@ def chainable(&block)
build_query dataset: instance_eval(&block)
end

def each_model_in_batches(size:, &block)
dataset.each_page(size) do |page|
page.each do |row|
block.call build_model(row)
end
end
end

def with_related(relation_name, primary_keys)
ds = collection_class.relations[relation_name].apply_filter environment, dataset, primary_keys
build_query dataset: ds
Expand Down
2 changes: 1 addition & 1 deletion lib/rasti/db/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Rasti
module DB
VERSION = '2.1.0'
VERSION = '2.2.0'
end
end
2 changes: 0 additions & 2 deletions spec/minitest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ class Minitest::Spec
let :db do
Sequel.connect(driver).tap do |db|

db.extension :pagination

db.create_table :users do
primary_key :id
String :name, null: false, unique: true
Expand Down
12 changes: 6 additions & 6 deletions spec/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@
.must_equal [post]
end

describe 'Append computed attribute' do
describe 'Select computed attributes' do
it 'With join' do
db[:comments].insert post_id: 1, user_id: 5, text: 'Comment 4'
users_query.append_computed_attribute(:comments_count)
.where(id: 5)
.all
.must_equal [User.new(id: 5, name: 'User 5', comments_count: 2)]
users_query.select_computed_attributes(:comments_count)
.where(id: 5)
.all
.must_equal [User.new(id: 5, name: 'User 5', comments_count: 2)]
end

it 'Without join' do
Expand All @@ -159,7 +159,7 @@
birth_date: Date.parse('2020-04-24'),
full_name: 'Name 1 Last Name 1'

people_query.append_computed_attribute(:full_name)
people_query.select_computed_attributes(:full_name)
.where(document_number: 'document_1')
.all
.must_equal [person_expected]
Expand Down

0 comments on commit 2ed4e78

Please sign in to comment.