mongo_mapper_acts_as_versioned 0.0.12 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +2 -0
- data/lib/acts_as_versioned.rb +36 -30
- data/spec/acts_as_versioned_spec.rb +1 -2
- data/spec/spec_helper.rb +2 -0
- metadata +1 -1
    
        data/README.md
    CHANGED
    
    | @@ -2,6 +2,8 @@ | |
| 2 2 |  | 
| 3 3 | 
             
            Basic MongoMapper port of technoweenie's [acts_as_versioned](http://github.com/technoweenie/acts_as_versioned). Stores changed attributes in a Hash key inside an Embedded Document instead of copying all keys from the original model.
         | 
| 4 4 |  | 
| 5 | 
            +
            *Note:* This plugin is intended for MongoMapper 0.8.6. The plugin architecture must be slightly changed for it to work on 0.9+.
         | 
| 6 | 
            +
             | 
| 5 7 | 
             
            ## Usage
         | 
| 6 8 |  | 
| 7 9 | 
             
            ### Basic Example
         | 
    
        data/lib/acts_as_versioned.rb
    CHANGED
    
    | @@ -1,38 +1,39 @@ | |
| 1 | 
            -
            require 'active_support/concern' unless defined?(ActiveSupport)
         | 
| 2 | 
            -
             | 
| 3 1 | 
             
            module MongoMapper
         | 
| 4 2 | 
             
              module Acts
         | 
| 5 3 | 
             
                module Versioned
         | 
| 6 | 
            -
                   | 
| 7 | 
            -
             | 
| 8 | 
            -
                  VERSION   = '0.0.12'
         | 
| 4 | 
            +
                  VERSION   = '0.1.0'
         | 
| 9 5 | 
             
                  CALLBACKS = [:save_version, :clear_old_versions]
         | 
| 10 6 |  | 
| 11 | 
            -
                   | 
| 12 | 
            -
                     | 
| 7 | 
            +
                  def self.configure(model)
         | 
| 8 | 
            +
                    model.class_eval do
         | 
| 9 | 
            +
                      cattr_accessor :versioned_class_name,
         | 
| 10 | 
            +
                        :non_versioned_keys, :max_version_limit
         | 
| 13 11 |  | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 12 | 
            +
                      self.versioned_class_name = :Version
         | 
| 13 | 
            +
                      self.max_version_limit    = 0
         | 
| 14 | 
            +
                      self.non_versioned_keys   = %w[
         | 
| 15 | 
            +
                        _id _type created_at updated_at
         | 
| 16 | 
            +
                        creator_id updater_id version
         | 
| 17 | 
            +
                      ]
         | 
| 19 18 |  | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 19 | 
            +
                      const_set(versioned_class_name, Class.new).class_eval do
         | 
| 20 | 
            +
                        include MongoMapper::EmbeddedDocument
         | 
| 22 21 |  | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 22 | 
            +
                        key :version,  Integer
         | 
| 23 | 
            +
                        key :modified, Hash
         | 
| 24 | 
            +
                      end
         | 
| 26 25 |  | 
| 27 | 
            -
             | 
| 28 | 
            -
                       | 
| 29 | 
            -
                         | 
| 26 | 
            +
                      class_name = "#{self}::#{versioned_class_name}"
         | 
| 27 | 
            +
                      many :versions, :class => class_name.constantize do
         | 
| 28 | 
            +
                        def [](version)
         | 
| 29 | 
            +
                          detect { |doc| doc.version.to_s == version.to_s }
         | 
| 30 | 
            +
                        end
         | 
| 30 31 | 
             
                      end
         | 
| 31 | 
            -
                    end
         | 
| 32 32 |  | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
| 33 | 
            +
                      key :version, Integer
         | 
| 34 | 
            +
                      before_save :save_version
         | 
| 35 | 
            +
                      before_save :clear_old_versions
         | 
| 36 | 
            +
                    end
         | 
| 36 37 | 
             
                  end
         | 
| 37 38 |  | 
| 38 39 | 
             
                  module InstanceMethods
         | 
| @@ -111,9 +112,10 @@ module MongoMapper | |
| 111 112 | 
             
                  protected
         | 
| 112 113 |  | 
| 113 114 | 
             
                    def next_version
         | 
| 114 | 
            -
                      new_record? || versions.empty? ? | 
| 115 | 
            +
                      new_record? || versions.empty? ?
         | 
| 116 | 
            +
                        1 : versions.map(&:version).max.next
         | 
| 115 117 | 
             
                    end
         | 
| 116 | 
            -
                  end
         | 
| 118 | 
            +
                  end # InstanceMethods
         | 
| 117 119 |  | 
| 118 120 | 
             
                  module ClassMethods
         | 
| 119 121 | 
             
                    def versioned_class
         | 
| @@ -124,6 +126,10 @@ module MongoMapper | |
| 124 126 | 
             
                      keys.keys - non_versioned_keys
         | 
| 125 127 | 
             
                    end
         | 
| 126 128 |  | 
| 129 | 
            +
                    def do_not_version(*args)
         | 
| 130 | 
            +
                      self.non_versioned_keys = non_versioned_keys | args
         | 
| 131 | 
            +
                    end
         | 
| 132 | 
            +
             | 
| 127 133 | 
             
                    def without_revision
         | 
| 128 134 | 
             
                      class_eval do
         | 
| 129 135 | 
             
                        CALLBACKS.each do |attr_name|
         | 
| @@ -139,7 +145,7 @@ module MongoMapper | |
| 139 145 | 
             
                        end
         | 
| 140 146 | 
             
                      end
         | 
| 141 147 | 
             
                    end
         | 
| 142 | 
            -
                  end
         | 
| 143 | 
            -
                end
         | 
| 144 | 
            -
              end
         | 
| 145 | 
            -
            end
         | 
| 148 | 
            +
                  end # ClassMethods
         | 
| 149 | 
            +
                end # Versioned
         | 
| 150 | 
            +
              end # Acts
         | 
| 151 | 
            +
            end # MongoMapper
         | 
    
        data/spec/spec_helper.rb
    CHANGED