Skip to content

Commit aebdb55

Browse files
committed
Initial commit.
0 parents  commit aebdb55

File tree

19 files changed

+415
-0
lines changed

19 files changed

+415
-0
lines changed

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
*.gem
2+
*.rbc
3+
/.config
4+
/coverage/
5+
/InstalledFiles
6+
/pkg/
7+
/spec/reports/
8+
/spec/examples.txt
9+
/test/tmp/
10+
/test/version_tmp/
11+
/tmp/
12+
13+
## Specific to RubyMotion:
14+
.dat*
15+
.repl_history
16+
build/
17+
18+
## Documentation cache and generated files:
19+
/.yardoc/
20+
/_yardoc/
21+
/doc/
22+
/rdoc/
23+
24+
## Environment normalization:
25+
/.bundle/
26+
/vendor/bundle
27+
/lib/bundler/man/
28+
29+
Gemfile.lock
30+
.ruby-version
31+
.ruby-gemset
32+
Gemfile.local
33+
34+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35+
.rvmrc

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: ruby
2+
sudo: false
3+
before_install:
4+
- bundle update
5+
rvm:
6+
- 2.1
7+
- 2.2
8+
- 2.3.0
9+
- 2.3.1
10+
- ruby-head
11+
12+
matrix:
13+
allow_failures:
14+
- rvm: ruby-head

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# jsonapi-rails
2+
Rails integration for [jsonapi-rb](https://github.com/jsonapi-rb/jsonapi-rb).
3+
4+
## Status
5+
6+
[![Gem Version](https://badge.fury.io/rb/jsonapi-rails.svg)](https://badge.fury.io/rb/jsonapi-rails)
7+
[![Build Status](https://secure.travis-ci.org/jsonapi-rb/rails.svg?branch=master)](http://travis-ci.org/jsonapi-rb/rails?branch=master)
8+
9+
## Installation
10+
11+
Add the following to your application's Gemfile:
12+
```ruby
13+
gem 'jsonapi-rails'
14+
```
15+
And then execute:
16+
```
17+
$ bundle
18+
```
19+
Or install it manually as:
20+
```
21+
$ gem install jsonapi-rails
22+
```
23+
24+
## Usage
25+
26+
TODO
27+
28+
## License
29+
30+
jsonapi-rails is released under the [MIT License](http://www.opensource.org/licenses/MIT).

Rakefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'bundler/gem_tasks'
2+
require 'rspec/core/rake_task'
3+
4+
RSpec::Core::RakeTask.new
5+
6+
task default: :spec
7+
task test: :spec

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.1.1.beta1

jsonapi-rails.gemspec

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
version = File.read(File.expand_path('../VERSION', __FILE__)).strip
2+
3+
Gem::Specification.new do |spec|
4+
spec.name = 'jsonapi-rails'
5+
spec.version = version
6+
spec.author = ['L. Preston Sego III', 'Lucas Hosseini']
7+
8+
spec.summary = 'Rails plugin for (de)serialization of JSONAPI resources'
9+
spec.description = 'Efficiently build and consume JSONAPI documents.'
10+
spec.homepage = 'https://github.com/jsonapi-rb/rails'
11+
spec.license = 'MIT'
12+
13+
spec.files = Dir['README.md', 'lib/**/*']
14+
spec.require_path = 'lib'
15+
16+
spec.add_dependency 'jsonapi-renderer', '0.1.1.beta2'
17+
spec.add_dependency 'jsonapi-parser', '0.1.1.beta3'
18+
spec.add_dependency 'jsonapi-serializable', '0.1.1.beta2'
19+
spec.add_dependency 'jsonapi-deserializable', '0.1.1.beta3'
20+
21+
spec.add_development_dependency 'activerecord', '>=5'
22+
spec.add_development_dependency 'sqlite3', '>= 1.3.12'
23+
spec.add_development_dependency 'rake', '>=0.9'
24+
spec.add_development_dependency 'rspec', '~>3.4'
25+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Description:
2+
Generates a deserializable resource for the given model.
3+
4+
Example:
5+
`rails generate jsonapi:deserializable User`
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module Jsonapi
2+
class DeserializableGenerator < ::Rails::Generators::NamedBase
3+
source_root File.expand_path('../templates', __FILE__)
4+
5+
# TODO(beauby): Implement generator-level whitelisting.
6+
# TODO(beauby): Implement versioning.
7+
8+
def copy_deserializable_file
9+
template 'deserializable.rb.erb',
10+
File.join('app/deserializable', class_path,
11+
"deserializable_#{file_name}.rb")
12+
end
13+
14+
private
15+
16+
def model_klass
17+
# TODO(beauby): Ensure the model class exists.
18+
class_name.safe_constantize
19+
end
20+
21+
def attr_names
22+
attrs = model_klass.new.attribute_names - %w(id created_at updated_at)
23+
fk_attrs = model_klass.reflect_on_all_associations(:belongs_to)
24+
.map(&:foreign_key)
25+
attrs - fk_attrs
26+
end
27+
28+
def has_one_rels
29+
has_one = model_klass.reflect_on_all_associations(:has_one)
30+
belongs_to = model_klass.reflect_on_all_associations(:belongs_to)
31+
32+
has_one + belongs_to
33+
end
34+
35+
def has_one_id_field_name(rel_name)
36+
"#{rel_name}_id"
37+
end
38+
39+
def has_one_type_field_name(rel_name)
40+
"#{rel_name}_type"
41+
end
42+
43+
def has_many_rels
44+
model_klass.reflect_on_all_associations(:has_many)
45+
end
46+
47+
def has_many_id_field_name(rel_name)
48+
"#{rel_name.to_s.singularize}_ids"
49+
end
50+
51+
def has_many_type_field_name(rel_name)
52+
"#{rel_name.to_s.singularize}_types"
53+
end
54+
end
55+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<% module_namespacing do -%>
2+
class Deserializable<%= class_name %> < JSONAPI::Deserializable::Resource
3+
id
4+
5+
<% attr_names.each do |attr| -%>
6+
attribute :<%= attr %>
7+
<% end -%>
8+
9+
<% has_one_rels.each do |reflection| -%>
10+
has_one :<%= reflection.name %> do
11+
field :<%= has_one_id_field_name(reflection.name) %> do |rel|
12+
rel['data'] && rel['data']['id']
13+
end
14+
<% if reflection.polymorphic? -%>
15+
field :<%= has_one_type_field_name(reflection.name) %> do
16+
rel['data'] && rel['data']['type']
17+
end
18+
<% end -%>
19+
end
20+
<% end -%>
21+
<% has_many_rels.each do |reflection| -%>
22+
has_many :<%= reflection.name %> do
23+
field :<%= has_many_id_field_name(reflection.name) %> do |rel|
24+
rel['data'].map { |ri| ri['id'] }
25+
end
26+
<% if reflection.polymorphic? -%>
27+
field :<%= has_one_type_field_name(reflection.name) %> do
28+
rel['data'].map { |ri| ri['type'] }
29+
end
30+
<% end -%>
31+
end
32+
<% end -%>
33+
end
34+
<% end -%>

0 commit comments

Comments
 (0)