Skip to content
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
42 changes: 42 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}

## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
# $ mkdir config
# $ mv Guardfile config/
# $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"

guard :minitest do
# with Minitest::Unit
watch(%r{^test/(.*)\/?(.*)_test\.rb$})
watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
watch(%r{^test/test_helper\.rb$}) { 'test' }

# with Minitest::Spec
# watch(%r{^spec/(.*)_spec\.rb$})
# watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
# watch(%r{^spec/spec_helper\.rb$}) { 'spec' }

# Rails 4
# watch(%r{^app/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
# watch(%r{^app/controllers/application_controller\.rb$}) { 'test/controllers' }
# watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" }
# watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
# watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" }
# watch(%r{^test/.+_test\.rb$})
# watch(%r{^test/test_helper\.rb$}) { 'test' }

# Rails < 4
# watch(%r{^app/controllers/(.*)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" }
# watch(%r{^app/helpers/(.*)\.rb$}) { |m| "test/helpers/#{m[1]}_test.rb" }
# watch(%r{^app/models/(.*)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" }
end
4 changes: 4 additions & 0 deletions jsonapi-resources.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rake'
spec.add_development_dependency 'minitest'
spec.add_development_dependency 'minitest-spec-rails'
spec.add_development_dependency 'minitest-focus'
spec.add_development_dependency 'minitest-reporters'
spec.add_development_dependency 'awesome_print'
spec.add_development_dependency 'simplecov'
spec.add_development_dependency 'pry'
spec.add_development_dependency 'guard'
spec.add_development_dependency 'guard-minitest'
spec.add_dependency 'rails', '>= 4.0'
end
41 changes: 41 additions & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@
t.string :serial_number
t.integer :person_id
end

create_table :parents, force: true do |t|
t.string :name
end

create_table :children, force: true do |t|
t.string :name
t.integer :parent_id
end
end

### MODELS
Expand Down Expand Up @@ -440,6 +449,16 @@ class Product < ActiveRecord::Base
has_one :picture, as: :imageable
end

class Parent < ActiveRecord::Base
has_one :child

accepts_nested_attributes_for :child
end

class Child < ActiveRecord::Base
belongs_to :parent
end

### OperationsProcessor
class CountingActiveRecordOperationsProcessor < ActiveRecordOperationsProcessor
after_find_operation do
Expand Down Expand Up @@ -866,6 +885,28 @@ def _save
end
end

class ParentResource < JSONAPI::Resource
attribute :name

has_one :child

private

def child_attributes=(attributes)
@model.child_attributes = attributes
end

def self.creatable_fields(context)
super + [:child_attributes]
end
end

class ChildResource < JSONAPI::Resource
attribute :name

has_one :parent
end

class PlanetResource < JSONAPI::Resource
attribute :name
attribute :description
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/children.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a:
id: 1
name: Daniel Jr.
parent_id: 1
3 changes: 3 additions & 0 deletions test/fixtures/parents.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a:
id: 1
name: Daniel
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
require 'rails/all'
require 'rails/test_help'
require 'minitest/mock'
require 'minitest/reporters'
require 'minitest/focus'
require 'jsonapi-resources'
require 'pry'
require 'awesome_print'

require File.expand_path('../helpers/value_matchers', __FILE__)
require File.expand_path('../helpers/assertions', __FILE__)
Expand Down Expand Up @@ -256,6 +259,8 @@ class CatResource < JSONAPI::Resource
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: true)]

class Minitest::Test
include Helpers::Assertions
include Helpers::ValueMatchers
Expand Down
32 changes: 31 additions & 1 deletion test/unit/operation/operations_processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,36 @@ def test_create_multiple_resources
assert_equal(Planet.count, count + 3)
end

def test_create_single_resource_with_nested_attributes
op = TestOperationsProcessor.new()

child_count = Child.count
parent_count = Parent.count

operations = [
JSONAPI::CreateResourceOperation.new(ParentResource,
data: {
attributes: {
'name' => 'earth',
'child_attributes' => {
'name' => 'earth jr.'
}
}
})
]

request = JSONAPI::Request.new
request.operations = operations

operation_results = op.process(request)

assert_kind_of(JSONAPI::OperationResults, operation_results)
assert_equal(:created, operation_results.results[0].code)
assert_equal(operation_results.results.size, 1)
assert_equal(Child.count, child_count + 1)
assert_equal(Parent.count, parent_count + 1)
end

def test_replace_to_one_relationship
op = JSONAPI::OperationsProcessor.new()

Expand Down Expand Up @@ -520,7 +550,7 @@ def test_safe_run_callback_catch_fail
error = StandardError.new

callback = ->(error) { nil.explosions}
result = op.send(:safe_run_callback, callback, error)
result = op.send(:safe_run_callback, callback, error)

assert_instance_of(JSONAPI::ErrorsOperationResult, result)
assert_equal(result.code, 500)
Expand Down