Skip to content

Commit

Permalink
Remove the json_schema dependency
Browse files Browse the repository at this point in the history
We do not want to include the json_schema as a dependency of the gem, since the
idea is to the json_schema to be used only by the test helper.

This patch load the json_schema only inside the `assert_response_schema` test
helper and raises a good error message when the the json_schema gem is not
installed.
  • Loading branch information
maurogeorge committed Jan 11, 2016
1 parent 6ca94a6 commit 7cf0526
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
3 changes: 1 addition & 2 deletions active_model_serializers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ Gem::Specification.new do |spec|
# activesupport
# activemodel

spec.add_runtime_dependency 'json_schema'

# Soft dependency for pagination
spec.add_development_dependency 'kaminari', ' ~> 0.16.3'
spec.add_development_dependency 'will_paginate', '~> 3.0', '>= 3.0.7'
Expand All @@ -56,4 +54,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'timecop', '~> 0.7'
spec.add_development_dependency 'minitest-reporters'
spec.add_development_dependency 'grape', ['>= 0.13', '< 1.0']
spec.add_development_dependency 'json_schema'
end
6 changes: 6 additions & 0 deletions docs/howto/test.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# How to test

## Dependencies

To use the `assert_response_schema` you need to have the
[`json_schema`](https://github.com/brandur/json_schema) on your Gemfile. Please
add it to your Gemfile and run `$ bundle install`.

## Minitest test helpers

ActiveModelSerializers provides a `assert_response_schema` method to be used on your controller tests to
Expand Down
9 changes: 7 additions & 2 deletions lib/active_model_serializers/test/schema.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'json_schema'

module ActiveModelSerializers
module Test
module Schema
Expand All @@ -23,6 +21,7 @@ class AssertResponseSchema
attr_reader :schema_path, :response, :message

def initialize(schema_path, response, message)
require_json_schema!
@response = response
@schema_path = schema_path || schema_path_default
@message = message
Expand Down Expand Up @@ -92,6 +91,12 @@ def load_json_file(path)
rescue Errno::ENOENT
raise MissingSchema, "No Schema file at #{schema_full_path}"
end

def require_json_schema!
require 'json_schema'
rescue LoadError
raise LoadError, "You don't have json_schema installed in your application. Please add it to your Gemfile and run bundle install"
end
end
end
end
Expand Down

0 comments on commit 7cf0526

Please sign in to comment.