Module: Mongoid::History::Trackable::MyInstanceMethods

Defined in:
lib/mongoid/history/trackable.rb

Instance Method Summary collapse

Instance Method Details

#get_version(version) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/mongoid/history/trackable.rb', line 161

def get_version(version)
  track = history_tracks.first
  
  track.modified.keys.each do |key|
    track[key] = track.modified[key]
  end
  
  version -= 1
  
  if (version < 0)
    return nil
  end
  
  if (version >= history_tracks.count)
    version = history_tracks.count-1
  end
  
  for i in 1..version

    history_tracks[i].modified.keys.each do |key|
      track[key] = history_tracks[i].modified[key]
    end

    track.modified = history_tracks[i].modified
    track.original = history_tracks[i].original

  end

  track.version = version
  track
end

#history_tracksObject



80
81
82
# File 'lib/mongoid/history/trackable.rb', line 80

def history_tracks
  @history_tracks ||= Mongoid::History.tracker_class.where(:scope => history_trackable_options[:scope], :association_chain => association_hash)
end

#redo!(modifier, options_or_version = nil) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/mongoid/history/trackable.rb', line 150

def redo!(modifier, options_or_version=nil)
  versions = get_versions_criteria(options_or_version).to_a
  versions.sort!{|v1, v2| v1.version <=> v2.version}

  versions.each do |v|
    redo_attr = v.redo_attr(modifier)
    self.attributes = redo_attr
  end
  save!
end

#undo!(modifier, options_or_version = nil) ⇒ Object

undo :from => 1, :to => 5

undo 4
undo :last => 10


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mongoid/history/trackable.rb', line 87

def undo!(modifier, options_or_version=nil)
  if (!options_or_version.is_a?(Hash))
    version = get_version(options_or_version.to_i)
    attrs = {}
    fields = history_tracks.first.modified.keys
    
    if history_trackable_options[:on] == :all

      fields.each do |k|
        if !history_trackable_options[:except].include?(k)
          if (!version.attributes[:modified].nil? && version.attributes[:modified].include?(k))
            attrs[k] = version.attributes[:modified][k]
          else
            attrs[k] = version.attributes[k]
          end
        end
      end
  
    else
  
      fields.each do |k|
        if history_trackable_options[:on].include?(k)
          
          if (!version.attributes[:modified].nil? && version.attributes[:modified].include?(k))
            attrs[k] = version.attributes[:modified][k]
          else
            attrs[k] = version.attributes[k]
          end
          
        end
      end
  
    end
    
    if history_trackable_options[:remember_attachment]
      
      addr = [Rails.root, '/public/', 'system/', 'store/', attachment_definitions.first.first.to_s, '/', id.to_s, '/', version.data_updated_at.to_i.to_s, '/'].join()
      Dir.chdir(addr)
      file_name = Dir.glob('*.*').first
      
      attach = File.new(addr + file_name) 
      data = attach
      
      attrs["data_file_name"] = file_name
      
    end
    
    self.update_attributes(attrs)
    
  else
    
    versions = get_versions_criteria(options_or_version).to_a
    versions.sort!{|v1, v2| v2.version <=> v1.version}
    
    versions.each do |v|
      undo_attr = v.undo_attr(modifier)
      self.attributes = v.undo_attr(modifier)
    end
  end

  save!
end