destroy query if table widget is removed

pull/7185/head
Jens Ulferts 6 years ago
parent 43eed63cab
commit 1181d1d4ec
No known key found for this signature in database
GPG Key ID: 3CAA4B1182CF5308
  1. 10
      modules/grids/app/models/grids/widget.rb
  2. 26
      modules/grids/lib/grids/configuration.rb
  3. 4
      modules/grids/lib/grids/my_page_grid_registration.rb
  4. 34
      modules/grids/spec/models/grids/my_page_spec.rb

@ -34,5 +34,15 @@ module Grids
belongs_to :grid
serialize :options, Hash
after_destroy :execute_after_destroy_strategy
private
def execute_after_destroy_strategy
proc = Grids::Configuration.widget_strategy(grid, identifier).after_destroy
instance_exec(&proc)
end
end
end

@ -95,6 +95,10 @@ class Grids::Configuration
(grid_classes || []).include?(grid)
end
def widget_strategy(grid, identifier)
grid_register[grid.class.to_s].widget_strategy(identifier)
end
def writable?(grid, user)
grid_register[grid.class.to_s]&.writable?(grid, user)
end
@ -120,6 +124,18 @@ class Grids::Configuration
end
end
class WidgetStrategy
class << self
def after_destroy(proc = nil)
if proc
@after_destroy = proc
end
@after_destroy ||= -> {}
end
end
end
class Registration
class << self
def grid_class(name_string = nil)
@ -146,6 +162,16 @@ class Grids::Configuration
@widgets
end
def widget_strategy(widget_name, &block)
@widget_strategies ||= {}
if block_given?
@widget_strategies[widget_name.to_s] = Class.new(Grids::Configuration::WidgetStrategy, &block)
end
@widget_strategies[widget_name.to_s] ||= Grids::Configuration::WidgetStrategy
end
def defaults(hash = nil)
# This is called during code load, which
# may not have the table available.

@ -13,6 +13,10 @@ module Grids
'documents',
'news'
widget_strategy 'work_packages_table' do
after_destroy -> { ::Query.find_by(id: options[:queryId])&.destroy }
end
defaults(
row_count: 7,
column_count: 4,

@ -31,18 +31,46 @@ require 'spec_helper'
require_relative './shared_model'
describe Grids::MyPage, type: :model do
let(:instance) { described_class.new }
let(:instance) { described_class.new(row_count: 5, column_count: 5) }
let(:user) { FactoryBot.build_stubbed(:user) }
it_behaves_like 'grid attributes'
context 'attributes' do
let(:user) { FactoryBot.build_stubbed :user }
it '#user' do
instance.user = user
expect(instance.user)
.to eql user
end
end
context 'altering widgets' do
context 'when removing a work_packages_table widget' do
let(:user) { FactoryBot.create(:user) }
let(:query) do
FactoryBot.create(:query,
user: user,
hidden: true)
end
before do
widget = Grids::Widget.new(identifier: 'work_packages_table',
start_row: 1,
end_row: 2,
start_column: 1,
end_column: 2,
options: { queryId: query.id })
instance.widgets = [widget]
instance.save!
end
it 'removes the widget\'s query' do
instance.widgets = []
expect(Query.find_by(id: query.id))
.to be_nil
end
end
end
end

Loading…
Cancel
Save