A simple way to provide hideability to ActiveRecord models. Mark records as hidden instead of destroying them for good.
Run the generator to create migrations for all of the models you want to hide:
rails generate hideable:migration Foo Bar rake db:migrate
Make your models
class Foo < ActiveRecord::Base extend Hideable::ActiveRecord hideable end
This will add
#hidden? instance methods and
Hideable is designed to work in a similar way to the
:dependent option on associations too, if that's what you want.
class Foo < ActiveRecord::Base has_one :bar extend Hideable::ActiveRecord hideable :dependent => :hide end class Bar < ActiveRecord::Base belongs_to :foo extend Hideable::ActiveRecord hideable end
All models that you wish to hide, including dependents, must be declared as
foo = Foo.not_hidden.first foo.hide! foo.bar.hidden? #=> true
Hiding dependents requires the records to be instantiated and can be quite expensive depending on the number of records. You can move the operation into the background where suitable. Instead of
:dependent => :hide, use the
:updated option to provide a method name or callable that will be called when the object is hidden or unhidden. The following example demonstrates the use of Sidekiq to hide dependents asynchronously.
class Foo < ActiveRecord::Base has_one :bar extend Hideable::ActiveRecord hideable :updated => :update_bars private def update_bars HideableWorker.perform_async(self.class.name, self.id) end end class Bar < ActiveRecord::Base belongs_to :foo extend Hideable::ActiveRecord hideable end require 'sidekiq' class HideableWorker include Sidekiq::Worker def perform(klass_name, id) klass = klass_name.constantize klass.find(id).update_hideable_dependencies! end end
Install the general dependencies, including all the versions of
hideable is tested against.
bundle install && rake appraisal:install
Run tests as follows.