myexperiment-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myexperiment-hackers] [3044] branches/versions/vendor/plugins: adding r


From: noreply
Subject: [myexperiment-hackers] [3044] branches/versions/vendor/plugins: adding replacement versioning plugin
Date: Wed, 25 Jul 2012 13:58:29 +0000 (UTC)

Revision
3044
Author
dgc
Date
2012-07-25 13:58:28 +0000 (Wed, 25 Jul 2012)

Log Message

adding replacement versioning plugin

Added Paths

Diff

Added: branches/versions/vendor/plugins/versioning/MIT-LICENSE (0 => 3044)


--- branches/versions/vendor/plugins/versioning/MIT-LICENSE	                        (rev 0)
+++ branches/versions/vendor/plugins/versioning/MIT-LICENSE	2012-07-25 13:58:28 UTC (rev 3044)
@@ -0,0 +1,20 @@
+Copyright (c) 2011 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Added: branches/versions/vendor/plugins/versioning/README (0 => 3044)


--- branches/versions/vendor/plugins/versioning/README	                        (rev 0)
+++ branches/versions/vendor/plugins/versioning/README	2012-07-25 13:58:28 UTC (rev 3044)
@@ -0,0 +1,57 @@
+Versioning
+==========
+
+This plugin provides a simple versioning mechanism for Active Record.  Two
+models are required: The model to be versioned and a model that contains the
+versioned data for each version.
+
+
+Example
+=======
+
+This example requires the following tables.  A Record model which contains a
+title, description and some content.
+
+
+  create_table "records" do |t|
+    t.string   "title"
+    t.text     "description"
+    t.binary   "content"
+    t.datetime "created_at"
+    t.datetime "updated_at"
+    t.integer  "current_version"
+  end
+
+  create_table "record_versions" do |t|
+    t.string   "title"
+    t.text     "description"
+    t.binary   "content"
+    t.datetime "created_at"
+    t.datetime "updated_at"
+    t.integer  "version"
+  end
+
+
+In the following model code, we're stating that the "title", "description" and
+"content" attributes are to be versioned.  In addition, the "title" and
+"description" attributes may be changed in a specific version.
+
+
+  class Record < ActiveRecord::Base
+    has_versions :record_versions,
+        :attributes => [ :title, :description, :content ],
+        :mutable    => [ :title, :description ]
+  end
+
+  class RecordVersion < ActiveRecord::Base
+    is_version_of :record
+  end
+
+
+Since "content" is the only attribute that cannot be changed in the
+RecordVersion class, saving a record with different content will cause a new
+version to be created.  If only the title or the description changes, then the
+current version is updated to reflect those new changes.
+
+
+Copyright (c) 2011 Don Cruickshank, released under the MIT license

Added: branches/versions/vendor/plugins/versioning/Rakefile (0 => 3044)


--- branches/versions/vendor/plugins/versioning/Rakefile	                        (rev 0)
+++ branches/versions/vendor/plugins/versioning/Rakefile	2012-07-25 13:58:28 UTC (rev 3044)
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the versioning plugin.'
+Rake::TestTask.new(:test) do |t|
+  t.libs << 'lib'
+  t.libs << 'test'
+  t.pattern = 'test/**/*_test.rb'
+  t.verbose = true
+end
+
+desc 'Generate documentation for the versioning plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+  rdoc.rdoc_dir = 'rdoc'
+  rdoc.title    = 'Versioning'
+  rdoc.options << '--line-numbers' << '--inline-source'
+  rdoc.rdoc_files.include('README')
+  rdoc.rdoc_files.include('lib/**/*.rb')
+end

Added: branches/versions/vendor/plugins/versioning/init.rb (0 => 3044)


--- branches/versions/vendor/plugins/versioning/init.rb	                        (rev 0)
+++ branches/versions/vendor/plugins/versioning/init.rb	2012-07-25 13:58:28 UTC (rev 3044)
@@ -0,0 +1,7 @@
+# myExperiment: vendor/plugins/versioning/init.rb
+#
+# Copyright (c) 2011 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+ActiveRecord::Base.extend Versioning::ActsMethods
+

Added: branches/versions/vendor/plugins/versioning/install.rb (0 => 3044)


--- branches/versions/vendor/plugins/versioning/install.rb	                        (rev 0)
+++ branches/versions/vendor/plugins/versioning/install.rb	2012-07-25 13:58:28 UTC (rev 3044)
@@ -0,0 +1 @@
+# Install hook code here

Added: branches/versions/vendor/plugins/versioning/lib/tasks/versioning.rake (0 => 3044)


--- branches/versions/vendor/plugins/versioning/lib/tasks/versioning.rake	                        (rev 0)
+++ branches/versions/vendor/plugins/versioning/lib/tasks/versioning.rake	2012-07-25 13:58:28 UTC (rev 3044)
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :versioning do
+#   # Task goes here
+# end

Added: branches/versions/vendor/plugins/versioning/lib/versioning.rb (0 => 3044)


--- branches/versions/vendor/plugins/versioning/lib/versioning.rb	                        (rev 0)
+++ branches/versions/vendor/plugins/versioning/lib/versioning.rb	2012-07-25 13:58:28 UTC (rev 3044)
@@ -0,0 +1,142 @@
+# myExperiment: vendor/plugins/versioning/lib/versioning.rb
+#
+# Copyright (c) 2011 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+module Versioning
+
+  module ActsMethods
+
+    def has_versions(version_class, opts = {})
+
+      cattr_accessor :version_class, :versioned_attributes, :mutable_attributes, :versioned_resource_column, :ignore_columns
+
+      attr_accessor :inhibit_version_check
+
+      self.version_class             = version_class.to_s.camelize.singularize.constantize
+      self.versioned_attributes      = opts[:attributes]                || []
+      self.mutable_attributes        = opts[:mutable]                   || []
+      self.versioned_resource_column = opts[:versioned_resource_column] || "#{self.table_name.singularize}_id"
+      self.ignore_columns            = [:id, :version, :created_at, :updated_at, self.versioned_resource_column.to_sym]
+
+      inhibit_version_check = false
+
+      class_eval do
+
+        has_many :versions, :class_name => self.version_class.to_s
+
+        def find_version(v)
+          match = self.version_class.find(:first, :conditions => ["#{self.versioned_resource_column} = ? AND version = ?", id, v])
+          return match if match
+
+          raise ActiveRecord::RecordNotFound.new("Couldn't find #{self.version_class.name} with #{self.versioned_resource_column}=#{id} and version=#{v}")
+        end
+
+        def describe_version(version_number)
+          return "" if versions.count < 2
+          return "(earliest)" if version_number == versions.first.version
+          return "(latest)" if version_number == versions.last.version
+          return ""
+        end
+      end
+
+      before_save do |resource|
+
+        unless resource.inhibit_version_check
+
+          # Create a new version if we need to.  We need to do this if either this is
+          # a new record or an attribute has changed that is version tracked.
+
+          new_version = resource.version_class.new
+
+          versioned_attributes = new_version.attribute_names.map do |name| name.to_sym end - ignore_columns
+
+          changed_attributes = resource.changes.keys.map do |attr| attr.to_sym end
+
+          changed_versioned_attributes = versioned_attributes & changed_attributes
+
+          if resource.new_record? || ((versioned_attributes - mutable_attributes) & changed_attributes).length > 0
+
+            # If this is a new record or a versioned attribute has changed that is
+            # not marked as mutable, then create a new version.
+
+            resource.current_version = new_version[:version] = resource.current_version ? resource.current_version + 1 : 1
+
+            versioned_attributes.each do |attr|
+              new_version[attr] = resource[attr]
+            end
+
+            resource.versions << new_version
+
+          elsif !changed_versioned_attributes.empty?
+
+            # A new version wasn't created, but some attributes in the latest
+            # version need updating.
+
+            version = resource.find_version(resource.current_version)
+
+            changed_versioned_attributes.each do |attr|
+              version[attr] = resource[attr]
+            end
+
+            version.save
+          end
+        end
+      end
+    end
+
+    def is_version_of(versioned_resource_class_name, opts = {})
+
+      cattr_accessor :versioned_resource_class_name
+
+      attr_accessor :inhibit_version_check
+
+      self.versioned_resource_class_name = versioned_resource_class_name
+
+      inhibit_version_check = false
+
+      class_eval do
+        versioned_resource_class_name
+        belongs_to self.versioned_resource_class_name
+        alias_method :versioned_resource, self.versioned_resource_class_name
+      end
+
+      validate do |version|
+
+        if !version.new_record?
+
+          resource_class = self.versioned_resource_class_name.to_s.camelize.constantize
+
+          immutable_attributes = resource_class.versioned_attributes - resource_class.mutable_attributes
+          changed_attributes   = version.changes.keys.map do |attr| attr.to_sym end
+          blocking_attributes  = immutable_attributes & changed_attributes
+
+          blocking_attributes.each do |attr|
+puts "ADDING ERROR"
+            version.errors.add(attr, 'is immutable')
+          end
+        end
+      end
+      
+      after_save do |version|
+
+        unless version.inhibit_version_check
+
+          versioned_resource = version.versioned_resource
+
+          if version.version == versioned_resource.current_version
+
+            versioned_resource.versioned_attributes.each do |attr|
+              versioned_resource[attr] = version[attr]
+            end
+
+            versioned_resource.inhibit_version_check = true
+            versioned_resource.save
+            versioned_resource.inhibit_version_check = false
+          end
+        end
+      end
+    end
+  end
+end
+

Added: branches/versions/vendor/plugins/versioning/test/test_helper.rb (0 => 3044)


--- branches/versions/vendor/plugins/versioning/test/test_helper.rb	                        (rev 0)
+++ branches/versions/vendor/plugins/versioning/test/test_helper.rb	2012-07-25 13:58:28 UTC (rev 3044)
@@ -0,0 +1,4 @@
+require 'rubygems'
+require 'test/unit'
+require 'active_support'
+require 'active_support/test_case'

Added: branches/versions/vendor/plugins/versioning/test/versioning_test.rb (0 => 3044)


--- branches/versions/vendor/plugins/versioning/test/versioning_test.rb	                        (rev 0)
+++ branches/versions/vendor/plugins/versioning/test/versioning_test.rb	2012-07-25 13:58:28 UTC (rev 3044)
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class VersioningTest < ActiveSupport::TestCase
+  # Replace this with your real tests.
+  test "the truth" do
+    assert true
+  end
+end

Added: branches/versions/vendor/plugins/versioning/uninstall.rb (0 => 3044)


--- branches/versions/vendor/plugins/versioning/uninstall.rb	                        (rev 0)
+++ branches/versions/vendor/plugins/versioning/uninstall.rb	2012-07-25 13:58:28 UTC (rev 3044)
@@ -0,0 +1 @@
+# Uninstall hook code here

reply via email to

[Prev in Thread] Current Thread [Next in Thread]