lib/mobility/plugins/dirty.rb in mobility-0.8.13 vs lib/mobility/plugins/dirty.rb in mobility-1.0.0.alpha

- old
+ new

@@ -1,8 +1,6 @@ # frozen_string_literal: true -require "mobility/backend_resetter" -require "mobility/plugins/fallthrough_accessors" module Mobility module Plugins =begin @@ -18,42 +16,23 @@ through to that locale as changed, even though it may look like it has changed. See the specs for details on expected behavior. =end module Dirty - class << self - # Applies dirty plugin to attributes for a given option value. - # @param [Attributes] attributes - # @param [Boolean] option Value of option - # @raise [ArgumentError] if model class does not support dirty tracking - def apply(attributes, option) - if option - FallthroughAccessors.apply(attributes, true) - include_dirty_module(attributes.backend_class, attributes.model_class, *attributes.names) - end - end + extend Plugin - private + default true - def include_dirty_module(backend_class, model_class, *attribute_names) - dirty_module = - if Loaded::ActiveRecord && model_class.ancestors.include?(::ActiveModel::Dirty) - if (model_class < ::ActiveRecord::Base) - require "mobility/plugins/active_record/dirty" - Plugins::ActiveRecord::Dirty - else - require "mobility/plugins/active_model/dirty" - Plugins::ActiveModel::Dirty - end - elsif Loaded::Sequel && model_class < ::Sequel::Model - require "mobility/plugins/sequel/dirty" - Plugins::Sequel::Dirty - else - raise ArgumentError, "#{model_class} does not support Dirty module." - end - backend_class.include dirty_module.const_get(:BackendMethods) - model_class.include dirty_module.const_get(:MethodsBuilder).new(*attribute_names) + requires :backend, include: :before + requires :fallthrough_accessors + + initialize_hook do + if options[:dirty] && !options[:fallthrough_accessors] + warn 'The Dirty plugin depends on Fallthrough Accessors being enabled, '\ + 'but fallthrough_accessors option is falsey' end end end + + register_plugin(:dirty, Dirty) end end