Skip to content
This repository has been archived by the owner on Feb 9, 2019. It is now read-only.

class and include_predecessor_id paramters added #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 32 additions & 12 deletions lib/heritage/active_record/acts_as_heir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,49 @@ module Heritage
module ActiveRecord
module ActsAsHeir

def child_of(parent_symbol)
acts_as_heir_of(parent_symbol)
def child_of(parent_symbol, params)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are not defaulting the params to anything, this will break backwards compatibility, and since this gem is in active use other places, this is not an option at the moment.

acts_as_heir_of(parent_symbol, params)
end

def acts_as_heir_of(predecessor_symbol)
def acts_as_heir_of(predecessor_symbol, params)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, the params not having a default value (say a hash) will break backwards compatibility.

extend ClassMethods
include InstanceMethods

class_attribute :_predecessor_klass, :_predecessor_symbol
self._predecessor_symbol = predecessor_symbol
self._predecessor_klass = Object.const_get(predecessor_symbol.to_s.capitalize)
if defined? params and not params.nil? and defined? params[:class] and not params[:class].nil? then
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I read it, you are asking two things: (A) Is the params hash present, and (B) Is the params[:class] present. So why not simply ask params[:class].present?

self._predecessor_klass = params[:class].constantize
else
self._predecessor_klass = predecessor_symbol.constantize
end

has_one :predecessor, :as => :heir, :class_name => predecessor_symbol.to_s.capitalize, :autosave => true, :dependent => :destroy
has_one :predecessor, :as => :heir, :class_name => self._predecessor_klass.to_s, :autosave => true, :dependent => :destroy

alias_method_chain :predecessor, :build

# Expose columns from the predecessor
self._predecessor_klass.columns.reject{|c| c.primary || c.name =~ /^heir_/}.map(&:name).each do |att|
define_method(att) do
predecessor.send(att)
self._predecessor_klass.columns.reject{|c| c.primary and not c.name.eql?("id") }.each do |att|
if att.primary then
if ( defined? params[:include_predecessor_id] and not params[:include_predecessor_id].nil? and params[:include_predecessor_id] == true) then
define_method(predecessor_symbol.to_s.underscore + "_id") do
predecessor.send(att.name)
end
end
next
end

if att.name =~ /^heir_/ then
define_method(predecessor_symbol.to_s.underscore + att.name) do
predecessor.send(att.name)
end
next
end

define_method(att.name) do
predecessor.send(att.name)
end
define_method("#{att}=") do |val|
predecessor.send("#{att}=",val)
define_method("#{att.name}=") do |val|
predecessor.send("#{att.name}=",val)
end
end

Expand All @@ -45,7 +65,7 @@ def acts_as_heir_of(predecessor_symbol)
self._predecessor_klass.get_heritage_exposed_methods.each do |method_symbol|
define_method(method_symbol.to_s) do |*args|
if args.length > 0
predecessor.send(method_symbol.to_s, args)
predecessor.send(method_symbol.to_s, args.flatten)
else
predecessor.send(method_symbol.to_s)
end
Expand All @@ -71,4 +91,4 @@ def touch_predecessor

end
end
end
end