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

Relationship cleanup #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions lib/railsapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
require 'railsapi/resource_relationships'
require 'railsapi/resource_attributes'
require 'railsapi/resource_fields'
require 'railsapi/resource_records'

module RailsAPI
class Resource
include ResourceCallbacks
include ResourceRelationships
include ResourceAttributes
include ResourceFields
include ResourceRecords

attr_reader :context

Expand Down Expand Up @@ -310,6 +308,10 @@ def module_path

private

def is_active_record_model?
_model_class && _model_class.ancestors.collect{|ancestor| ancestor.name}.include?('ActiveRecord::Base')
end

def check_reserved_resource_name(type, name)
if [:ids, :types, :hrefs, :links].include?(type)
warn "[NAME COLLISION] `#{name}` is a reserved resource name."
Expand Down
27 changes: 0 additions & 27 deletions lib/railsapi/resource_records.rb

This file was deleted.

39 changes: 25 additions & 14 deletions lib/railsapi/resource_relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _add_relationship(klass, *attrs)
check_reserved_relationship_name(relationship_name)

# Initialize from an ActiveRecord model's properties
if _model_class && _model_class.ancestors.collect{|ancestor| ancestor.name}.include?('ActiveRecord::Base')
if is_active_record_model?
model_association = _model_class.reflect_on_association(relationship_name)
if model_association
options[:class_name] ||= model_association.class_name
Expand All @@ -82,21 +82,29 @@ def _add_relationship(klass, *attrs)

@_relationships[relationship_name] = relationship = klass.new(relationship_name, options)

associated_records_method_name = case relationship
when RailsAPI::Relationship::ToOne then "record_for_#{relationship_name}"
when RailsAPI::Relationship::ToMany then "records_for_#{relationship_name}"
end

foreign_key = relationship.foreign_key

define_method "#{foreign_key}=" do |value|
@model.method("#{foreign_key}=").call(value)
end unless method_defined?("#{foreign_key}=")

define_method associated_records_method_name do
relationship = self.class._relationships[relationship_name]
relation_name = relationship.relation_name(context: @context)
records_for(relation_name)
# Resources for relationships are returned through the dynamically generated method named for the
# relationship (for example `has_many :comments` will create a method named `comments` on the resource). This
# method must return a single Resource for a `has_one` and an array of Resources for a `has_many`
# relationship.
#
# In addition ActiveRecord Relation records for each relationship are retrieved through a dynamically
# generated method named for the relationship (`record(s)_for_<relationship_name>). This in turn calls
# the standard `records_for` method, which can be overridden for common code related to retrieving related
# records.

associated_records_method_name = case relationship
when RailsAPI::Relationship::ToOne then "record_for_#{relationship_name}"
when RailsAPI::Relationship::ToMany then "records_for_#{relationship_name}"
end

define_method associated_records_method_name do |options = {}|
records_for_relationship(relationship_name, options)
end unless method_defined?(associated_records_method_name)

if relationship.is_a?(RailsAPI::Relationship::ToOne)
Expand Down Expand Up @@ -151,11 +159,8 @@ def _add_relationship(klass, *attrs)
relationship = self.class._relationships[relationship_name]

resource_klass = relationship.resource_klass
records = public_send(associated_records_method_name)

records = resource_klass.apply_filters(records, options)
records = resource_klass.apply_sort(records, options)
records = resource_klass.apply_pagination(records, options)
records = public_send(associated_records_method_name, options)

return records.collect do |record|
if relationship.polymorphic?
Expand Down Expand Up @@ -219,6 +224,12 @@ def records_for(relation_name)
_model.public_send relation_name
end

def records_for_relationship(relationship_name, _options = {})
relationship = self.class._relationships[relationship_name]
relation_name = relationship.relation_name(context: @context)
records_for(relation_name)
end

private
def _create_to_many_links(relationship_type, relationship_key_values)
relationship = self.class._relationships[relationship_type]
Expand Down