Skip to content

Commit

Permalink
Merge pull request #122 from cerebris/prep_1_0
Browse files Browse the repository at this point in the history
Prep 1.0
  • Loading branch information
dgeb committed Mar 18, 2015
2 parents cd6f986 + 2ed0498 commit d147f7c
Show file tree
Hide file tree
Showing 33 changed files with 865 additions and 536 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ test/tmp
test/version_tmp
tmp
coverage
test/log
test/log
test_db
test_db-journal
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
language: ruby
env:
- "RAILS_VERSION=4.0"
- "RAILS_VERSION=4.1"
- "RAILS_VERSION=4.2"
rvm:
- 2.0
- 2.1
Expand Down
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ platforms :jruby do
gem 'activerecord-jdbcsqlite3-adapter'
end

version = ENV['RAILS_VERSION'] || '4.0.4'
version = ENV['RAILS_VERSION'] || 'default'
rails = case version
when 'master'
{:github => 'rails/rails'}
when 'default'
'>= 4.2'
else
"~> #{version}"
end
Expand Down
1 change: 1 addition & 0 deletions lib/jsonapi-resources.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'jsonapi/resource'
require 'jsonapi/resource_controller'
require 'jsonapi/resources/version'
require 'jsonapi/configuration'
require 'jsonapi/paginator'
Expand Down
10 changes: 1 addition & 9 deletions lib/jsonapi/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@ def initialize(name, options={})
@name = name.to_s
@options = options
@acts_as_set = options.fetch(:acts_as_set, false) == true
@key = options[:key] ? options[:key].to_sym : nil

if @key.nil?
@foreign_key = options[:foreign_key ] ? options[:foreign_key ].to_sym : nil
else
# :nocov:
warn '[DEPRECATION] `key` is deprecated in associations. Please use `foreign_key` instead.'
# :nocov:
end
@foreign_key = options[:foreign_key ] ? options[:foreign_key ].to_sym : nil
end

def primary_key
Expand Down
7 changes: 1 addition & 6 deletions lib/jsonapi/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,11 @@ def errors
end

class InvalidLinksObject < Error
attr_accessor :value
def initialize(value)
@value = value
end

def errors
[JSONAPI::Error.new(code: JSONAPI::INVALID_LINKS_OBJECT,
status: :bad_request,
title: 'Invalid Links Object',
detail: "#{value} is not a valid Links Object.")]
detail: 'Data is not a valid Links Object.')]
end
end

Expand Down
4 changes: 1 addition & 3 deletions lib/jsonapi/paginator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ def initialize(params)
end

def apply(relation)
# :nocov:
relation
# :nocov:
# relation
end

class << self
Expand Down
30 changes: 6 additions & 24 deletions lib/jsonapi/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def setup(params)
parse_pagination(params[:page])
when 'get_related_resource', 'get_related_resources'
@source_klass = Resource.resource_for(params.require(:source))
@source_id = params.require(@source_klass._as_parent_key)
@source_id = @source_klass.verify_key(params.require(@source_klass._as_parent_key), @context)
parse_fields(params[:fields])
parse_include(params[:include])
parse_filters(params[:filter])
Expand Down Expand Up @@ -232,7 +232,7 @@ def parse_has_one_links_object(raw)
end

if !raw.is_a?(Hash) || raw.length != 2 || !(raw.has_key?('type') && raw.has_key?('id'))
raise JSONAPI::Exceptions::InvalidLinksObject.new(raw)
raise JSONAPI::Exceptions::InvalidLinksObject.new
end

{
Expand All @@ -243,23 +243,18 @@ def parse_has_one_links_object(raw)

def parse_has_many_links_object(raw)
if raw.nil?
raise JSONAPI::Exceptions::InvalidLinksObject.new(raw)
raise JSONAPI::Exceptions::InvalidLinksObject.new
end

links_object = {}
if raw.is_a?(Hash)
if raw.length != 2 || !(raw.has_key?('type') && raw.has_key?('ids')) || !(raw['ids'].is_a?(Array))
raise JSONAPI::Exceptions::InvalidLinksObject.new(raw)
end
links_object[raw['type']] = raw['ids']
elsif raw.is_a?(Array)
if raw.is_a?(Array)
raw.each do |link|
link_object = parse_has_one_links_object(link)
links_object[link_object[:type]] ||= []
links_object[link_object[:type]].push(link_object[:id])
end
else
raise JSONAPI::Exceptions::InvalidLinksObject.new(raw)
raise JSONAPI::Exceptions::InvalidLinksObject.new
end
links_object
end
Expand Down Expand Up @@ -312,10 +307,6 @@ def parse_params(params, allowed_fields)
checked_has_many_associations[param] = association_resource.verify_keys(keys, @context)
end
end
else
# :nocov:
raise JSONAPI::Exceptions::InvalidLinksObject.new(key)
# :nocov:
end
end
else
Expand Down Expand Up @@ -355,23 +346,14 @@ def parse_add_association_operation(data, association_type, parent_key)
association = resource_klass._association(association_type)

if association.is_a?(JSONAPI::Association::HasMany)
ids = data.require(:ids)
type = data.require(:type)

object_params = {links: {association.name => {'type' => type, 'ids' => ids}}}
object_params = {links: {association.name => data}}
verified_param_set = parse_params(object_params, @resource_klass.updateable_fields(@context))

@operations.push JSONAPI::CreateHasManyAssociationOperation.new(resource_klass,
parent_key,
association_type,
verified_param_set[:has_many].values[0])
else
# :nocov:
@errors.concat(JSONAPI::Exceptions::InvalidLinksObject.new(:data).errors)
# :nocov:
end
rescue ActionController::ParameterMissing => e
@errors.concat(JSONAPI::Exceptions::ParameterMissing.new(e.param).errors)
end

def parse_update_association_operation(data, association_type, parent_key)
Expand Down
14 changes: 0 additions & 14 deletions lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,6 @@ def filter(attr)
@_allowed_filters.add(attr.to_sym)
end

def key(key)
# :nocov:
warn '[DEPRECATION] `key` is deprecated. Please use `primary_key` instead.'
@_primary_key = key.to_sym
# :nocov:
end

def primary_key(key)
@_primary_key = key.to_sym
end
Expand Down Expand Up @@ -456,13 +449,6 @@ def _model_name
@_model_name ||= self.name.demodulize.sub(/Resource$/, '')
end

def _key
# :nocov:
warn '[DEPRECATION] `_key` is deprecated. Please use `_primary_key` instead.'
_primary_key
# :nocov:
end

def _primary_key
@_primary_key ||= :id
end
Expand Down
8 changes: 1 addition & 7 deletions lib/jsonapi/resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def show
def show_association
association_type = params[:association]

parent_key = params[resource_klass._as_parent_key]
parent_key = resource_klass.verify_key(params[resource_klass._as_parent_key], context)

parent_resource = resource_klass.find_by_key(parent_key, context: context)

Expand All @@ -66,9 +66,7 @@ def show_association

render json: serializer.serialize_to_links_hash(parent_resource, association)
rescue => e
# :nocov:
handle_exceptions(e)
# :nocov:
end

def create
Expand Down Expand Up @@ -153,9 +151,7 @@ def ensure_correct_media_type
raise JSONAPI::Exceptions::UnsupportedMediaTypeError.new(request.content_type)
end
rescue => e
# :nocov:
handle_exceptions(e)
# :nocov:
end

def setup_request
Expand All @@ -165,9 +161,7 @@ def setup_request
})
render_errors(@request.errors) unless @request.errors.empty?
rescue => e
# :nocov:
handle_exceptions(e)
# :nocov:
end

def setup_response
Expand Down
63 changes: 46 additions & 17 deletions lib/jsonapi/resource_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,19 @@ def serialize_to_hash(source)
end

def serialize_to_links_hash(source, requested_association)
{data: link_object(source, requested_association, true)}
if requested_association.is_a?(JSONAPI::Association::HasOne)
data = has_one_linkage(source, requested_association)
else
data = has_many_linkage(source, requested_association)
end

{
links: {
self: self_link(source, requested_association),
related: related_link(source, requested_association)
},
data: data
}
end

private
Expand Down Expand Up @@ -224,29 +236,46 @@ def format_route(route)
@route_formatter.format(route.to_s)
end

def link_object_has_one(source, association)
route = association.name
def self_link(source, association)
"#{self_href(source)}/links/#{format_route(association.name)}"
end

def related_link(source, association)
"#{self_href(source)}/#{format_route(association.name)}"
end

def has_one_linkage(source, association)
linkage = {}
linkage_id = foreign_key_value(source, association)
if linkage_id
linkage[:type] = format_route(association.type)
linkage[:id] = linkage_id
end
linkage
end

def has_many_linkage(source, association)
linkage = []
linkage_ids = foreign_key_value(source, association)
linkage_ids.each do |linkage_id|
linkage.append({type: format_route(association.type), id: linkage_id})
end
linkage
end

def link_object_has_one(source, association)
link_object_hash = {}
link_object_hash[:self] = "#{self_href(source)}/links/#{format_route(route)}"
link_object_hash[:related] = "#{self_href(source)}/#{format_route(route)}"
# ToDo: Get correct formatting figured out
link_object_hash[:type] = format_route(association.type)
link_object_hash[:id] = foreign_key_value(source, association)
link_object_hash[:self] = self_link(source, association)
link_object_hash[:related] = related_link(source, association)
link_object_hash[:linkage] = has_one_linkage(source, association)
link_object_hash
end

def link_object_has_many(source, association, include_linkage)
route = association.name

link_object_hash = {}
link_object_hash[:self] = "#{self_href(source)}/links/#{format_route(route)}"
link_object_hash[:related] = "#{self_href(source)}/#{format_route(route)}"
if include_linkage
# ToDo: Get correct formatting figured out
link_object_hash[:type] = format_route(association.type)
link_object_hash[:ids] = foreign_key_value(source, association)
end
link_object_hash[:self] = self_link(source, association)
link_object_hash[:related] = related_link(source, association)
link_object_hash[:linkage] = has_many_linkage(source, association) if include_linkage
link_object_hash
end

Expand Down
3 changes: 2 additions & 1 deletion test/config/database.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test:
adapter: sqlite3
database: ":memory:"
database: "test_db"
# database: ":memory:"
pool: 5
timeout: 5000
Loading

0 comments on commit d147f7c

Please sign in to comment.