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

#270 - Return a query builder for has many associations instead of array #274

Closed
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
12 changes: 11 additions & 1 deletion lib/json_api_client/associations/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ def has_many(attr_name, options = {})
end

class Association < BaseAssociation
def query_builder(url)
association_class.query_builder.new(
association_class,
association_class.requestor_class.new(association_class, url)
)
end

def data(url)
query_builder(url)
end
end
end
end
end
end
7 changes: 4 additions & 3 deletions lib/json_api_client/query/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ module JsonApiClient
module Query
class Builder

attr_reader :klass
attr_reader :klass, :requestor
delegate :key_formatter, to: :klass

def initialize(klass)
def initialize(klass, requestor = nil)
@klass = klass
@requestor = requestor || klass.requestor
@primary_key = nil
@pagination_params = {}
@path_params = {}
Expand Down Expand Up @@ -97,7 +98,7 @@ def find(args = {})
@primary_key = args
end

klass.requestor.get(params)
requestor.get(params)
end

def method_missing(method_name, *args, &block)
Expand Down
10 changes: 6 additions & 4 deletions lib/json_api_client/query/requestor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ class Requestor
extend Forwardable
include Helpers::URI

def initialize(klass)
def initialize(klass, path = nil)
@klass = klass
@path = path
end

# expects a record
Expand Down Expand Up @@ -45,14 +46,15 @@ def custom(method_name, options, params)

protected

attr_reader :klass
attr_reader :klass, :path
def_delegators :klass, :connection

def resource_path(parameters)
base_path = path || klass.path(parameters)
if resource_id = parameters[klass.primary_key]
File.join(klass.path(parameters), encode_part(resource_id))
File.join(base_path, encode_part(resource_id))
else
klass.path(parameters)
base_path
end
end

Expand Down
8 changes: 6 additions & 2 deletions test/unit/association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,12 @@ def test_has_many_fetches_relationship
]
}.to_json)
owner = Owner.find(1).first
properties = owner.properties
assert_equal(Array, properties.class)
properties_query_builder = owner.properties
properties = properties_query_builder.to_a
assert_equal(JsonApiClient::Query::Builder, properties_query_builder.class)
assert_equal(Property, properties_query_builder.klass)
assert_equal(JsonApiClient::Query::Requestor, properties_query_builder.requestor.class)
assert_equal(JsonApiClient::ResultSet, properties.class)
assert_equal("314 150th Ave", properties.last.address)
end

Expand Down