Skip to content

Commit

Permalink
* Implement 'branch_source' as a class method instead of as a
Browse files Browse the repository at this point in the history
  belongs_to association. AFAIK, there is no way to escape the
  default_scope of the revisable class that only selects rows with
  revisable_is_current==true.

  This means that branch_source will work for any parent, whether it
  is an active object or a revision.
  • Loading branch information
mheffner authored and rich committed Oct 18, 2009
1 parent 13ad33d commit 15635e7
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/acts_as_revisable/acts/common.rb
Expand Up @@ -21,10 +21,9 @@ def self.included(base) #:nodoc:
base.instance_eval do
define_callbacks :before_branch, :after_branch
has_many :branches, (revisable_options.revision_association_options || {}).merge({:class_name => base.name, :foreign_key => :revisable_branched_from_id})

belongs_to :branch_source, :class_name => base.name, :foreign_key => :revisable_branched_from_id

after_save :execute_blocks_after_save
end
end
end

# Executes the blocks stored in an accessor after a save.
Expand Down Expand Up @@ -98,6 +97,18 @@ def branch!(*args)
def is_branching!(value=true) #:nodoc:
set_revisable_state(:branching, value)
end

# XXX: This should be done with a "belongs_to" but the default
# scope on the target class prevents the find with revisions.
def branch_source
self[:branch_source] ||= if self[:revisable_branched_from_id]
self.class.find(self[:revisable_branched_from_id],
:with_revisions => true)
else
nil
end

end

# Returns true if the _record_ (not just this instance
# of the record) is currently being branched.
Expand Down

0 comments on commit 15635e7

Please sign in to comment.