From 3f55673acfc78b0c2cfca6cbe71275461864218e Mon Sep 17 00:00:00 2001 From: Ken Hill Date: Thu, 8 Jun 2017 14:50:35 -0400 Subject: [PATCH 1/3] add support for remote_class specification --- lib/ruby_json_api_client/adapters/rest_adapter.rb | 4 ++-- lib/ruby_json_api_client/base.rb | 7 +++++++ lib/ruby_json_api_client/serializers/ams_serializer.rb | 4 ++-- spec/support/classes.rb | 6 ++++++ spec/unit/adapters/rest_spec.rb | 10 ++++++++++ spec/unit/base_spec.rb | 6 +++++- 6 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/ruby_json_api_client/adapters/rest_adapter.rb b/lib/ruby_json_api_client/adapters/rest_adapter.rb index c6138ac..bc21140 100644 --- a/lib/ruby_json_api_client/adapters/rest_adapter.rb +++ b/lib/ruby_json_api_client/adapters/rest_adapter.rb @@ -25,7 +25,7 @@ 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] @@ -33,7 +33,7 @@ def single_path(klass, params = {}) end def collection_path(klass, params) - name = klass.name + name = klass.remote_class plural = ActiveSupport::Inflector.pluralize(name) "#{@namespace}/#{plural.underscore}" end diff --git a/lib/ruby_json_api_client/base.rb b/lib/ruby_json_api_client/base.rb index b0b80cf..12c234e 100644 --- a/lib/ruby_json_api_client/base.rb +++ b/lib/ruby_json_api_client/base.rb @@ -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 diff --git a/lib/ruby_json_api_client/serializers/ams_serializer.rb b/lib/ruby_json_api_client/serializers/ams_serializer.rb index 5022c81..1edfd7c 100644 --- a/lib/ruby_json_api_client/serializers/ams_serializer.rb +++ b/lib/ruby_json_api_client/serializers/ams_serializer.rb @@ -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], @@ -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) diff --git a/spec/support/classes.rb b/spec/support/classes.rb index 63925c5..8492a31 100644 --- a/spec/support/classes.rb +++ b/spec/support/classes.rb @@ -27,3 +27,9 @@ class Thing < RubyJsonApiClient::Base class Nothing < RubyJsonApiClient::Base end + +module LocalNamespace + class TestClass < RubyJsonApiClient::Base + remote_class "SomeOtherClass" + end +end diff --git a/spec/unit/adapters/rest_spec.rb b/spec/unit/adapters/rest_spec.rb index ec3e8bb..394a3c4 100644 --- a/spec/unit/adapters/rest_spec.rb +++ b/spec/unit/adapters/rest_spec.rb @@ -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 @@ -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 diff --git a/spec/unit/base_spec.rb b/spec/unit/base_spec.rb index c70e738..0759b57 100644 --- a/spec/unit/base_spec.rb +++ b/spec/unit/base_spec.rb @@ -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 @@ -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) } From 90e45b549c1a33374b540649a7733d27aa2c84fc Mon Sep 17 00:00:00 2001 From: Ken Hill Date: Thu, 15 Jun 2017 12:41:42 -0400 Subject: [PATCH 2/3] user remote_class in to_data --- lib/ruby_json_api_client/serializers/ams_serializer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby_json_api_client/serializers/ams_serializer.rb b/lib/ruby_json_api_client/serializers/ams_serializer.rb index 1edfd7c..a6e0e77 100644 --- a/lib/ruby_json_api_client/serializers/ams_serializer.rb +++ b/lib/ruby_json_api_client/serializers/ams_serializer.rb @@ -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] = {} From 0c5063386aac9e443025171e9c08eb3d2a9e8cda Mon Sep 17 00:00:00 2001 From: Ken Hill Date: Fri, 23 Jun 2017 13:57:03 -0400 Subject: [PATCH 3/3] accept options param in save method --- lib/ruby_json_api_client/base.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/ruby_json_api_client/base.rb b/lib/ruby_json_api_client/base.rb index 12c234e..8acfe62 100644 --- a/lib/ruby_json_api_client/base.rb +++ b/lib/ruby_json_api_client/base.rb @@ -158,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)