Skip to content

Commit

Permalink
Import the plugin code from the FatJam Revisable branch.
Browse files Browse the repository at this point in the history
Setup a simple gitignore.
Some initial hoe configuration.
  • Loading branch information
Rich Cavanaugh committed May 3, 2008
1 parent 3172787 commit 3299d0b
Show file tree
Hide file tree
Showing 15 changed files with 578 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
30 changes: 4 additions & 26 deletions config/hoe.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,39 +2,20 @@


AUTHOR = 'Rich Cavanaugh' # can also be an array of Authors AUTHOR = 'Rich Cavanaugh' # can also be an array of Authors
EMAIL = "rich@fatjam.com" EMAIL = "rich@fatjam.com"
DESCRIPTION = "description of gem" DESCRIPTION = "Rails plugin to track revisions to your models."
GEM_NAME = 'acts_as_revisable' # what ppl will type to install your gem GEM_NAME = 'acts_as_revisable' # what ppl will type to install your gem
RUBYFORGE_PROJECT = 'acts_as_revisable' # The unix name for your project HOMEPATH = "http://github.com/rich/acts_as_revisable/tree/master"
HOMEPATH = "http://github.com/rich/acts_as_revisable" DOWNLOAD_PATH = "http://github.com/rich/acts_as_revisable/tree/master"
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
EXTRA_DEPENDENCIES = [ EXTRA_DEPENDENCIES = [
['activesupport', '>= 2.1'], ['activerecord', '>= 2.1']
] # An array of rubygem dependencies [name, version] ] # An array of rubygem dependencies [name, version]


@config_file = "~/.rubyforge/user-config.yml" @config_file = "~/.rubyforge/user-config.yml"
@config = nil @config = nil
RUBYFORGE_USERNAME = "unknown"
def rubyforge_username
unless @config
begin
@config = YAML.load(File.read(File.expand_path(@config_file)))
rescue
puts <<-EOS
ERROR: No rubyforge config file found: #{@config_file}
Run 'rubyforge setup' to prepare your env for access to Rubyforge
- See http://newgem.rubyforge.org/rubyforge.html for more details
EOS
exit
end
end
RUBYFORGE_USERNAME.replace @config["username"]
end



REV = nil REV = nil
# UNCOMMENT IF REQUIRED: # UNCOMMENT IF REQUIRED:
# REV = YAML.load(`svn info`)['Revision'] # REV = YAML.load(`svn info`)['Revision']
VERS = ActsAsRevisable::VERSION::STRING + (REV ? ".#{REV}" : "") VERS = FatJam::ActsAsRevisable::VERSION::STRING + (REV ? ".#{REV}" : "")
RDOC_OPTS = ['--quiet', '--title', 'acts_as_revisable documentation', RDOC_OPTS = ['--quiet', '--title', 'acts_as_revisable documentation',
"--opname", "index.html", "--opname", "index.html",
"--line-numbers", "--line-numbers",
Expand All @@ -55,7 +36,6 @@ def extra_deps
p.description = DESCRIPTION p.description = DESCRIPTION
p.summary = DESCRIPTION p.summary = DESCRIPTION
p.url = HOMEPATH p.url = HOMEPATH
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
p.test_globs = ["test/**/test_*.rb"] p.test_globs = ["test/**/test_*.rb"]
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean. p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.


Expand All @@ -67,7 +47,5 @@ def extra_deps
end end


CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n") CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
$hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
$hoe.rsync_args = '-av --delete --ignore-errors' $hoe.rsync_args = '-av --delete --ignore-errors'
$hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue "" $hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,9 @@
class RevisableMigrationGenerator < Rails::Generator::NamedBase
def manifest
record do |m|
revisable_columns = [["revisable_original_id", "integer"], ["revisable_branched_from_id", "integer"], ["revisable_number", "integer"], ["revisable_name", "string"], ["revisable_type", "string"], ["revisable_current_at", "datetime"], ["revisable_revised_at", "datetime"], ["revisable_deleted_at", "datetime"], ["revisable_is_current", "boolean"]]

m.migration_template 'migration.rb', 'db/migrate', :migration_file_name => "make_#{class_name.downcase}_revisable", :assigns => {:cols => revisable_columns}
end
end
end
13 changes: 13 additions & 0 deletions generators/revisable_migration/templates/migration.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,13 @@
class Make<%= class_name.underscore.camelize %>Revisable < ActiveRecord::Migration
def self.up
<% cols.each do |c| -%>
add_column :<%= class_name.downcase.pluralize %>, :<%= c.first %>, :<%= c.last %>
<% end -%>
end
def self.down
<% cols.each do |c| -%>
remove_column :<%= class_name.downcase.pluralize %>, :<%= c.first %>
<% end -%>
end
end
11 changes: 8 additions & 3 deletions lib/acts_as_revisable.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,11 @@
$:.unshift(File.dirname(__FILE__)) unless $:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))


module ActsAsRevisable require 'acts_as_revisable/version.rb'

require 'acts_as_revisable/acts/scoped_model'
end require 'acts_as_revisable/quoted_columns'
require 'acts_as_revisable/base'

ActiveRecord::Base.send(:include, FatJam::ActsAsScopedModel)
ActiveRecord::Base.send(:include, FatJam::QuotedColumnConditions)
ActiveRecord::Base.send(:include, FatJam::ActsAsRevisable)
83 changes: 83 additions & 0 deletions lib/acts_as_revisable/acts/common.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,83 @@
module FatJam
module ActsAsRevisable
module Common
def self.included(base)
base.send(:extend, ClassMethods)

class << base
alias_method_chain :instantiate, :revisable
end

base.instance_eval do
define_callbacks :before_branch, :after_branch
has_many :branches, :class_name => base.class_name, :foreign_key => :revisable_branched_from_id
belongs_to :branch_source, :class_name => base.class_name, :foreign_key => :revisable_branched_from_id

end
base.alias_method_chain :branch_source, :open_scope
end

def branch_source_with_open_scope(*args, &block)
self.class.without_model_scope do
branch_source_without_open_scope(*args, &block)
end
end

def branch(*args)
unless run_callbacks(:before_branch) { |r, o| r == false}
raise ActiveRecord::RecordNotSaved
end

options = args.extract_options! || {}
options[:revisable_branched_from_id] = self.id
self.class.column_names.each do |col|
next unless self.class.revisable_should_clone_column? col
options[col.to_sym] ||= self[col]
end

returning(self.class.revisable_class.create!(options)) do |br|
run_callbacks(:after_branch)
br.run_callbacks(:after_branch_created)
end
end

def original_id
self[:revisable_original_id] || self[:id]
end
module ClassMethods
def revisable_should_clone_column?(col)
return false if (FatJam::REVISABLE_SYSTEM_COLUMNS + FatJam::REVISABLE_UNREVISABLE_COLUMNS).member? col
true
end

def instantiate_with_revisable(record)
is_current = columns_hash["revisable_is_current"].type_cast(
record["revisable_is_current"])

if (is_current && self == self.revisable_class) || (is_current && self == self.revision_class)
return instantiate_without_revisable(record)
end

object = if is_current
self.revisable_class
else
self.revision_class
end.allocate

object.instance_variable_set("@attributes", record)
object.instance_variable_set("@attributes_cache", Hash.new)

if object.respond_to_without_attributes?(:after_find)
object.send(:callback, :after_find)
end

if object.respond_to_without_attributes?(:after_initialize)
object.send(:callback, :after_initialize)
end

object
end
end
end
end
end
Loading

0 comments on commit 3299d0b

Please sign in to comment.