Module: Archiving::ArchiveTable::ClassMethods
- Defined in:
- lib/archiving/archive_table.rb
Instance Attribute Summary collapse
-
#archive_associations ⇒ Object
readonly
Returns the value of attribute archive_associations.
-
#archive_table ⇒ Object
Returns the value of attribute archive_table.
Instance Method Summary collapse
- #archive ⇒ Object
- #archive_aged_records(where: ['created_at < ?', 6.months.ago], order: :id, batch_size: 100, before_callback: nil) ⇒ Object
- #has_archive_associations(associations) ⇒ Object
- #has_archive_table(connection: nil, read_only: true) ⇒ Object
- #with_archive(query = nil, order: nil, limit: nil, offset: nil) ⇒ Object
Instance Attribute Details
#archive_associations ⇒ Object (readonly)
Returns the value of attribute archive_associations.
65 66 67 |
# File 'lib/archiving/archive_table.rb', line 65 def archive_associations @archive_associations end |
#archive_table ⇒ Object
Returns the value of attribute archive_table.
9 10 11 |
# File 'lib/archiving/archive_table.rb', line 9 def archive_table @archive_table end |
Instance Method Details
#archive ⇒ Object
25 26 27 |
# File 'lib/archiving/archive_table.rb', line 25 def archive @archive_model end |
#archive_aged_records(where: ['created_at < ?', 6.months.ago], order: :id, batch_size: 100, before_callback: nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/archiving/archive_table.rb', line 51 def archive_aged_records(where: ['created_at < ?', 6.months.ago], order: :id, batch_size: 100, before_callback: nil) return unless archive records = nil while records.nil? || records.any? before_callback.call if before_callback records = self.where(where).order(order).limit(batch_size) transaction do records.each(&:archive!) end end end |
#has_archive_associations(associations) ⇒ Object
67 68 69 |
# File 'lib/archiving/archive_table.rb', line 67 def has_archive_associations(associations) @archive_associations = associations end |
#has_archive_table(connection: nil, read_only: true) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/archiving/archive_table.rb', line 11 def has_archive_table(connection: nil, read_only: true) model = name.constantize @archive_model = model.const_set("Archive", Class.new(model)) if read_only @archive_model.after_initialize do |record| record.readonly! unless record.new_record? end end @archive_model.table_name = "#{table_name}_archive" if connection @archive_model.establish_connection connection end end |
#with_archive(query = nil, order: nil, limit: nil, offset: nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/archiving/archive_table.rb', line 29 def with_archive(query = nil, order: nil, limit: nil, offset: nil) active = select_archive_attributes_for(self, archive_table_type: "active") active = query.call(active) if query archived = select_archive_attributes_for(archive, archive_table_type: "archived") archived = query.call(archived) if query sql = "(#{active.to_sql}) UNION (#{archived.to_sql})" if order sql << sanitize_sql([" ORDER BY %s", order]) end if limit sql << sanitize_sql([" LIMIT %s", limit]) end if offset sql << sanitize_sql([" OFFSET %s", offset]) end find_active_and_archived_by_sql(sql) end |