Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added origin and origin_attribute to ActiveRecord::Relation [#835 sta…
…te:resolved]
- Loading branch information
Showing
2 changed files
with
29 additions
and
0 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
28 changes: 28 additions & 0 deletions
28
hobo/lib/hobo/extensions/active_record/relation_with_origin.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module ActiveRecord | ||
|
||
class Relation | ||
attr_accessor :origin, :origin_attribute | ||
end | ||
|
||
module Associations | ||
class AssociationCollection | ||
|
||
def scoped_with_origin | ||
relation = scoped_without_origin | ||
relation.origin = @owner | ||
relation.origin_attribute = @reflection.name | ||
relation | ||
end | ||
alias_method_chain :scoped, :origin | ||
|
||
def method_missing_with_origin(method, *args) | ||
res = method_missing_without_origin(method, *args) | ||
res.origin = @owner if res.respond_to?(:origin) | ||
res.origin_attribute = @reflection.name if res.respond_to?(:origin_attribute) | ||
res | ||
end | ||
alias_method_chain :method_missing, :origin | ||
|
||
end | ||
end | ||
end |