Allow plugin patches to reload in development mode
This commit is contained in:
parent
5ba3a2bd9d
commit
f11253dcb6
|
@ -74,18 +74,19 @@ class Plugin::Instance
|
|||
delegate :name, to: :metadata
|
||||
|
||||
def add_to_serializer(serializer, attr, define_include_method = true, &block)
|
||||
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.send(:define_method, attr, &block)
|
||||
|
||||
return unless define_include_method
|
||||
|
||||
unless define_include_method
|
||||
# Don't include serialized methods if the plugin is disabled
|
||||
plugin = self
|
||||
klass.send(:define_method, "include_#{attr}?") { plugin.enabled? }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def whitelist_staff_user_custom_field(field)
|
||||
User.register_plugin_staff_custom_field(field, self)
|
||||
|
@ -93,34 +94,35 @@ class Plugin::Instance
|
|||
|
||||
# Extend a class but check that the plugin is enabled
|
||||
# for class methods use `add_class_method`
|
||||
def add_to_class(klass, attr, &block)
|
||||
klass = klass.to_s.classify.constantize rescue klass.to_s.constantize
|
||||
|
||||
def add_to_class(class_name, attr, &block)
|
||||
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)
|
||||
|
||||
plugin = self
|
||||
klass.send(:define_method, attr) do |*args|
|
||||
send(hidden_method_name, *args) if plugin.enabled?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Adds a class method to a class, respecting if plugin is enabled
|
||||
def add_class_method(klass, attr, &block)
|
||||
klass = klass.to_s.classify.constantize rescue klass.to_s.constantize
|
||||
def add_class_method(klass_name, attr, &block)
|
||||
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"
|
||||
klass.send(:define_singleton_method, hidden_method_name, &block)
|
||||
|
||||
plugin = self
|
||||
klass.send(:define_singleton_method, attr) do |*args|
|
||||
send(hidden_method_name, *args) if plugin.enabled?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def add_model_callback(klass, callback, options = {}, &block)
|
||||
klass = klass.to_s.classify.constantize rescue klass.to_s.constantize
|
||||
plugin = self
|
||||
def add_model_callback(klass_name, callback, options = {}, &block)
|
||||
reloadable_patch(self) do |plugin|
|
||||
klass = klass_name.to_s.classify.constantize rescue klass_name.to_s.constantize
|
||||
|
||||
# generate a unique method name
|
||||
method_name = "#{plugin.name}_#{klass.name}_#{callback}#{@idx}".underscore
|
||||
|
@ -134,6 +136,7 @@ class Plugin::Instance
|
|||
|
||||
hidden_method_name
|
||||
end
|
||||
end
|
||||
|
||||
# Add validation method but check that the plugin is enabled
|
||||
def validate(klass, name, &block)
|
||||
|
@ -435,4 +438,16 @@ JS
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue