Allow plugin patches to reload in development mode

This commit is contained in:
Robin Ward 2017-08-09 12:28:32 -04:00
parent 5ba3a2bd9d
commit f11253dcb6
1 changed files with 50 additions and 35 deletions

View File

@ -74,17 +74,18 @@ class Plugin::Instance
delegate :name, to: :metadata delegate :name, to: :metadata
def add_to_serializer(serializer, attr, define_include_method = true, &block) def add_to_serializer(serializer, attr, define_include_method = true, &block)
klass = "#{serializer.to_s.classify}Serializer".constantize rescue "#{serializer.to_s}Serializer".constantize reloadable_patch(self) do |plugin|
klass = "#{serializer.to_s.classify}Serializer".constantize rescue "#{serializer.to_s}Serializer".constantize
klass.attributes(attr) unless attr.to_s.start_with?("include_") klass.attributes(attr) unless attr.to_s.start_with?("include_")
klass.send(:define_method, attr, &block) klass.send(:define_method, attr, &block)
return unless define_include_method unless define_include_method
# Don't include serialized methods if the plugin is disabled
# Don't include serialized methods if the plugin is disabled klass.send(:define_method, "include_#{attr}?") { plugin.enabled? }
plugin = self end
klass.send(:define_method, "include_#{attr}?") { plugin.enabled? } end
end end
def whitelist_staff_user_custom_field(field) def whitelist_staff_user_custom_field(field)
@ -93,46 +94,48 @@ class Plugin::Instance
# Extend a class but check that the plugin is enabled # Extend a class but check that the plugin is enabled
# for class methods use `add_class_method` # for class methods use `add_class_method`
def add_to_class(klass, attr, &block) def add_to_class(class_name, attr, &block)
klass = klass.to_s.classify.constantize rescue klass.to_s.constantize reloadable_patch(self) do |plugin|
klass = class_name.to_s.classify.constantize rescue class_name.to_s.constantize
hidden_method_name = :"#{attr}_without_enable_check"
klass.send(:define_method, hidden_method_name, &block)
hidden_method_name = :"#{attr}_without_enable_check" klass.send(:define_method, attr) do |*args|
klass.send(:define_method, hidden_method_name, &block) send(hidden_method_name, *args) if plugin.enabled?
end
plugin = self
klass.send(:define_method, attr) do |*args|
send(hidden_method_name, *args) if plugin.enabled?
end end
end end
# Adds a class method to a class, respecting if plugin is enabled # Adds a class method to a class, respecting if plugin is enabled
def add_class_method(klass, attr, &block) def add_class_method(klass_name, attr, &block)
klass = klass.to_s.classify.constantize rescue klass.to_s.constantize reloadable_patch(self) do |plugin|
klass = klass_name.to_s.classify.constantize rescue klass_name.to_s.constantize
hidden_method_name = :"#{attr}_without_enable_check" hidden_method_name = :"#{attr}_without_enable_check"
klass.send(:define_singleton_method, hidden_method_name, &block) klass.send(:define_singleton_method, hidden_method_name, &block)
plugin = self klass.send(:define_singleton_method, attr) do |*args|
klass.send(:define_singleton_method, attr) do |*args| send(hidden_method_name, *args) if plugin.enabled?
send(hidden_method_name, *args) if plugin.enabled? end
end end
end end
def add_model_callback(klass, callback, options = {}, &block) def add_model_callback(klass_name, callback, options = {}, &block)
klass = klass.to_s.classify.constantize rescue klass.to_s.constantize reloadable_patch(self) do |plugin|
plugin = self klass = klass_name.to_s.classify.constantize rescue klass_name.to_s.constantize
# generate a unique method name # generate a unique method name
method_name = "#{plugin.name}_#{klass.name}_#{callback}#{@idx}".underscore method_name = "#{plugin.name}_#{klass.name}_#{callback}#{@idx}".underscore
@idx += 1 @idx += 1
hidden_method_name = :"#{method_name}_without_enable_check" hidden_method_name = :"#{method_name}_without_enable_check"
klass.send(:define_method, hidden_method_name, &block) klass.send(:define_method, hidden_method_name, &block)
klass.send(callback, options) do |*args| klass.send(callback, options) do |*args|
send(hidden_method_name, *args) if plugin.enabled? send(hidden_method_name, *args) if plugin.enabled?
end
hidden_method_name
end end
hidden_method_name
end end
# Add validation method but check that the plugin is enabled # Add validation method but check that the plugin is enabled
@ -435,4 +438,16 @@ JS
end end
end end
def reloadable_patch(plugin)
if Rails.env.development? && defined?(ActionDispatch::Reloader)
ActionDispatch::Reloader.to_prepare do
yield plugin
end
end
# apply the patch
yield plugin
end
end end