Module: Snaps

Defined in:
lib/snaps.rb,
lib/snaps/engine.rb,
lib/snaps/version.rb,
lib/snaps/snapshot.rb,
app/models/snaps/tag.rb,
app/helpers/snaps/application_helper.rb,
app/models/concerns/snaps/suppressor.rb,
app/controllers/snaps/application_controller.rb

Defined Under Namespace

Modules: ApplicationHelper, Suppressor Classes: ApplicationController, Engine, Snapshot, SuppressorRegistry, Tag

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.revision(options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
# File 'lib/snaps.rb', line 5

def self.revision(options={})
  Module.new do
    extend ActiveSupport::Concern

    included do
      before_save :ensure_perma_id
      if options[:default_tag]
        after_create do
          snaps_tag!(options[:default_tag])
        end
      end
    end

    def snapshot!(options = {}, &block)
      Snapshot.create(self,
                      options.merge(self.class.snaps_options),
                      &block)
    end

    def snaps_tag!(tag)
      Tag.create!(
        record: self,
        tag: tag
      )
    end

    def snaps_untag!(tag)
      Tag
        .for_all_revisions_of(self)
        .with_name(tag)
        .supersede!
    end

    def snaps_revisions
       self.class.where(perma_id: perma_id)
    end

    def ensure_perma_id
      self.perma_id ||= (self.class.maximum(:perma_id) || 0) + 1
    end

    class_methods do
      def with_snaps_tag(tag, options={})
        if options[:all_revisions]
          Tag.all_revisions_with_tag(self, tag)
        else
          Tag.current_revisions_with_tag(self, tag)
        end
      end

      define_method :snaps_options do
        options
      end
    end
  end
end