From 2aed1d0f4236571a0bb9a5b49c791f3084457429 Mon Sep 17 00:00:00 2001 From: Aldo Ziflaj Date: Wed, 31 May 2017 14:13:45 +0200 Subject: [PATCH 1/3] added mongoid support --- .gitignore | 1 + README.md | 5 ++- lib/json_api_responders.rb | 6 ++- lib/json_api_responders/config.rb | 15 ++++++++ lib/json_api_responders/errors.rb | 16 ++++++++ .../json_api_responders/config/config_spec.rb | 37 +++++++++++++++++++ spec/support/fake_classes.rb | 7 ++++ 7 files changed, 85 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 0cb6eeb..d2d0efd 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ /pkg/ /spec/reports/ /tmp/ +.idea \ No newline at end of file diff --git a/README.md b/README.md index 8c37e43..0c96cac 100644 --- a/README.md +++ b/README.md @@ -58,13 +58,14 @@ This method requires HTTP status code and an optional parameter explaining the e ## Configuration -Currently you can only configure which options are required to be passed through the `respond_with` method. These required options are categorized by the controller's actions. Bellow you can find an example: +Currently you can configure which options are required to be passed through the `respond_with` method and the datadabase adapter. These required options are categorized by the controller's actions. Below you can find an example: JsonApiResponders.configure do |config| config.required_options = { index: [:each_serializer], create: [:serializer] } + config.adapter = :mongoid # default is :active_record end ... def create @@ -74,6 +75,8 @@ Currently you can only configure which options are required to be passed through If `:serializer` was left out of the above `respond_with` method you would see the `JsonApiResponders::Errors::RequiredOptionMissingError` be raised. +Currently, the only database adaptors are Active Record and Mongoid. + ## Responses ### index diff --git a/lib/json_api_responders.rb b/lib/json_api_responders.rb index 0e82701..4701cb3 100644 --- a/lib/json_api_responders.rb +++ b/lib/json_api_responders.rb @@ -17,7 +17,11 @@ def self.config end def self.included(base) - base.rescue_from ActiveRecord::RecordNotFound, with: :record_not_found! + if self.config.adapter == :active_record + base.rescue_from ActiveRecord::RecordNotFound, with: :record_not_found! + elsif self.config.adapter == :mongoid + base.rescue_from Mongoid::Errors::DocumentNotFound, with: :record_not_found! + end base.rescue_from ActionController::ParameterMissing, with: :parameter_missing! end diff --git a/lib/json_api_responders/config.rb b/lib/json_api_responders/config.rb index 942da48..2ab526d 100644 --- a/lib/json_api_responders/config.rb +++ b/lib/json_api_responders/config.rb @@ -1,8 +1,11 @@ module JsonApiResponders class Config attr_reader :required_options + # attr_reader :adapter DEFAULT_RENDER_METHOD = :json RENDER_METHODS = [:jsonapi, :json] + DEFAULT_ADAPTER = :active_record + ADAPTERS = [:active_record, :mongoid] def required_options=(opts = {}) @required_options = opts @@ -31,6 +34,18 @@ def render_method=(render_method) end end + def adapter + @adapter || DEFAULT_ADAPTER + end + + def adapter=(adapter) + if ADAPTERS.include?(adapter) + @adapter = adapter + else + raise JsonApiResponders::Errors::InvalidDatabaseAdapterError, adapter + end + end + private def action(options) diff --git a/lib/json_api_responders/errors.rb b/lib/json_api_responders/errors.rb index 46ff038..ff7deed 100644 --- a/lib/json_api_responders/errors.rb +++ b/lib/json_api_responders/errors.rb @@ -1,3 +1,5 @@ +require 'json_api_responders/config' + module JsonApiResponders module Errors class UnknownHTTPStatus < StandardError @@ -65,5 +67,19 @@ def message 'Status is not defined' end end + + class InvalidDatabaseAdapterError < StandardError + attr_reader :adapter + + def initialize(adapter) + @adapter = adapter + super(message) + end + + def message + "Unknown database adapter '#{adapter}'.\n"\ + "Accepted adapters are #{JsonApiResponders::Config::ADAPTERS.join(', ')}" + end + end end end diff --git a/spec/lib/json_api_responders/config/config_spec.rb b/spec/lib/json_api_responders/config/config_spec.rb index 32fe4fb..602f5c4 100644 --- a/spec/lib/json_api_responders/config/config_spec.rb +++ b/spec/lib/json_api_responders/config/config_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' + describe JsonApiResponders::Config do let(:config) { JsonApiResponders.config } @@ -36,6 +37,42 @@ end end + # Database Adapter specs + + context 'when config database adapter is not set' do + after { clear_config } + + it 'adapter is set to :active_record' do + expect(config.adapter).to eq JsonApiResponders::Config::DEFAULT_ADAPTER + end + end + + context 'when config database adapter is :mongoid' do + before do + JsonApiResponders.configure do |config| + config.adapter = :mongoid + end + end + after { clear_config } + + it 'adapter is set to :mongoid' do + expect(config.adapter).to eq :mongoid + end + end + + context 'when config database adapter is :invalid' do + after { clear_config } + subject do + JsonApiResponders.configure do |config| + config.adapter = :invalid + end + end + + it 'raises an error' do + expect{ subject }.to raise_error(JsonApiResponders::Errors::InvalidDatabaseAdapterError) + end + end + private def clear_config diff --git a/spec/support/fake_classes.rb b/spec/support/fake_classes.rb index 95caafc..59c603b 100644 --- a/spec/support/fake_classes.rb +++ b/spec/support/fake_classes.rb @@ -3,6 +3,13 @@ class RecordNotFound < StandardError end end +module Mongoid + module Errors + class DocumentNotFound < StandardError + end + end +end + module ActionController class ParameterMissing end From 52eba0b2e4a3e16c3d25db1201bf0f744f704bee Mon Sep 17 00:00:00 2001 From: Aldo Ziflaj Date: Wed, 31 May 2017 14:17:37 +0200 Subject: [PATCH 2/3] Update config.rb I slipped a comment --- lib/json_api_responders/config.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/json_api_responders/config.rb b/lib/json_api_responders/config.rb index 2ab526d..315a9e0 100644 --- a/lib/json_api_responders/config.rb +++ b/lib/json_api_responders/config.rb @@ -1,7 +1,6 @@ module JsonApiResponders class Config attr_reader :required_options - # attr_reader :adapter DEFAULT_RENDER_METHOD = :json RENDER_METHODS = [:jsonapi, :json] DEFAULT_ADAPTER = :active_record From 2a174d1c65ae3a2e1d0f436ec4823aaf6b728e53 Mon Sep 17 00:00:00 2001 From: Aldo Ziflaj Date: Tue, 26 Sep 2017 16:47:39 +0200 Subject: [PATCH 3/3] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0c96cac..ae2f0a9 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ This gem gives a few convenient methods for working with JSONAPI. It is inspired Add this line to your application's Gemfile: ```ruby -gem 'json_api_responders' +gem 'json_api_responders', git: 'https://github.com/aziflaj/json_api_responders.git', branch: 'mongoid' ``` And then execute: