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

.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
55
56
57
58
59
60
61
62
# File 'lib/traka/change.rb', line 33

def changes(opts={})
  opts = {:version => nil,
          :filter => true,
          :actions => [],
          :only => []}.merge(opts)

  # If version is specified, return only published changes from v onwards.
  # Otherwise, return only unstaged changes.
  unless opts[:version].is_a?(Range)
    opts[:version] = opts[:version]    ?
      (opts[:version]..latest_version) :
      latest_version + 1
  end

  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.all
  end
end

.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

Instance Method Details

#get_recordObject



94
95
96
97
# File 'lib/traka/change.rb', line 94

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