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..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: @@ -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..315a9e0 100644 --- a/lib/json_api_responders/config.rb +++ b/lib/json_api_responders/config.rb @@ -3,6 +3,8 @@ class Config attr_reader :required_options 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 +33,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