-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import the plugin code from the FatJam Revisable branch.
Setup a simple gitignore. Some initial hoe configuration.
- Loading branch information
Rich Cavanaugh
committed
May 3, 2008
1 parent
3172787
commit 3299d0b
Showing
15 changed files
with
578 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
generators/revisable_migration/revisable_migration_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.