Skip to content

Commit 14ff694

Browse files
author
Lee Richmond
committed
Add appraisal, test rails 4+
1 parent 8b2c018 commit 14ff694

File tree

11 files changed

+531
-117
lines changed

11 files changed

+531
-117
lines changed

Appraisals

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
appraise "rails-4" do
2+
gem "rails", "~> 4.1"
3+
end
4+
5+
appraise "rails-5" do
6+
gem "rails", "~> 5.0"
7+
end

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ gemspec
55
gem 'active_model_serializers', git: 'https://github.com/richmolj/active_model_serializers.git'
66

77
group :test do
8+
gem 'appraisal'
89
gem 'guard'
910
gem 'guard-rspec'
1011
gem 'pry'

README.md

Lines changed: 11 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,20 @@
1-
[official documentation](https://bbgithub.dev.bloomberg.com/pages/InfrastructureExperience/jsonapi_compliable)
2-
3-
### Getting Started
4-
Please read below documentation related to [jsonapi](http://jsonapi.org/format/#document-resource-objects)
5-
6-
7-
### Adding JSONAPICompliable
8-
```
9-
class ApplicationController < ActionController::Base
10-
include JSONAPICompliable
11-
end
12-
13-
```
14-
### Define whitelist includes
15-
```
16-
class AuthorsController < ApplicationController
17-
jsonapi do
18-
includes whitelist: { index: [{ books: :genre }, :state] }
19-
20-
allow_filter :last_name
21-
22-
allow_filter :first_name, aliases: [:title], if: :can_filter_first_name?
23-
24-
allow_filter :first_name_prefix do |scope, filter|
25-
scope.where('first_name like ?', "#{filter}%")
26-
end
27-
end
28-
end
29-
```
30-
Below url requesting state/foo as includes.
31-
But foo include is ignored as it is not whitelist.
32-
>authors?include=state,foo
33-
34-
### Defining filter field
35-
36-
```
37-
class AuthorsController < ApplicationController
38-
jsonapi do
39-
allow_filter :last_name
40-
end
41-
end
42-
```
43-
will allow us make request as below
44-
>/authors?filter[first_name]=john
1+
### JsonapiCompliable
452

46-
### Defining filter field alias
3+
[official documentation](https://bbgithub.dev.bloomberg.com/pages/InfrastructureExperience/jsonapi_compliable)
474

48-
```
49-
class AuthorsController < ApplicationController
50-
jsonapi do
51-
allow_filter :last_name, aliases: [:name]
52-
end
53-
end
54-
```
55-
will allow us make request as below
56-
>/authors?filter[name]=john
5+
Supported Rails versions: >= 4.1
576

58-
### Adding guard to filter
7+
### Running tests
598

60-
```
61-
class AuthorsController < ApplicationController
62-
jsonapi do
63-
allow_filter :last_name if: :can_filter?
64-
end
9+
We support Rails >= 4.1. To do so, we use the [appraisal](https://github.com/thoughtbot/appraisal) gem. So, run:
6510

66-
def can_filter?
67-
true
68-
end
69-
end
11+
```bash
12+
$ bin/appraisal rails-4 bin/rspec
13+
$ bin/appraisal rails-5 bin/rspec
7014
```
7115

72-
### Defining default filter
73-
74-
```
75-
class AuthorsController < ApplicationController
76-
jsonapi do
77-
default_filter :first_name do |scope|
78-
scope.where(first_name: 'Willaim')
79-
end
80-
end
81-
end
82-
```
83-
if no filter provided below request will filter authors first_name='Willaim'
16+
Or run tests for all versions:
8417

85-
>/authors
86-
87-
### Deserialize requests params which are coming jsonapi format
88-
89-
```
90-
class AuthorsController < ApplicationController
91-
before_action :deserialize_jsonapi!, only: [:update, :create]
92-
end
93-
```
94-
incoming parameters
95-
```
96-
{
97-
data: {
98-
type: 'authors',
99-
attributes: {
100-
first_name: 'Stephen',
101-
last_name: 'King'
102-
},
103-
relationships: {
104-
books: {
105-
data: [
106-
{ type: 'books', attributes: { title: 'The Shining' } }
107-
]
108-
}
109-
}
110-
}
111-
}
112-
```
113-
will be deserialized to
114-
```
115-
{
116-
authors: {
117-
first_name: 'Stephen',
118-
last_name: 'King'
119-
books_attributes: [{
120-
title: 'The Shingin'
121-
}]
122-
}
123-
}
18+
```bash
19+
$ bin/appraisal bin/rspec
12420
```

bin/appraisal

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
#
4+
# This file was generated by Bundler.
5+
#
6+
# The application 'appraisal' is installed as part of a gem, and
7+
# this file is here to facilitate running it.
8+
#
9+
10+
require "pathname"
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12+
Pathname.new(__FILE__).realpath)
13+
14+
require "rubygems"
15+
require "bundler/setup"
16+
17+
load Gem.bin_path("appraisal", "appraisal")

gemfiles/rails_3.gemfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This file was generated by Appraisal
2+
3+
source "http://artprod.dev.bloomberg.com/artifactory/api/gems/rubygems/"
4+
5+
gem "appraisal"
6+
gem "active_model_serializers", :git => "https://github.com/richmolj/active_model_serializers.git"
7+
gem "rails", "~> 3.2"
8+
9+
gemspec :path => "../"

gemfiles/rails_4.gemfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This file was generated by Appraisal
2+
3+
source "http://artprod.dev.bloomberg.com/artifactory/api/gems/rubygems/"
4+
5+
gem "active_model_serializers", :git => "https://github.com/richmolj/active_model_serializers.git"
6+
gem "rails", "~> 4.0"
7+
8+
group :test do
9+
gem "appraisal"
10+
gem "guard"
11+
gem "guard-rspec"
12+
gem "pry"
13+
gem "pry-byebug"
14+
end
15+
16+
gemspec :path => "../"

0 commit comments

Comments
 (0)