Skip to content

Commit

Permalink
Rubocop: Consistent spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Aug 31, 2015
1 parent c998a97 commit 884d643
Show file tree
Hide file tree
Showing 47 changed files with 278 additions and 339 deletions.
278 changes: 123 additions & 155 deletions .rubocop_todo.yml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions active_model_serializers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
spec.platform = Gem::Platform::RUBY
spec.authors = ['Steve Klabnik']
spec.email = ['[email protected]']
spec.summary = %q{Conventions-based JSON generation for Rails.}
spec.description = %q{ActiveModel::Serializers allows you to generate your JSON in an object-oriented and convention-driven manner.}
spec.summary = 'Conventions-based JSON generation for Rails.'
spec.description = 'ActiveModel::Serializers allows you to generate your JSON in an object-oriented and convention-driven manner.'
spec.homepage = 'https://github.com/rails-api/active_model_serializers'
spec.license = 'MIT'

Expand Down
2 changes: 1 addition & 1 deletion lib/action_controller/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def serialization_scope
end

def get_serializer(resource, options = {})
if ! use_adapter?
if !use_adapter?
warn 'ActionController::Serialization#use_adapter? has been removed. '\
"Please pass 'adapter: false' or see ActiveSupport::SerializableResource#serialize"
options[:adapter] = false
Expand Down
2 changes: 0 additions & 2 deletions lib/active_model/serializable_resource.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'set'
module ActiveModel
class SerializableResource

ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter])

def initialize(resource, options = {})
Expand Down Expand Up @@ -79,6 +78,5 @@ def serializer?
ActiveModelSerializers.silence_warnings do
attr_reader :resource, :adapter_opts, :serializer_opts
end

end
end
1 change: 0 additions & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Serializer
include Configuration
include Associations


# Matches
# "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"
# AND
Expand Down
7 changes: 3 additions & 4 deletions lib/active_model/serializer/adapter/fragment_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module ActiveModel
class Serializer
class Adapter
class FragmentCache

attr_reader :serializer

def initialize(adapter, serializer, options)
Expand Down Expand Up @@ -35,7 +34,7 @@ def fetch

def cached_attributes(klass, serializers)
attributes = serializer.class._attributes
cached_attributes = (klass._cache_only) ? klass._cache_only : attributes.reject {|attr| klass._cache_except.include?(attr) }
cached_attributes = (klass._cache_only) ? klass._cache_only : attributes.reject { |attr| klass._cache_except.include?(attr) }
non_cached_attributes = attributes - cached_attributes

cached_attributes.each do |attribute|
Expand All @@ -60,15 +59,15 @@ def fragment_serializer(name, klass)
Object.const_set cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(cached)
Object.const_set non_cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(non_cached)

klass._cache_options ||= {}
klass._cache_options ||= {}
klass._cache_options[:key] = klass._cache_key if klass._cache_key

cached.constantize.cache(klass._cache_options)

cached.constantize.fragmented(serializer)
non_cached.constantize.fragmented(serializer)

serializers = {cached: cached, non_cached: non_cached}
serializers = { cached: cached, non_cached: non_cached }
cached_attributes(klass, serializers)
serializers
end
Expand Down
1 change: 0 additions & 1 deletion lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def serializable_hash(options = nil)
def fragment_cache(cached_hash, non_cached_hash)
Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash)
end

end
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/active_model/serializer/adapter/json/fragment_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ class Serializer
class Adapter
class Json < Adapter
class FragmentCache

def fragment_cache(cached_hash, non_cached_hash)
non_cached_hash.merge cached_hash
end

end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def add_relationships(resource, name, serializers)
resource[:relationships][name][:data] += serializers.map { |serializer| { type: serializer.json_api_type, id: serializer.id.to_s } }
end

def add_relationship(resource, name, serializer, val=nil)
def add_relationship(resource, name, serializer, val = nil)
resource[:relationships] ||= {}
resource[:relationships][name] = { data: val }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ class Serializer
class Adapter
class JsonApi < Adapter
class FragmentCache

def fragment_cache(root, cached_hash, non_cached_hash)
hash = {}
core_cached = cached_hash.first
core_non_cached = non_cached_hash.first
no_root_cache = cached_hash.delete_if {|key, value| key == core_cached[0] }
no_root_non_cache = non_cached_hash.delete_if {|key, value| key == core_non_cached[0] }
no_root_cache = cached_hash.delete_if { |key, value| key == core_cached[0] }
no_root_non_cache = non_cached_hash.delete_if { |key, value| key == core_non_cached[0] }
cached_resource = (core_cached[1]) ? core_cached[1].deep_merge(core_non_cached[1]) : core_non_cached[1]
hash = (root) ? { root => cached_resource } : cached_resource

hash.deep_merge no_root_non_cache.deep_merge no_root_cache
end

end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/active_model/serializer/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class Serializer
# Association.new(:comments, CommentSummarySerializer)
#
Association = Struct.new(:name, :serializer, :options) do

# @return [Symbol]
#
def key
Expand Down
6 changes: 2 additions & 4 deletions lib/active_model/serializer/fieldset.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module ActiveModel
class Serializer
class Fieldset

def initialize(fields, root = nil)
@root = root
@raw_fields = fields
Expand All @@ -16,15 +15,15 @@ def fields_for(serializer)
fields[key.to_sym] || fields[key.pluralize.to_sym]
end

private
private

ActiveModelSerializers.silence_warnings do
attr_reader :raw_fields, :root
end

def parsed_fields
if raw_fields.is_a?(Hash)
raw_fields.inject({}) { |h,(k,v)| h[k.to_sym] = v.map(&:to_sym); h}
raw_fields.inject({}) { |h, (k, v)| h[k.to_sym] = v.map(&:to_sym); h }
elsif raw_fields.is_a?(Array)
if root.nil?
raise ArgumentError, 'The root argument must be specified if the fileds argument is an array.'
Expand All @@ -36,7 +35,6 @@ def parsed_fields
{}
end
end

end
end
end
2 changes: 0 additions & 2 deletions lib/active_model/serializer/lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ module ActiveModel::Serializer::Lint
# always return +{}+, and the tests would pass. It is up to you to ensure
# that the values are semantically meaningful.
module Tests

# Passes if the object responds to <tt>serializable_hash</tt> and if it takes
# zero or one arguments.
# Fails otherwise.
Expand Down Expand Up @@ -126,6 +125,5 @@ def resource
def assert_instance_of(result, name)
assert result.instance_of?(name), "#{result} should be an instance of #{name}"
end

end
end
1 change: 0 additions & 1 deletion lib/active_model_serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def silence_warnings
ensure
$VERBOSE = verbose
end

end

require 'active_model'
Expand Down
2 changes: 1 addition & 1 deletion test/action_controller/explicit_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_render_array_using_explicit_serializer_and_custom_serializers
{ 'title' => 'New Post',
'body' => 'Body',
'id' => assigns(:post).id,
'comments' => [{'id' => 1}, {'id' => 2}],
'comments' => [{ 'id' => 1 }, { 'id' => 2 }],
'author' => { 'id' => assigns(:author).id }
}
]
Expand Down
10 changes: 5 additions & 5 deletions test/action_controller/json_api/linked_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def render_collection_without_include

def render_collection_with_include
setup_post
render json: [@post], include: ['author', 'comments'], adapter: :json_api
render json: [@post], include: %w(author comments), adapter: :json_api
end
end

Expand Down Expand Up @@ -107,7 +107,7 @@ def test_render_resource_with_nested_has_many_include
},
'relationships' => {
'posts' => { 'data' => [] },
'roles' => { 'data' => [{ 'type' =>'roles', 'id' => '1' }, { 'type' =>'roles', 'id' => '2' }] },
'roles' => { 'data' => [{ 'type' => 'roles', 'id' => '1' }, { 'type' => 'roles', 'id' => '2' }] },
'bio' => { 'data' => nil }
}
}, {
Expand All @@ -119,7 +119,7 @@ def test_render_resource_with_nested_has_many_include
'slug' => 'admin-1'
},
'relationships' => {
'author' => { 'data' => { 'type' =>'authors', 'id' => '1' } }
'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } }
}
}, {
'id' => '2',
Expand All @@ -130,7 +130,7 @@ def test_render_resource_with_nested_has_many_include
'slug' => 'colab-2'
},
'relationships' => {
'author' => { 'data' => { 'type' =>'authors', 'id' => '1' } }
'author' => { 'data' => { 'type' => 'authors', 'id' => '1' } }
}
}
]
Expand Down Expand Up @@ -172,7 +172,7 @@ def test_render_collection_with_missing_nested_has_many_include
end

def has_type?(collection, value)
collection.detect { |i| i['type'] == value}
collection.detect { |i| i['type'] == value }
end
end
end
Expand Down
44 changes: 22 additions & 22 deletions test/action_controller/json_api/pagination_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,59 +47,59 @@ def render_array_without_pagination_links
tests PaginationTestController

def test_render_pagination_links_with_will_paginate
expected_links = { 'self'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
'first'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'prev'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'next'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
'last'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"}
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'prev' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }

get :render_pagination_using_will_paginate, page: { number: 2, size: 1 }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end

def test_render_only_last_and_next_pagination_links
expected_links = { 'self'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
'next'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
'last'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2"}
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" }
get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end

def test_render_pagination_links_with_kaminari
expected_links = { 'self'=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
'first'=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'prev'=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'next'=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
'last'=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"}
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'next' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
get :render_pagination_using_kaminari, page: { number: 2, size: 1 }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end

def test_render_only_prev_and_first_pagination_links
expected_links = { 'self'=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
'first'=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'prev'=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1"}
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1" }
get :render_pagination_using_kaminari, page: { number: 3, size: 1 }
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end

def test_render_only_last_and_next_pagination_links_with_additional_params
expected_links = { 'self'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
'next'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
'last'=>"#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional"}
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" }
get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }, teste: 'additional'
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
end

def test_render_only_prev_and_first_pagination_links_with_additional_params
expected_links = { 'self'=>"#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
'first'=>"#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
'prev'=>"#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional"}
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional" }
get :render_pagination_using_kaminari, page: { number: 3, size: 1 }, teste: 'additional'
response = JSON.parse(@response.body)
assert_equal expected_links, response['links']
Expand Down
Loading

0 comments on commit 884d643

Please sign in to comment.