This commit is contained in:
Thorsten Eckel 2018-02-19 20:48:09 +01:00
parent a1391a5cd3
commit 0f28d1f0b6
3 changed files with 112 additions and 27 deletions

View file

@ -23,7 +23,6 @@ returns
group_ids: :group_ids_access_map= group_ids: :group_ids_access_map=
}.each do |param, setter| }.each do |param, setter|
map = params[param] map = params[param]
next if map.blank?
next if !respond_to?(setter) next if !respond_to?(setter)
send(setter, map) send(setter, map)
end end

View file

@ -211,6 +211,7 @@ module HasGroups
end end
def groups_access_map_store(map) def groups_access_map_store(map)
fill_group_access_buffer do
map.each do |group_identifier, accesses| map.each do |group_identifier, accesses|
# use given key as identifier or look it up # use given key as identifier or look it up
# via the given block which returns the identifier # via the given block which returns the identifier
@ -227,19 +228,36 @@ module HasGroups
) )
end end
end end
end
end
def fill_group_access_buffer
@group_access_buffer = []
yield
check_group_access_buffer if id check_group_access_buffer if id
end end
def push_group_access_buffer(entry) def push_group_access_buffer(entry)
@group_access_buffer ||= []
@group_access_buffer.push(entry) @group_access_buffer.push(entry)
end end
def flushed_group_access_buffer
# group_access_buffer is at least an empty Array
# if changes to the map were performed
# otherwise it's just an update of other attributes
return if group_access_buffer.nil?
yield
group_access_buffer = nil
cache_delete
end
def check_group_access_buffer def check_group_access_buffer
return if group_access_buffer.blank?
flushed_group_access_buffer do
destroy_group_relations destroy_group_relations
break if group_access_buffer.blank?
foreign_key = group_through.foreign_key foreign_key = group_through.foreign_key
entries = group_access_buffer.collect do |entry| entries = group_access_buffer.collect do |entry|
entry[foreign_key] = id entry[foreign_key] = id
@ -247,10 +265,8 @@ module HasGroups
end end
group_through.klass.create!(entries) group_through.klass.create!(entries)
end
group_access_buffer = nil
cache_delete
true true
end end

View file

@ -229,6 +229,19 @@ RSpec.shared_examples 'HasGroups' do
described_class.group_through.klass.count described_class.group_through.klass.count
}.by(3) }.by(3)
end end
it 'allows empty Hash value' do
group_access_instance.group_names_access_map = {
group_full.name => 'full',
group_read.name => %w[read change],
}
expect do
group_access_instance.group_names_access_map = {}
end.to change {
described_class.group_through.klass.count
}.by(-3)
end
end end
context 'new instance' do context 'new instance' do
@ -256,6 +269,16 @@ RSpec.shared_examples 'HasGroups' do
described_class.group_through.klass.count described_class.group_through.klass.count
}.by(2) }.by(2)
end end
it 'allows empty Hash value' do
expect do
new_group_access_instance.group_names_access_map = {}
new_group_access_instance.save
end.not_to change {
described_class.group_through.klass.count
}
end
end end
end end
@ -284,6 +307,18 @@ RSpec.shared_examples 'HasGroups' do
expect(group_access_instance_inactive.group_names_access_map).to be_empty expect(group_access_instance_inactive.group_names_access_map).to be_empty
end end
it 'returns empty map if none is stored' do
group_access_instance.group_names_access_map = {
group_full.name => 'full',
group_read.name => 'read',
}
group_access_instance.group_names_access_map = {}
expect(group_access_instance.group_names_access_map).to be_blank
end
end end
context '#group_ids_access_map=' do context '#group_ids_access_map=' do
@ -305,7 +340,7 @@ RSpec.shared_examples 'HasGroups' do
}.by(2) }.by(2)
end end
it 'stores Hash with String values' do it 'stores Hash with Array<String> values' do
expect do expect do
group_access_instance.group_ids_access_map = { group_access_instance.group_ids_access_map = {
group_full.id => 'full', group_full.id => 'full',
@ -315,6 +350,19 @@ RSpec.shared_examples 'HasGroups' do
described_class.group_through.klass.count described_class.group_through.klass.count
}.by(3) }.by(3)
end end
it 'allows empty Hash value' do
group_access_instance.group_ids_access_map = {
group_full.id => 'full',
group_read.id => %w[read change],
}
expect do
group_access_instance.group_ids_access_map = {}
end.to change {
described_class.group_through.klass.count
}.by(-3)
end
end end
context 'new instance' do context 'new instance' do
@ -342,6 +390,16 @@ RSpec.shared_examples 'HasGroups' do
described_class.group_through.klass.count described_class.group_through.klass.count
}.by(2) }.by(2)
end end
it 'allows empty Hash value' do
expect do
new_group_access_instance.group_ids_access_map = {}
new_group_access_instance.save
end.not_to change {
described_class.group_through.klass.count
}
end
end end
end end
@ -370,6 +428,18 @@ RSpec.shared_examples 'HasGroups' do
expect(group_access_instance_inactive.group_ids_access_map).to be_empty expect(group_access_instance_inactive.group_ids_access_map).to be_empty
end end
it 'returns empty map if none is stored' do
group_access_instance.group_ids_access_map = {
group_full.id => 'full',
group_read.id => 'read',
}
group_access_instance.group_ids_access_map = {}
expect(group_access_instance.group_ids_access_map).to be_blank
end
end end
context '#associations_from_param' do context '#associations_from_param' do