-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor association methods + fix issue with #find_by_id
#158
Open
tiagopog
wants to merge
4
commits into
active-hash:master
Choose a base branch
from
tiagopog: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
4 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 |
---|---|---|
@@ -1,8 +1,19 @@ | ||
module ActiveHash | ||
module Associations | ||
def fetch_associations(target_model:, method: :find_by, filter:, conditions: nil) | ||
base = apply_scope(model: target_model, conditions: conditions) | ||
base.send(method, filter) | ||
end | ||
|
||
module ActiveRecordExtensions | ||
def apply_scope(model:, conditions: nil) | ||
if conditions && model.respond_to?(:scoped) | ||
model.scoped(conditions: conditions) | ||
else | ||
model | ||
end | ||
end | ||
|
||
module ActiveRecordExtensions | ||
def belongs_to(*args) | ||
our_args = args.dup | ||
options = our_args.extract_options! | ||
|
@@ -34,7 +45,7 @@ def belongs_to_active_hash(association_id, options = {}) | |
options[:shortcuts] = [options[:shortcuts]] unless options[:shortcuts].kind_of?(Array) | ||
|
||
define_method(association_id) do | ||
options[:class_name].constantize.send("find_by_#{options[:primary_key]}", send(options[:foreign_key])) | ||
options[:class_name].constantize.send(:find_by, options[:primary_key] => send(options[:foreign_key])) | ||
end | ||
|
||
define_method("#{association_id}=") do |new_value| | ||
|
@@ -92,65 +103,72 @@ def self.included(base) | |
end | ||
|
||
module Methods | ||
def has_many(association_id, options = {}) | ||
def association_metadata(type, association_name, options = {}) | ||
association_names = association_name.to_s | ||
association_class = (options[:class_name] || association_names.classify).constantize | ||
{ | ||
target_model: association_class, | ||
primary_key: normalize_primary_key(type, association_class), | ||
foreign_key: normalize_foreign_key(type, association_name, options) | ||
}.merge(options) | ||
end | ||
|
||
define_method(association_id) do | ||
options = { | ||
:class_name => association_id.to_s.classify, | ||
:foreign_key => self.class.to_s.foreign_key, | ||
:primary_key => self.class.primary_key | ||
}.merge(options) | ||
|
||
klass = options[:class_name].constantize | ||
primary_key_value = send(options[:primary_key]) | ||
foreign_key = options[:foreign_key].to_sym | ||
|
||
if Object.const_defined?(:ActiveRecord) && ActiveRecord.const_defined?(:Relation) && klass < ActiveRecord::Relation | ||
klass.where(foreign_key => primary_key_value) | ||
elsif klass.respond_to?(:scoped) | ||
klass.scoped(:conditions => {foreign_key => primary_key_value}) | ||
def normalize_primary_key(type, association_class) | ||
primary_key = type == :belongs_to ? association_class.primary_key : name.constantize.primary_key | ||
primary_key.to_sym | ||
end | ||
|
||
def normalize_foreign_key(type, association_name, options) | ||
foreign_key = | ||
if type == :belongs_to | ||
options[:foreign_key] || (options[:class_name] || association_name).to_s.foreign_key | ||
else | ||
klass.where(foreign_key => primary_key_value) | ||
name.foreign_key | ||
end | ||
end | ||
foreign_key.to_sym | ||
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. |
||
end | ||
|
||
def has_one(association_id, options = {}) | ||
define_method(association_id) do | ||
options = { | ||
:class_name => association_id.to_s.classify, | ||
:foreign_key => self.class.to_s.foreign_key | ||
}.merge(options) | ||
|
||
scope = options[:class_name].constantize | ||
def has_many(association_name, options = {}) | ||
meta = association_metadata(:has_many, association_name, options) | ||
|
||
if scope.respond_to?(:scoped) && options[:conditions] | ||
scope = scope.scoped(:conditions => options[:conditions]) | ||
end | ||
scope.send("find_by_#{options[:foreign_key]}", id) | ||
define_method(association_name) do | ||
args = meta | ||
.slice(:target_model, :conditions) | ||
.merge( | ||
method: :where, | ||
filter: { meta[:foreign_key] => public_send(meta[:primary_key]) } | ||
) | ||
fetch_associations(args) | ||
end | ||
end | ||
|
||
def belongs_to(association_id, options = {}) | ||
def has_one(association_name, options = {}) | ||
meta = association_metadata(:has_one, association_name , options) | ||
|
||
options = { | ||
:class_name => association_id.to_s.classify, | ||
:foreign_key => association_id.to_s.foreign_key, | ||
:primary_key => "id" | ||
}.merge(options) | ||
define_method(association_name) do | ||
args = meta | ||
.slice(:target_model, :conditions) | ||
.merge(filter: { meta[:foreign_key] => public_send(meta[:primary_key]) }) | ||
fetch_associations(args) | ||
end | ||
end | ||
|
||
field options[:foreign_key].to_sym | ||
def belongs_to(association_name, options = {}) | ||
meta = association_metadata(:belongs_to, association_name, options) | ||
|
||
define_method(association_id) do | ||
options[:class_name].constantize.send("find_by_#{options[:primary_key]}", send(options[:foreign_key])) | ||
end | ||
field meta[:foreign_key].to_sym | ||
|
||
define_method("#{association_id}=") do |new_value| | ||
attributes[options[:foreign_key].to_sym] = new_value ? new_value.send(options[:primary_key]) : nil | ||
define_method(association_name) do | ||
args = meta | ||
.slice(:target_model) | ||
.merge(filter: { meta[:primary_key] => public_send(meta[:foreign_key]) }) | ||
fetch_associations(args) | ||
end | ||
|
||
define_method("#{association_name}=") do |new_value| | ||
attributes[meta[:foreign_key].to_sym] = new_value ? new_value.send(meta[:primary_key]) : nil | ||
end | ||
end | ||
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
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.
@tiagopog I'd like to recommend to invoke the primary_key method just once before of
.to_sym
calling. e.g: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.
Nice catch, @serradura 👍