Skip to content
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
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
108 changes: 63 additions & 45 deletions lib/associations/associations.rb
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!
Expand Down Expand Up @@ -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|
Expand Down Expand Up @@ -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

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:

klass = type == :belongs_to ? association_class : name.constantize
klass.primary_key.to_sym

Copy link
Author

Choose a reason for hiding this comment

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

Nice catch, @serradura 👍

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

Choose a reason for hiding this comment

The 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
2 changes: 1 addition & 1 deletion spec/associations/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SchoolStatus < ActiveHash::Base
@excluded_author = Author.create :city_id => 2
end

it "find the correct records" do
it "finds the correct records" do
City.has_many :authors
city = City.create :id => 1
city.authors.should == [@included_author_1, @included_author_2]
Expand Down