Skip to content

Commit

Permalink
Implement methods for Algolia Model.search('')
Browse files Browse the repository at this point in the history
  • Loading branch information
gburgett committed Nov 29, 2018
1 parent 85bddec commit 1300597
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
19 changes: 17 additions & 2 deletions wcc-contentful/lib/wcc/contentful/active_record_shim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ def attributes

class_methods do
def model_name
name
WCC::Contentful::Helpers.constant_from_content_type(content_type)
end

def const_get(name)
# Because our pattern is `class MyModel < WCC::Contentful::Model::MyModel`
# if you do MyModel.const_get('MyModel') Algolia expects you to return
# ::MyModel not WCC::Contentful::Model::MyModel
return self if name == model_name

super
end

def table_name
name.tableize
model_name.tableize
end

def unscoped
Expand All @@ -33,5 +42,11 @@ def find_in_batches(options, &block)

find_all(filter).each_slice(batch_size, &block)
end

def where(**conditions)
# TODO: return a Query object that implements more of the ActiveRecord query interface
# https://guides.rubyonrails.org/active_record_querying.html#conditions
find_all(conditions)
end
end
end
35 changes: 35 additions & 0 deletions wcc-contentful/spec/wcc/contentful/active_record_shim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

class ShimTest
include WCC::Contentful::ActiveRecordShim

def self.content_type
'shim-test'
end
end

RSpec.describe WCC::Contentful::ActiveRecordShim do
Expand Down Expand Up @@ -47,4 +51,35 @@ class ShimTest
expect(find_all).to have_received(:each_slice).with(50, &block)
end
end

describe '.where' do
it 'calls `find_all` with filters' do
find_all = spy
expect(ShimTest).to receive(:find_all)
.with(id: %w[1 2 3])
.and_return(find_all)

result = ShimTest.where(id: %w[1 2 3])

expect(result).to eq(find_all)
end
end

describe '.const_get' do
class ShimTest::ConstGetTest
def self.content_type
'const-get-test'
end
end

class ConstGetTest < ShimTest::ConstGetTest
include WCC::Contentful::ActiveRecordShim
end

it 'loads the top-level constant instead of the superclass' do
got = ConstGetTest.const_get('ConstGetTest')

expect(got).to eq(ConstGetTest)
end
end
end

0 comments on commit 1300597

Please sign in to comment.