Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,31 @@ There are a handful of exciting new additions in version 1.0 of <tt>vestal_versi
>> @user.version
=> 2

* Plugin modules. By setting <tt>:plugins</tt> to a module, or array of modules, you can mixin your own code.

module MyCustomPlugin
extend ActiveSupport::Concern
included do
class_attribute :plugged_in_model
VestalVersions::Version.class_eval{ include MyCustomPluginMethods}
end
end
module MyCustomPluginMethods
extend ActiveSupport::Concern
included do
class_attribute :plugged_in_version
end
end

class User < ActiveRecord::Base
versioned :plugins => MyCustomPlugin
end

>> User.respond_to? :plugged_in_model
=> true
>> VestalVersions::Version.respond_to? :plugged_in_version
=> true

== Thanks!

Thank you to all those who post {issues and suggestions}[http://github.com/laserlemon/vestal_versions/issues]. And special thanks to:
Expand All @@ -197,4 +222,4 @@ Thank you to all those who post {issues and suggestions}[http://github.com/laser
* snaury[http://github.com/snaury], who helped out early on with the <tt>between</tt> association method, the <tt>:dependent</tt> option and a conflict from using a method called <tt>changes</tt>
* sthapit[http://github.com/sthapit], who was responsible for the <tt>:only</tt> and <tt>:except</tt> options as well as showing me that I'm a dummy for storing a useless first version

To contribute to <tt>vestal_versions</tt>, please fork, hack away in the integration[http://github.com/laserlemon/vestal_versions/tree/integration] branch and send me a pull request. Remember your tests!
To contribute to <tt>vestal_versions</tt>, please fork, hack away in the integration[http://github.com/laserlemon/vestal_versions/tree/integration] branch and send me a pull request. Remember your tests!
5 changes: 5 additions & 0 deletions lib/vestal_versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module VestalVersions
autoload :Control
autoload :Creation
autoload :Deletion
autoload :Plugins
autoload :Options
autoload :Reload
autoload :Reset
Expand Down Expand Up @@ -102,6 +103,9 @@ module ClassMethods
# is called on any symbols given and the resulting procs are called, passing in the object
# itself. If an array is given and any element evaluates as +true+, the version creation will
# be skipped.
# * <tt>:plugins</tt>: This option allows you to load additional functionality from a custom
# module or array of modules, allowing you to extend functionality of both the Version model
# and the +versioned+ models. See VestalVersions::Users for an example plugin class.
def versioned(options = {}, &block)
return if versioned?

Expand All @@ -116,6 +120,7 @@ def versioned(options = {}, &block)
include VersionTagging
include Reload
include Deletion
include Plugins

prepare_versioned_options(options)
has_many :versions, options, &block
Expand Down
29 changes: 29 additions & 0 deletions lib/vestal_versions/plugins.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module VestalVersions
# Allows custom modules to be loaded based on given <tt>:plugins</tt> option
module Plugins
extend ActiveSupport::Concern
# Class methods on ActiveRecord::Base to prepare the <tt>:plugins</tt> option.
module ClassMethods
# After the original +prepare_versioned_options+ method cleans the given options, this alias
# also extracts the <tt>:plugins</tt> and includes them if required
def prepare_versioned_options(options)
result = super(options)

# Pull the plugins out of options, constantize or ensure they're a Module, otherwise drop them
plugins = Array(result.delete :plugins).map{|p|
if p.is_a? Module
p
elsif p.is_a? String
p.constantize
end
}.flatten.compact

# Include requested plugins
plugins.each {|p| include p unless self.included_modules.include? p}
self.vestal_versions_options[:plugins] = plugins

result
end
end
end
end
16 changes: 16 additions & 0 deletions spec/support/plugins.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module MyCustomPlugin
extend ActiveSupport::Concern

included do
class_attribute :plugged_in_model
VestalVersions::Version.class_eval{ include MyCustomPluginMethods}
end
end

module MyCustomPluginMethods
extend ActiveSupport::Concern

included do
class_attribute :plugged_in_version
end
end
8 changes: 7 additions & 1 deletion spec/vestal_versions/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
before do
VestalVersions::Version.config.clear
VestalVersions::Version.config.class_name = 'MyCustomVersion'
User.prepare_versioned_options(options.dup)
end

it 'stores options in a class variable' do
User.vestal_versions_options.slice(:class_name).should == {
:class_name => 'MyCustomVersion'
}
end

it 'has symbolized keys' do
Expand All @@ -20,7 +27,6 @@
:class_name => 'MyCustomVersion'
}
end

end

context 'default configuration options' do
Expand Down
25 changes: 25 additions & 0 deletions spec/vestal_versions/plugins_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'

describe VestalVersions::Plugins do
context 'when a plugin is requested' do
before do
User.prepare_versioned_options(:plugins => 'MyCustomPlugin')
end

it 'stores plugins the options hash' do
User.vestal_versions_options[:plugins].should == [MyCustomPlugin]
end

it 'converts strings to modules' do
User.vestal_versions_options[:plugins].all?{|p| p.is_a?(Module)}
end

it 'modifies versioned models if requested' do
User.should respond_to(:plugged_in_model)
end

it 'modifies Version if requested' do
VestalVersions::Version.should respond_to(:plugged_in_version)
end
end
end