require "active_patterns" # Declares service object for merging ActiveRecord instances. # # After extending your active record model with the module, # new ::merge_all method is available. # module ActiveMerge extend ActiveSupport::Autoload autoload :SimpleService autoload :Service # Merges instances from the association # # class Lord < ActiveRecord::Base # extend ActiveMerge # end # # Lord.all.merge_all # => merges all the class instances # Lord.where("id > :id", id: 100) # => merges instances with id > 100 # # You can use the validate: false option. # # With this option set any activerecord validation and callback are skipped. # # Post.all.merge_all validate: false # # See details in ActiveMerge::Service#provide documentation. # def merge_all(options = {}) ActiveMerge::Service.new(all).provide(options) end end