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,46 +211,62 @@ module HasGroups
end end
def groups_access_map_store(map) def groups_access_map_store(map)
map.each do |group_identifier, accesses| fill_group_access_buffer do
# use given key as identifier or look it up map.each do |group_identifier, accesses|
# via the given block which returns the identifier # use given key as identifier or look it up
group_id = block_given? ? yield(group_identifier) : group_identifier # via the given block which returns the identifier
group_id = block_given? ? yield(group_identifier) : group_identifier
if !accesses.is_a?(Array) if !accesses.is_a?(Array)
accesses = [accesses] accesses = [accesses]
end end
accesses.each do |access| accesses.each do |access|
push_group_access_buffer( push_group_access_buffer(
group_id: group_id, group_id: group_id,
access: access access: access
) )
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 check_group_access_buffer def flushed_group_access_buffer
return if group_access_buffer.blank? # group_access_buffer is at least an empty Array
destroy_group_relations # 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
foreign_key = group_through.foreign_key def check_group_access_buffer
entries = group_access_buffer.collect do |entry|
entry[foreign_key] = id flushed_group_access_buffer do
entry destroy_group_relations
break if group_access_buffer.blank?
foreign_key = group_through.foreign_key
entries = group_access_buffer.collect do |entry|
entry[foreign_key] = id
entry
end
group_through.klass.create!(entries)
end end
group_through.klass.create!(entries)
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