This repository has been archived by the owner on Feb 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
class and include_predecessor_id paramters added #6
Open
davidkovaccs
wants to merge
3
commits into
dipth:master
Choose a base branch
from
davidkovaccs:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
acts_as_heir_of(parent_symbol, params) | ||
end | ||
|
||
def acts_as_heir_of(predecessor_symbol) | ||
def acts_as_heir_of(predecessor_symbol, params) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, the |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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 | ||
|
||
|
@@ -71,4 +91,4 @@ def touch_predecessor | |
|
||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.