Skip to content

added mongoid support #12

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/pkg/
/spec/reports/
/tmp/
.idea
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/json_api_responders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions lib/json_api_responders/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions lib/json_api_responders/errors.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'json_api_responders/config'

module JsonApiResponders
module Errors
class UnknownHTTPStatus < StandardError
Expand Down Expand Up @@ -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
37 changes: 37 additions & 0 deletions spec/lib/json_api_responders/config/config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'

describe JsonApiResponders::Config do
let(:config) { JsonApiResponders.config }

Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/support/fake_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ class RecordNotFound < StandardError
end
end

module Mongoid
module Errors
class DocumentNotFound < StandardError
end
end
end

module ActionController
class ParameterMissing
end
Expand Down