Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New option for Modis::Model - :enable_all_index #7

Merged
merged 2 commits into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/modis/finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def find(*ids)
end

def all
unless all_index_enabled?
raise IndexError, "Unable to retrieve all records of #{name}, "\
"because you disabled all index. See :enable_all_index for more."
end

records = Modis.with_connection do |redis|
ids = redis.smembers(key_for(:all))
redis.pipelined do
Expand Down
20 changes: 16 additions & 4 deletions lib/modis/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def sti_base_key_for(id)
"#{sti_base_absolute_namespace}:#{id}"
end

def enable_all_index(bool)
@use_all_index = bool
end

def all_index_enabled?
@use_all_index == true || @use_all_index.nil?
end

def create(attrs)
model = new(attrs)
model.save
Expand Down Expand Up @@ -128,8 +136,10 @@ def destroy
run_callbacks :destroy do
redis.pipelined do
remove_from_indexes(redis)
redis.srem(self.class.key_for(:all), id)
redis.srem(self.class.sti_base_key_for(:all), id) if self.class.sti_child?
if self.class.all_index_enabled?
redis.srem(self.class.key_for(:all), id)
redis.srem(self.class.sti_base_key_for(:all), id) if self.class.sti_child?
end
redis.del(key)
end
end
Expand Down Expand Up @@ -198,8 +208,10 @@ def persist
future = attrs.any? ? redis.hmset(key, attrs) : :unchanged

if new_record?
redis.sadd(self.class.key_for(:all), id)
redis.sadd(self.class.sti_base_key_for(:all), id) if self.class.sti_child?
if self.class.all_index_enabled?
redis.sadd(self.class.key_for(:all), id)
redis.sadd(self.class.sti_base_key_for(:all), id) if self.class.sti_child?
end
add_to_indexes(redis)
else
update_indexes(redis)
Expand Down
10 changes: 10 additions & 0 deletions spec/finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class Producer < User

class Worker < Producer
end

class UserNoAllIndex < User
enable_all_index false
end
end

describe Modis::Finder do
Expand Down Expand Up @@ -67,6 +71,12 @@ class Worker < Producer
model.destroy
expect(FindersSpec::User.all).to eq([])
end

it 'throws error when enable_all_index option is set to false' do
FindersSpec::UserNoAllIndex.create!(name: 'Yana')
expect { FindersSpec::UserNoAllIndex.all }
.to raise_error(Modis::IndexError)
end
end

it 'identifies a found record as not being new' do
Expand Down
22 changes: 22 additions & 0 deletions spec/persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def test_before_save
called_callbacks << :test_before_save
end
end

class MockModelNoAllIndex < MockModel
enable_all_index false
end
end

describe Modis::Persistence do
Expand Down Expand Up @@ -296,4 +300,22 @@ def test_before_save
expect(model.update_attributes(name: nil)).to be false
end
end

describe 'key for all records' do
let(:all_key_name) { "#{PersistenceSpec::MockModel.absolute_namespace}:all" }

describe 'when :enable_all_index option is set to false' do
it 'does not save new record to the *:all key' do
model = PersistenceSpec::MockModel.create!(name: 'Sage')
expect(Redis.new.smembers(all_key_name).map(&:to_i)).to include(model.id)
end
end

describe 'when :enable_all_index option is set to false' do
it 'does not save new record to the *:all key' do
model = PersistenceSpec::MockModelNoAllIndex.create!(name: 'Alex')
expect(Redis.new.smembers(all_key_name).map(&:to_i)).to_not include(model.id)
end
end
end
end