Skip to content

add support for remote_class specification #5

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

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
4 changes: 2 additions & 2 deletions lib/ruby_json_api_client/adapters/rest_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ def initialize(options = {})
end

def single_path(klass, params = {})
name = klass.name
name = klass.remote_class
plural = ActiveSupport::Inflector.pluralize(name)
path = plural.underscore
id = params[:id]
"#{@namespace}/#{path}/#{id}"
end

def collection_path(klass, params)
name = klass.name
name = klass.remote_class
plural = ActiveSupport::Inflector.pluralize(name)
"#{@namespace}/#{plural.underscore}"
end
Expand Down
13 changes: 11 additions & 2 deletions lib/ruby_json_api_client/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def self.fields
@_fields ||= Set.new [_identifier]
end

def self.remote_class(name=nil)
if name.present?
@_remote_class = name
end
@_remote_class || self.to_s
end

def self.attributes
fields.reduce({}) do |attributes, field|
attributes[field] = nil
Expand Down Expand Up @@ -151,8 +158,10 @@ def reload
RubyJsonApiClient::Store.instance.reload(self)
end

def save
perform_validations() && RubyJsonApiClient::Store.instance.save(self)
def save(options={})
options.fetch(:validate, true) ?
perform_validations() && RubyJsonApiClient::Store.instance.save(self) :
RubyJsonApiClient::Store.instance.save(self)
end

def update_attributes(data)
Expand Down
6 changes: 3 additions & 3 deletions lib/ruby_json_api_client/serializers/ams_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def transform(response)
end

def to_data(model)
key = model.class.to_s.underscore.downcase
key = model.class.remote_class.underscore.downcase
id_field = model.class._identifier
data = {}
data[key] = {}
Expand Down Expand Up @@ -85,7 +85,7 @@ def _create_model(klass, data)

def extract_single(klass, id, response)
return nil if response.nil?
name = klass.to_s.underscore
name = klass.remote_class.underscore
data = transform(response)

assert data[name],
Expand All @@ -105,7 +105,7 @@ def extract_single(klass, id, response)
end

def extract_many(klass, response, key = nil)
key = klass.to_s.underscore if key.nil?
key = klass.remote_class.underscore if key.nil?
plural = ActiveSupport::Inflector.pluralize(key)

data = transform(response)
Expand Down
6 changes: 6 additions & 0 deletions spec/support/classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ class Thing < RubyJsonApiClient::Base

class Nothing < RubyJsonApiClient::Base
end

module LocalNamespace
class TestClass < RubyJsonApiClient::Base
remote_class "SomeOtherClass"
end
end
10 changes: 10 additions & 0 deletions spec/unit/adapters/rest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
subject { adapter.single_path(CellPhone, { id: 3 }) }
it { should == "testing/cell_phones/3" }
end

context LocalNamespace::TestClass do
subject { adapter.single_path(LocalNamespace::TestClass, {id: 4}) }
it { should == "testing/some_other_classes/4"}
end
end

describe :collection_path do
Expand All @@ -48,6 +53,11 @@
subject { adapter.collection_path(CellPhone, {}) }
it { should == "testing/cell_phones" }
end

context LocalNamespace::TestClass do
subject { adapter.collection_path(LocalNamespace::TestClass, {}) }
it { should == "testing/some_other_classes" }
end
end

describe :find do
Expand Down
6 changes: 5 additions & 1 deletion spec/unit/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Person < RubyJsonApiClient::Base
subject { Person.new(firstname: 'ryan').valid? }
it { should eq(true) }
end

it "should prevent creating invalid records" do
person = Person.create({})
expect(person.persisted?).to eq false
Expand All @@ -48,6 +48,10 @@ class Person < RubyJsonApiClient::Base
end
end

describe :remote_class do

end

describe :has_field? do
context "a class with no fields" do
subject { Nothing.has_field?(:nope) }
Expand Down