Module: Cms::Behaviors::Versioning::InstanceMethods
- Defined in:
- lib/cms/behaviors/versioning.rb
Instance Method Summary collapse
- #as_of_draft_version ⇒ Object
- #as_of_version(version) ⇒ Object
- #build_new_version ⇒ Object
- #current_version ⇒ Object
- #default_version_comment ⇒ Object
- #draft ⇒ Object
- #draft_attributes ⇒ Object
- #draft_version? ⇒ Boolean
- #find_version(number) ⇒ Object
- #initialize_version ⇒ Object
- #live_version ⇒ Object
- #live_version? ⇒ Boolean
- #revert ⇒ Object
- #revert_to(version) ⇒ Object
- #revert_to_without_save(version) ⇒ Object
- #save(perform_validations = true) ⇒ Object
- #save!(perform_validations = true) ⇒ Object
- #version_comment ⇒ Object
- #version_comment=(version_comment) ⇒ Object
Instance Method Details
#as_of_draft_version ⇒ Object
202 203 204 |
# File 'lib/cms/behaviors/versioning.rb', line 202 def as_of_draft_version as_of_version(draft.version) end |
#as_of_version(version) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/cms/behaviors/versioning.rb', line 206 def as_of_version(version) v = find_version(version) raise ActiveRecord::RecordNotFound.new("version #{version.inspect} does not exist for <#{self.class}:#{id}>") unless v obj = self.class.new (self.class.versioned_columns + [:version, :created_at, :created_by_id, :updated_at, :updated_by_id]).each do |a| obj.send("#{a}=", v.send(a)) end obj.id = id obj.lock_version = lock_version # Need to do this so associations can be loaded obj.instance_variable_set("@new_record", false) # Callback to allow us to load other data when an older version is loaded obj.after_as_of_version if obj.respond_to?(:after_as_of_version) # Last but not least, clear the changed attributes if changed_attrs = obj.send(:changed_attributes) changed_attrs.clear end obj end |
#build_new_version ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/cms/behaviors/versioning.rb', line 76 def build_new_version # First get the values from the draft attrs = draft_attributes # Now overwrite any changed values self.class.versioned_columns.each do |col| if(send("#{col}_changed?")) attrs[col] = send(col) end end attrs[:version_comment] = @version_comment || default_version_comment @version_comment = nil new_version = versions.build(attrs) new_version.version = new_record? ? 1 : (draft.version.to_i + 1) after_build_new_version(new_version) if respond_to?(:after_build_new_version) new_version end |
#current_version ⇒ Object
194 195 196 |
# File 'lib/cms/behaviors/versioning.rb', line 194 def current_version find_version(self.version) end |
#default_version_comment ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/cms/behaviors/versioning.rb', line 102 def default_version_comment if new_record? "Created" else "Changed #{(changes.keys - %w[version created_by_id updated_by_id]).sort.join(', ')}" end end |
#draft ⇒ Object
178 179 180 |
# File 'lib/cms/behaviors/versioning.rb', line 178 def draft versions.first(:order => "version desc") end |
#draft_attributes ⇒ Object
95 96 97 98 99 100 |
# File 'lib/cms/behaviors/versioning.rb', line 95 def draft_attributes # When there is no draft, we'll just copy the attibutes from this object # Otherwise we need to use the draft d = new_record? ? self : draft self.class.versioned_columns.inject({}){|attrs, col| attrs[col] = d.send(col); attrs } end |
#draft_version? ⇒ Boolean
182 183 184 |
# File 'lib/cms/behaviors/versioning.rb', line 182 def draft_version? version == draft.version end |
#find_version(number) ⇒ Object
198 199 200 |
# File 'lib/cms/behaviors/versioning.rb', line 198 def find_version(number) versions.first(:conditions => { :version => number }) end |
#initialize_version ⇒ Object
72 73 74 |
# File 'lib/cms/behaviors/versioning.rb', line 72 def initialize_version self.version = 1 end |
#live_version ⇒ Object
186 187 188 |
# File 'lib/cms/behaviors/versioning.rb', line 186 def live_version find_version(self.class.find(id).version) end |
#live_version? ⇒ Boolean
190 191 192 |
# File 'lib/cms/behaviors/versioning.rb', line 190 def live_version? version == self.class.find(id).version end |
#revert ⇒ Object
231 232 233 234 |
# File 'lib/cms/behaviors/versioning.rb', line 231 def revert draft_version = draft.version revert_to(draft_version - 1) unless draft_version == 1 end |
#revert_to(version) ⇒ Object
247 248 249 250 |
# File 'lib/cms/behaviors/versioning.rb', line 247 def revert_to(version) revert_to_without_save(version) save end |
#revert_to_without_save(version) ⇒ Object
236 237 238 239 240 241 242 243 244 245 |
# File 'lib/cms/behaviors/versioning.rb', line 236 def revert_to_without_save(version) raise "Version parameter missing" if version.blank? self.revert_to_version = find_version(version) raise "Could not find version #{version}" unless revert_to_version (self.class.versioned_columns - ["version"]).each do |a| send("#{a}=", revert_to_version.send(a)) end self.version_comment = "Reverted to version #{version}" self end |
#save(perform_validations = true) ⇒ Object
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/cms/behaviors/versioning.rb', line 110 def save(perform_validations=true) transaction do #logger.info "..... Calling valid?" return false unless !perform_validations || valid? if changed? #logger.info "..... Changes => #{changes.inspect}" else #logger.info "..... No Changes" return true end #logger.info "..... Calling before_save" return false if callback(:before_save) == false if new_record? #logger.info "..... Calling before_create" return false if callback(:before_create) == false else #logger.info "..... Calling before_update" return false if callback(:before_update) == false end #logger.info "..... Calling build_new_version" new_version = build_new_version #logger.info "..... Is new version valid? #{new_version.valid?}" if new_record? self.version = 1 #logger.info "..... Calling create_without_callbacks" if result = create_without_callbacks #logger.info "..... Calling after_create" if callback(:after_create) != false #logger.info "..... Calling after_save" callback(:after_save) end if @publish_on_save publish @publish_on_save = nil end changed_attributes.clear end result elsif new_version #logger.info "..... Calling save" if result = new_version.save #logger.info "..... Calling after_save" if callback(:after_update) != false #logger.info "..... Calling after_update" callback(:after_save) end if @publish_on_save publish @publish_on_save = nil end changed_attributes.clear end result end true end end |
#save!(perform_validations = true) ⇒ Object
174 175 176 |
# File 'lib/cms/behaviors/versioning.rb', line 174 def save!(perform_validations=true) save(perform_validations) || raise(ActiveRecord::RecordNotSaved.new(errors.)) end |
#version_comment ⇒ Object
252 253 254 |
# File 'lib/cms/behaviors/versioning.rb', line 252 def version_comment @version_comment end |
#version_comment=(version_comment) ⇒ Object
256 257 258 259 |
# File 'lib/cms/behaviors/versioning.rb', line 256 def version_comment=(version_comment) @version_comment = version_comment send(:changed_attributes)["version_comment"] = @version_comment end |