Module: ImmosquareActiveRecordChangeTracker::ClassMethods
- Defined in:
- lib/immosquare-active-record-change-tracker.rb
Instance Method Summary collapse
-
#kept_in_db? ⇒ Boolean
## True si le modèle hôte utilise paranoia (acts_as_paranoid).
- #track_active_record_changes(options = {}, &modifier_block) ⇒ Object
Instance Method Details
#kept_in_db? ⇒ Boolean
##
True si le modèle hôte utilise paranoia (acts_as_paranoid). La gem paranoia est optionnelle : si elle n’est pas chargée, paranoid? n’existe pas — d’où le respond_to?.
##
17 18 19 |
# File 'lib/immosquare-active-record-change-tracker.rb', line 17 def kept_in_db? respond_to?(:paranoid?) && paranoid? end |
#track_active_record_changes(options = {}, &modifier_block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/immosquare-active-record-change-tracker.rb', line 21 def track_active_record_changes( = {}, &modifier_block) ##============================================================## ## Inclut les méthodes d'instance nécessaires ##============================================================## include(ImmosquareActiveRecordChangeTracker::InstanceMethods) ##============================================================## ## Construire dynamiquement les options de l'association ##============================================================## = { :as => :recordable, :class_name => "ImmosquareActiveRecordChangeTracker::HistoryRecord" } ##============================================================## ## Ajouter :dependent => :destroy si acts_as_paranoid n'est pas utilisé. ## Avec paranoia on garde l'historique au soft-delete et on le ## nettoie via after_real_destroy. ##============================================================## [:dependent] = :destroy if !kept_in_db? ##============================================================## ## Ajout de l'association has_many :history_records ##============================================================## has_many(:history_records, -> { order(:created_at => :desc) }, **) ##============================================================## ## Stocker les options dans un attribut de classe ##============================================================## class_attribute(:history_options) self. = ##============================================================## ## Stocker le bloc du modificateur s'il est fourni ##============================================================## [:modifier_block] = modifier_block if block_given? ##============================================================## ## Configure le callback after_save et after_destroy ##============================================================## after_save(:save_change_history) after_destroy(:delete_change_history) after_real_destroy(:delete_all_change_histories) if kept_in_db? end |