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..8acfe62 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 @@ -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) diff --git a/lib/ruby_json_api_client/serializers/ams_serializer.rb b/lib/ruby_json_api_client/serializers/ams_serializer.rb index 5022c81..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] = {} @@ -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) }