Class: Traka::Change

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/traka/change.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.latest_versionObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/traka/change.rb', line 10

def latest_version
  begin
    File.read(version_path).to_i
  rescue
    tc = self.last
    v  = tc ? tc.version : 1

    logger.warn "Latest Traka version not found. Defaulting to v#{v}"

    v
  end
end

.publish_new_version!Object



23
24
25
# File 'lib/traka/change.rb', line 23

def publish_new_version!
  set_version!(latest_version + 1)
end

.set_version!(v) ⇒ Object



27
28
29
30
31
# File 'lib/traka/change.rb', line 27

def set_version!(v)
  File.open(version_path, "w") do |f|
    f.write(v.to_s)
  end
end

.staged_changes(opts = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/traka/change.rb', line 33

def staged_changes(opts={})
  opts = {:version => latest_version + 1,
          :filter => true,
          :actions => [],
          :only => []}.merge(opts)

  c = where(["version #{opts[:version].is_a?(Range) ? "in" : ">="} (?)", opts[:version]])

  unless opts[:actions].empty?
    c = c.where(["action_type in (?)", opts[:actions]])
  end

  unless opts[:only].empty?
    c = c.where(["klass in (?)", opts[:only].map(&:to_s)])
  end

  if opts[:filter]
    filter_changes(c)
  else
    c
  end
end

Instance Method Details

#get_recordObject



87
88
89
90
# File 'lib/traka/change.rb', line 87

def get_record
  ar = ActiveRecord::Base.const_get(self.klass)
  ar.where(ar.traka_uuid => self.uuid).first
end