Skip to content
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
35 changes: 30 additions & 5 deletions lib/volt/models/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ def belongs_to(method_name, options = {})
local_key ||= options.fetch(:local_key, "#{method_name}_id")

# Add a field for the association_id
field(local_key)
field local_key, String

# initialize has_many association in foreign model
root.get(collection).class.has_many(self.class.name)

# getter
define_method(method_name) do
Expand All @@ -21,7 +24,7 @@ def belongs_to(method_name, options = {})
end

define_method(:"#{method_name}=") do |obj|
id = obj.is_a?(Fixnum) ? obj : obj.id
id = obj.is_a?(String) ? obj : obj.id

# Assign the current model's something_id to the object's id
set(local_key, id)
Expand Down Expand Up @@ -52,17 +55,39 @@ def has_many(method_name, options = {})

# has_one creates a method on the Volt::Model that returns a promise
# to get the associated model.
def has_one(method_name)
def has_one(method_name, options = {})
if method_name.plural?
raise NameError, "has_one takes a singluar association name"
end

collection ||= options.fetch(:collection, method_name).pluralize
foreign_key ||= options.fetch(:foreign_key, :id)
local_key ||= options.fetch(:local_key, "#{method_name}_id")

# Add a field for the association_id
field local_key, String

define_method(method_name) do
association_with_root_model('has_one') do |root|
key = self.class.to_s.underscore + '_id'
root.send(method_name.pluralize).where(key => id).first
lookup_key = get(local_key)

root.get(collection).where(foreign_key => lookup_key).first
end
end

define_method(:"#{method_name}=") do |obj|
id = obj.is_a?(String) ? obj : obj.id

# Delete old record
old_id = get(local_key)
if old_id and old_id != id
root.get(collection).where(foreign_key => old_id).first.then do |old_obj|
old_obj.parent.delete(old_obj)
end
end

set(local_key, id)
end
end
end

Expand Down