Skip to content

Commit c9ff3d4

Browse files
committed
Initial commit
0 parents  commit c9ff3d4

17 files changed

+392
-0
lines changed

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp

.ruby-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ruby-2.1.0-p0

Gemfile

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in json-api-resources.gemspec
4+
gemspec
5+
6+
platforms :ruby do
7+
# sqlite3 1.3.9 does not work with rubinius 2.2.5:
8+
# https://github.com/sparklemotion/sqlite3-ruby/issues/122
9+
gem 'sqlite3', '1.3.8'
10+
end
11+
12+
platforms :mri do
13+
gem 'coveralls', require: false
14+
end
15+
16+
platforms :jruby do
17+
gem 'activerecord-jdbcsqlite3-adapter'
18+
end
19+
20+
version = ENV['RAILS_VERSION'] || '4.0.4'
21+
rails = case version
22+
when 'master'
23+
{:github => 'rails/rails'}
24+
else
25+
"~> #{version}"
26+
end
27+
gem 'rails', rails
28+
29+
group :test do
30+
gem 'minitest', '~> 4.7.5'
31+
gem 'minitest-rails'
32+
gem 'simplecov', :require => false
33+
34+
gem 'minitest-reporters'
35+
36+
# guard
37+
gem 'guard'
38+
gem 'guard-minitest'
39+
gem 'turn'
40+
gem 'rb-fsevent', '~> 0.9.1'
41+
end

LICENSE.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2014 Larry Gebhardt
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Json::Api::Resources
2+
3+
TODO: Write a gem description
4+
5+
## Installation
6+
7+
Add this line to your application's Gemfile:
8+
9+
gem 'json-api-resources'
10+
11+
And then execute:
12+
13+
$ bundle
14+
15+
Or install it yourself as:
16+
17+
$ gem install json-api-resources
18+
19+
## Usage
20+
21+
TODO: Write usage instructions here
22+
23+
## Contributing
24+
25+
1. Fork it ( http://github.com/<my-github-username>/json-api-resources/fork )
26+
2. Create your feature branch (`git checkout -b my-new-feature`)
27+
3. Commit your changes (`git commit -am 'Add some feature'`)
28+
4. Push to the branch (`git push origin my-new-feature`)
29+
5. Create new Pull Request

Rakefile

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env rake
2+
require "bundler/gem_tasks"
3+
require "rake/testtask"
4+
5+
desc 'Run tests'
6+
test_task = Rake::TestTask.new(:test) do |t|
7+
t.libs << 'test'
8+
t.pattern = 'test/**/*_test.rb'
9+
t.verbose = true
10+
end
11+
12+
task default: :test
13+
14+
desc 'Run tests in isolated processes'
15+
namespace :test do
16+
task :isolated do
17+
Dir[test_task.pattern].each do |file|
18+
cmd = ['ruby']
19+
test_task.libs.each { |l| cmd << '-I' << l }
20+
cmd << file
21+
sh cmd.join(' ')
22+
end
23+
end
24+
end

json-api-resources.gemspec

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# coding: utf-8
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'json/api/resources/version'
5+
6+
Gem::Specification.new do |spec|
7+
spec.name = 'json-api-resources'
8+
spec.version = JSON::API::Resources::VERSION
9+
spec.authors = ['Larry Gebhardt']
10+
spec.email = ['[email protected]']
11+
spec.summary = %q{Provides JSON API support.}
12+
spec.description = %q{Provides JSON API support.}
13+
spec.homepage = ''
14+
spec.license = 'MIT'
15+
16+
spec.files = `git ls-files -z`.split("\x0")
17+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19+
spec.require_paths = ['lib']
20+
21+
spec.add_development_dependency 'bundler', '~> 1.5'
22+
spec.add_development_dependency 'rake'
23+
spec.add_development_dependency 'minitest'
24+
25+
spec.add_dependency 'activemodel'
26+
spec.add_dependency 'activesupport'
27+
end

lib/json/api/controller.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module JSON
2+
module API
3+
class Controller
4+
5+
end
6+
end
7+
end

lib/json/api/defualt_serializer.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module JSON
2+
module API
3+
class DefualtSerializer
4+
5+
end
6+
end
7+
end

lib/json/api/resource.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module JSON
2+
module API
3+
class Resource
4+
5+
6+
end
7+
end
8+
end

lib/json/api/resources.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "json/api/resources/version"
2+
3+
module JSON
4+
module API
5+
module Resources
6+
# Your code goes here...
7+
end
8+
end
9+
end

lib/json/api/resources/version.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module JSON
2+
module API
3+
module Resources
4+
VERSION = "0.0.1"
5+
end
6+
end
7+
end

lib/json/api/serializer.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module JSON
2+
module API
3+
class Serializer
4+
include ActiveModel::Serializers::JSON
5+
6+
def initialize(object, options={})
7+
@object = object
8+
end
9+
end
10+
end
11+
end

notes.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Overview
2+
3+
Resources
4+
* implies a route, controllers, model, serializer
5+
* any level could be overridden
6+
7+
8+
Models
9+
10+
Controllers
11+
12+
Serializers
13+
* could be auto-generated by inspecting models
14+
15+
Routes
16+
17+
18+
## Resources
19+
20+
ApplicationResource < JSON::API::Resource
21+
22+
PostResource < ApplicationResource
23+
* hasMany :comments
24+
* hasOne :author
25+
* attribute :title
26+
27+
28+
CommentResource < ApplicationResource
29+
* hasOne :post
30+
* hasOne :author
31+
* hasOne :parentComment
32+
* attribute :content
33+
34+
35+
## Serializers
36+
37+
ApplicationSerializer < JSON::API::Serializer
38+
39+
PostSerializer < ApplicationSerializer
40+
41+
CommentSerializer < ApplicationSerializer
42+
43+
44+
## Controllers
45+
46+
ApplicationController < JSON::API::Controller
47+
48+
PostController < ApplicationController
49+
50+
CommentController < ApplicationController

test/fixtures/active_record.rb

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
require 'active_record'
2+
require 'json/api/serializer'
3+
4+
ActiveRecord::Base.establish_connection(
5+
:adapter => 'sqlite3',
6+
:database => ':memory:'
7+
)
8+
9+
ActiveRecord::Schema.define do
10+
create_table :ar_people, force: true do |t|
11+
t.string :name
12+
t.string :email
13+
t.datetime :date_joined
14+
t.timestamps
15+
end
16+
17+
create_table :ar_posts, force: true do |t|
18+
t.string :title
19+
t.text :body
20+
#t.integer :author_id
21+
t.belongs_to :ar_section, index: true
22+
t.timestamps
23+
end
24+
25+
create_table :ar_comments, force: true do |t|
26+
t.text :body
27+
t.belongs_to :ar_post, index: true
28+
#t.integer :author_id
29+
t.timestamps
30+
end
31+
32+
create_table :ar_tags, force: true do |t|
33+
t.string :name
34+
end
35+
36+
create_table :ar_sections, force: true do |t|
37+
t.string :name
38+
end
39+
40+
create_table :ar_posts_tags, force: true do |t|
41+
t.references :ar_post, :ar_tag, index: true
42+
end
43+
44+
create_table :ar_comments_tags, force: true do |t|
45+
t.references :ar_comment, :ar_tag, index: true
46+
end
47+
end
48+
49+
#class ARPerson < ActiveRecord::Base
50+
# has_many :ar_posts, class_name: 'ARPost'
51+
# has_many :ar_comments, class_name: 'ARComment'
52+
#end
53+
54+
class ARPost < ActiveRecord::Base
55+
#belongs_to :author, class_name: 'ARPerson', foreign_key: 'author_id'
56+
has_many :ar_comments, class_name: 'ARComment'
57+
has_and_belongs_to_many :ar_tags, class_name: 'ARTag', join_table: :ar_posts_tags
58+
belongs_to :ar_section, class_name: 'ARSection'
59+
end
60+
61+
class ARComment < ActiveRecord::Base
62+
#belongs_to :author, class_name: 'ARPerson', foreign_key: 'author_id'
63+
belongs_to :ar_post, class_name: 'ARPost'
64+
has_and_belongs_to_many :ar_tags, class_name: 'ARTag', join_table: :ar_comments_tags
65+
end
66+
67+
class ARTag < ActiveRecord::Base
68+
end
69+
70+
class ARSection < ActiveRecord::Base
71+
end
72+
73+
#class ARPersonSerializer < ActiveModel::Serializer
74+
# attributes :name, :email, :date_joined
75+
#end
76+
77+
class ARPostSerializer < JSON::API::Serializer
78+
#attributes :id, :title, :body
79+
#
80+
#has_many :ar_comments, :ar_tags
81+
#has_one :ar_section
82+
#has_one :author, class_name: 'ARPeople'
83+
end
84+
85+
class ARCommentSerializer < JSON::API::Serializer
86+
#attributes :id, :body
87+
#has_one :ar_post
88+
#has_many :ar_tags
89+
end
90+
91+
class ARTagSerializer < JSON::API::Serializer
92+
#attributes :id, :name
93+
end
94+
95+
class ARSectionSerializer < JSON::API::Serializer
96+
#attributes 'name'
97+
end
98+
99+
#a = ARPerson.create(name: 'Joe Author',
100+
# email: '[email protected]',
101+
# date_joined: DateTime.parse('2013-08-07 20:25:00 UTC +00:00'))
102+
103+
ARPost.create(title: 'New post',
104+
body: 'A body!!!',
105+
#author_id: a.id,
106+
ar_section: ARSection.create(name: 'ruby')).tap do |post|
107+
108+
short_tag = post.ar_tags.create(name: 'short')
109+
whiny_tag = post.ar_tags.create(name: 'whiny')
110+
happy_tag = ARTag.create(name: 'happy')
111+
112+
post.ar_comments.create(body: 'what a dumb post').tap do |comment|
113+
comment.ar_tags.concat happy_tag, whiny_tag
114+
end
115+
116+
post.ar_comments.create(body: 'i liked it').tap do |comment|
117+
comment.ar_tags.concat happy_tag, short_tag
118+
end
119+
end

0 commit comments

Comments
 (0)