From 12e2987420127ec07606e99c733a0e29c1c38a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stas=20SU=C8=98COV?= Date: Sun, 8 Mar 2020 16:56:38 +0000 Subject: [PATCH] Rubocop cleanups. --- .../instrumentation/serializable_hash.rb | 4 +- .../instrumentation/serialized_json.rb | 4 +- .../skylight/normalizers/base.rb | 4 +- .../skylight/normalizers/serializable_hash.rb | 6 +- .../skylight/normalizers/serialized_json.rb | 6 +- lib/fast_jsonapi/object_serializer.rb | 39 ++++---- lib/fast_jsonapi/relationship.rb | 39 ++++---- lib/fast_jsonapi/scalar.rb | 6 +- lib/fast_jsonapi/serialization_core.rb | 13 +-- lib/fast_jsonapi/version.rb | 2 +- .../serializer/serializer_generator.rb | 6 +- spec/lib/extensions/active_record_spec.rb | 21 ++--- spec/lib/helpers_spec.rb | 2 +- .../as_notifications_negative_spec.rb | 9 +- .../instrumentation/as_notifications_spec.rb | 11 +-- .../object_serializer_attribute_param_spec.rb | 56 ++++++------ spec/lib/object_serializer_caching_spec.rb | 2 +- .../object_serializer_class_methods_spec.rb | 23 ++--- spec/lib/object_serializer_fields_spec.rb | 2 +- .../lib/object_serializer_inheritance_spec.rb | 11 +-- .../lib/object_serializer_polymorphic_spec.rb | 2 +- ...ject_serializer_relationship_links_spec.rb | 39 ++++---- ...ject_serializer_relationship_param_spec.rb | 36 ++++---- ...lizer_relationship_serializer_proc_spec.rb | 10 +-- spec/lib/object_serializer_spec.rb | 90 +++++++++---------- spec/lib/object_serializer_struct_spec.rb | 2 +- spec/lib/serialization_core_spec.rb | 12 +-- spec/shared/contexts/group_context.rb | 5 +- spec/shared/contexts/movie_context.rb | 46 +++++----- 29 files changed, 231 insertions(+), 277 deletions(-) diff --git a/lib/fast_jsonapi/instrumentation/serializable_hash.rb b/lib/fast_jsonapi/instrumentation/serializable_hash.rb index eb86a60..3363797 100644 --- a/lib/fast_jsonapi/instrumentation/serializable_hash.rb +++ b/lib/fast_jsonapi/instrumentation/serializable_hash.rb @@ -2,14 +2,12 @@ require 'active_support/notifications' module FastJsonapi module ObjectSerializer - - alias_method :serializable_hash_without_instrumentation, :serializable_hash + alias serializable_hash_without_instrumentation serializable_hash def serializable_hash ActiveSupport::Notifications.instrument(SERIALIZABLE_HASH_NOTIFICATION, { name: self.class.name }) do serializable_hash_without_instrumentation end end - end end diff --git a/lib/fast_jsonapi/instrumentation/serialized_json.rb b/lib/fast_jsonapi/instrumentation/serialized_json.rb index 4fa3cb5..3b00269 100644 --- a/lib/fast_jsonapi/instrumentation/serialized_json.rb +++ b/lib/fast_jsonapi/instrumentation/serialized_json.rb @@ -2,14 +2,12 @@ require 'active_support/notifications' module FastJsonapi module ObjectSerializer - - alias_method :serialized_json_without_instrumentation, :serialized_json + alias serialized_json_without_instrumentation serialized_json def serialized_json ActiveSupport::Notifications.instrument(SERIALIZED_JSON_NOTIFICATION, { name: self.class.name }) do serialized_json_without_instrumentation end end - end end diff --git a/lib/fast_jsonapi/instrumentation/skylight/normalizers/base.rb b/lib/fast_jsonapi/instrumentation/skylight/normalizers/base.rb index 83c971d..1b4cb1d 100644 --- a/lib/fast_jsonapi/instrumentation/skylight/normalizers/base.rb +++ b/lib/fast_jsonapi/instrumentation/skylight/normalizers/base.rb @@ -2,6 +2,6 @@ require 'skylight' SKYLIGHT_NORMALIZER_BASE_CLASS = begin ::Skylight::Core::Normalizers::Normalizer -rescue NameError - ::Skylight::Normalizers::Normalizer + rescue NameError + ::Skylight::Normalizers::Normalizer end diff --git a/lib/fast_jsonapi/instrumentation/skylight/normalizers/serializable_hash.rb b/lib/fast_jsonapi/instrumentation/skylight/normalizers/serializable_hash.rb index 16a3c62..a24839f 100644 --- a/lib/fast_jsonapi/instrumentation/skylight/normalizers/serializable_hash.rb +++ b/lib/fast_jsonapi/instrumentation/skylight/normalizers/serializable_hash.rb @@ -6,15 +6,13 @@ module FastJsonapi module Skylight module Normalizers class SerializableHash < SKYLIGHT_NORMALIZER_BASE_CLASS - register FastJsonapi::ObjectSerializer::SERIALIZABLE_HASH_NOTIFICATION CAT = "view.#{FastJsonapi::ObjectSerializer::SERIALIZABLE_HASH_NOTIFICATION}".freeze - def normalize(trace, name, payload) - [ CAT, payload[:name], nil ] + def normalize(_trace, _name, payload) + [CAT, payload[:name], nil] end - end end end diff --git a/lib/fast_jsonapi/instrumentation/skylight/normalizers/serialized_json.rb b/lib/fast_jsonapi/instrumentation/skylight/normalizers/serialized_json.rb index 3945c27..b57cbad 100644 --- a/lib/fast_jsonapi/instrumentation/skylight/normalizers/serialized_json.rb +++ b/lib/fast_jsonapi/instrumentation/skylight/normalizers/serialized_json.rb @@ -6,15 +6,13 @@ module FastJsonapi module Skylight module Normalizers class SerializedJson < SKYLIGHT_NORMALIZER_BASE_CLASS - register FastJsonapi::ObjectSerializer::SERIALIZED_JSON_NOTIFICATION CAT = "view.#{FastJsonapi::ObjectSerializer::SERIALIZED_JSON_NOTIFICATION}".freeze - def normalize(trace, name, payload) - [ CAT, payload[:name], nil ] + def normalize(_trace, _name, payload) + [CAT, payload[:name], nil] end - end end end diff --git a/lib/fast_jsonapi/object_serializer.rb b/lib/fast_jsonapi/object_serializer.rb index 2902780..18bbd72 100644 --- a/lib/fast_jsonapi/object_serializer.rb +++ b/lib/fast_jsonapi/object_serializer.rb @@ -40,7 +40,7 @@ module FastJsonapi hash_for_one_record end - alias_method :to_hash, :serializable_hash + alias to_hash serializable_hash def hash_for_one_record serializable_hash = { data: nil } @@ -79,7 +79,7 @@ module FastJsonapi ) serializable_hash.to_json end - alias_method :to_json, :serialized_json + alias to_json serialized_json private @@ -94,7 +94,7 @@ module FastJsonapi @links = options[:links] @is_collection = options[:is_collection] @params = options[:params] || {} - raise ArgumentError.new("`params` option passed to serializer must be a hash") unless @params.is_a?(Hash) + raise ArgumentError, '`params` option passed to serializer must be a hash' unless @params.is_a?(Hash) if options[:include].present? @includes = options[:include].reject(&:blank?).map(&:to_sym) @@ -121,7 +121,6 @@ module FastJsonapi end class_methods do - def inherited(subclass) super(subclass) subclass.attributes_to_serialize = attributes_to_serialize.dup if attributes_to_serialize.present? @@ -141,9 +140,7 @@ module FastJsonapi return @reflected_record_type if defined?(@reflected_record_type) @reflected_record_type ||= begin - if self.name && self.name.end_with?('Serializer') - self.name.split('::').last.chomp('Serializer').underscore.to_sym - end + name.split('::').last.chomp('Serializer').underscore.to_sym if name&.end_with?('Serializer') end end @@ -159,7 +156,7 @@ module FastJsonapi end def run_key_transform(input) - if self.transform_method.present? + if transform_method.present? input.to_s.send(*@transform_method).to_sym else input.to_sym @@ -181,7 +178,7 @@ module FastJsonapi def cache_options(cache_options) # FIXME: remove this if block once deprecated cache_options are not supported anymore - if !cache_options.key?(:store) + unless cache_options.key?(:store) # fall back to old, deprecated behaviour because no store was passed. # we assume the user explicitly wants new behaviour if he passed a # store because this is the new syntax. @@ -211,7 +208,7 @@ module FastJsonapi def attributes(*attributes_list, &block) attributes_list = attributes_list.first if attributes_list.first.class.is_a?(Array) options = attributes_list.last.is_a?(Hash) ? attributes_list.pop : {} - self.attributes_to_serialize = {} if self.attributes_to_serialize.nil? + self.attributes_to_serialize = {} if attributes_to_serialize.nil? # to support calling `attribute` with a lambda, e.g `attribute :key, ->(object) { ... }` block = attributes_list.pop if attributes_list.last.is_a?(Proc) @@ -235,11 +232,11 @@ module FastJsonapi self.uncachable_relationships_to_serialize = {} if uncachable_relationships_to_serialize.nil? if !relationship.cached - self.uncachable_relationships_to_serialize[relationship.name] = relationship + uncachable_relationships_to_serialize[relationship.name] = relationship else - self.cachable_relationships_to_serialize[relationship.name] = relationship + cachable_relationships_to_serialize[relationship.name] = relationship end - self.relationships_to_serialize[relationship.name] = relationship + relationships_to_serialize[relationship.name] = relationship end def has_many(relationship_name, options = {}, &block) @@ -265,11 +262,9 @@ module FastJsonapi name = base_key.to_sym if relationship_type == :has_many base_serialization_key = base_key.to_s.singularize - base_key_sym = base_serialization_key.to_sym id_postfix = '_ids' else base_serialization_key = base_key - base_key_sym = name id_postfix = '_id' end polymorphic = fetch_polymorphic_option(options) @@ -312,11 +307,11 @@ module FastJsonapi serializer_name = name.to_s.demodulize.classify + 'Serializer' serializer_class_name = namespace + serializer_name begin - return serializer_class_name.constantize + serializer_class_name.constantize rescue NameError - raise NameError, "#{self.name} cannot resolve a serializer class for '#{name}'. " + - "Attempted to find '#{serializer_class_name}'. " + - "Consider specifying the serializer directly through options[:serializer]." + raise NameError, "#{self.name} cannot resolve a serializer class for '#{name}'. " \ + "Attempted to find '#{serializer_class_name}'. " \ + 'Consider specifying the serializer directly through options[:serializer].' end end @@ -324,19 +319,20 @@ module FastJsonapi option = options[:polymorphic] return false unless option.present? return option if option.respond_to? :keys + {} end # def link(link_name, link_method_name = nil, &block) def link(*params, &block) - self.data_links = {} if self.data_links.nil? + self.data_links = {} if data_links.nil? options = params.last.is_a?(Hash) ? params.pop : {} link_name = params.first link_method_name = params[-1] key = run_key_transform(link_name) - self.data_links[key] = Link.new( + data_links[key] = Link.new( key: key, method: block || link_method_name, options: options @@ -352,6 +348,7 @@ module FastJsonapi relationships_to_serialize = klass.relationships_to_serialize || {} relationship_to_include = relationships_to_serialize[parsed_include] raise ArgumentError, "#{parsed_include} is not specified as a relationship on #{klass.name}" unless relationship_to_include + if relationship_to_include.static_serializer klass = relationship_to_include.static_serializer else diff --git a/lib/fast_jsonapi/relationship.rb b/lib/fast_jsonapi/relationship.rb index d783a0d..af9fe23 100644 --- a/lib/fast_jsonapi/relationship.rb +++ b/lib/fast_jsonapi/relationship.rb @@ -43,15 +43,14 @@ module FastJsonapi empty_case = relationship_type == :has_many ? [] : nil output_hash[key] = {} - unless (lazy_load_data && !included) - output_hash[key][:data] = ids_hash_from_record_and_relationship(record, serialization_params) || empty_case - end + output_hash[key][:data] = ids_hash_from_record_and_relationship(record, serialization_params) || empty_case unless lazy_load_data && !included add_links_hash(record, serialization_params, output_hash) if links.present? end end def fetch_associated_object(record, params) return FastJsonapi.call_proc(object_block, record, params) unless object_block.nil? + record.send(object_method_name) end @@ -65,7 +64,7 @@ module FastJsonapi def serializer_for(record, serialization_params) if @static_serializer - return @static_serializer + @static_serializer elsif polymorphic name = polymorphic[record.class] if polymorphic.is_a?(Hash) @@ -102,9 +101,11 @@ module FastJsonapi return unless associated_object = fetch_associated_object(record, params) - return associated_object.map do |object| - id_hash_from_record object, params - end if associated_object.respond_to? :map + if associated_object.respond_to? :map + return associated_object.map do |object| + id_hash_from_record object, params + end + end id_hash_from_record associated_object, params end @@ -116,10 +117,11 @@ module FastJsonapi def ids_hash(ids, record_type) return ids.map { |id| id_hash(id, record_type) } if ids.respond_to? :map + id_hash(ids, record_type) # ids variable is just a single id here end - def id_hash(id, record_type, default_return=false) + def id_hash(id, record_type, default_return = false) if id.present? { id: id.to_s, type: record_type } else @@ -131,24 +133,25 @@ module FastJsonapi if object_block.present? object = FastJsonapi.call_proc(object_block, record, params) return object.map { |item| item.public_send(id_method_name) } if object.respond_to? :map + return object.try(id_method_name) end record.public_send(id_method_name) end def add_links_hash(record, params, output_hash) - if links.is_a?(Symbol) - output_hash[key][:links] = record.public_send(links) - else - output_hash[key][:links] = links.each_with_object({}) do |(key, method), hash| - Link.new(key: key, method: method).serialize(record, params, hash)\ - end - end + output_hash[key][:links] = if links.is_a?(Symbol) + record.public_send(links) + else + links.each_with_object({}) do |(key, method), hash| + Link.new(key: key, method: method).serialize(record, params, hash)\ + end + end end def run_key_transform(input) - if self.transform_method.present? - input.to_s.send(*self.transform_method).to_sym + if transform_method.present? + input.to_s.send(*transform_method).to_sym else input.to_sym end @@ -156,6 +159,7 @@ module FastJsonapi def initialize_static_serializer return if @initialized_static_serializer + @static_serializer = compute_static_serializer @static_record_type = compute_static_record_type @initialized_static_serializer = true @@ -199,6 +203,7 @@ module FastJsonapi def record_type_for(record, serialization_params) # if the record type is static, return it return @static_record_type if @static_record_type + # if not, use the record type of the serializer, and memoize the transformed version serializer = serializer_for(record, serialization_params) @record_types_for[serializer] ||= run_key_transform(serializer.record_type) diff --git a/lib/fast_jsonapi/scalar.rb b/lib/fast_jsonapi/scalar.rb index 244ede8..3c67e57 100644 --- a/lib/fast_jsonapi/scalar.rb +++ b/lib/fast_jsonapi/scalar.rb @@ -10,10 +10,10 @@ module FastJsonapi def serialize(record, serialization_params, output_hash) if conditionally_allowed?(record, serialization_params) - output_hash[key] = if method.is_a?(Proc) - FastJsonapi.call_proc(method, record, serialization_params) + if method.is_a?(Proc) + output_hash[key] = FastJsonapi.call_proc(method, record, serialization_params) else - record.public_send(method) + output_hash[key] = record.public_send(method) end end end diff --git a/lib/fast_jsonapi/serialization_core.rb b/lib/fast_jsonapi/serialization_core.rb index 487c8d7..db3f1d6 100644 --- a/lib/fast_jsonapi/serialization_core.rb +++ b/lib/fast_jsonapi/serialization_core.rb @@ -25,7 +25,7 @@ module FastJsonapi end class_methods do - def id_hash(id, record_type, default_return=false) + def id_hash(id, record_type, default_return = false) if id.present? { id: id.to_s, type: record_type } else @@ -75,22 +75,22 @@ module FastJsonapi temp_hash end record_hash[:relationships] = record_hash[:relationships].merge(relationships_hash(record, uncachable_relationships_to_serialize, fieldset, includes_list, params)) if uncachable_relationships_to_serialize.present? - record_hash[:meta] = meta_hash(record, params) if meta_to_serialize.present? - record_hash else record_hash = id_hash(id_from_record(record, params), record_type, true) record_hash[:attributes] = attributes_hash(record, fieldset, params) if attributes_to_serialize.present? record_hash[:relationships] = relationships_hash(record, nil, fieldset, includes_list, params) if relationships_to_serialize.present? record_hash[:links] = links_hash(record, params) if data_links.present? - record_hash[:meta] = meta_hash(record, params) if meta_to_serialize.present? - record_hash end + + record_hash[:meta] = meta_hash(record, params) if meta_to_serialize.present? + record_hash end def id_from_record(record, params) return FastJsonapi.call_proc(record_id, record, params) if record_id.is_a?(Proc) return record.send(record_id) if record_id raise MandatoryField, 'id is a mandatory field in the jsonapi spec' unless record.respond_to?(:id) + record.id end @@ -116,12 +116,15 @@ module FastJsonapi items.each do |item| next unless relationships_to_serialize && relationships_to_serialize[item] + relationship_item = relationships_to_serialize[item] next unless relationship_item.include_relationship?(record, params) + relationship_type = relationship_item.relationship_type included_objects = relationship_item.fetch_associated_object(record, params) next if included_objects.blank? + included_objects = [included_objects] unless relationship_type == :has_many static_serializer = relationship_item.static_serializer diff --git a/lib/fast_jsonapi/version.rb b/lib/fast_jsonapi/version.rb index d884b26..01aef64 100644 --- a/lib/fast_jsonapi/version.rb +++ b/lib/fast_jsonapi/version.rb @@ -1,3 +1,3 @@ module FastJsonapi - VERSION = '1.7.0' + VERSION = '1.7.0'.freeze end diff --git a/lib/generators/serializer/serializer_generator.rb b/lib/generators/serializer/serializer_generator.rb index 7eb0e0a..14325bb 100644 --- a/lib/generators/serializer/serializer_generator.rb +++ b/lib/generators/serializer/serializer_generator.rb @@ -13,7 +13,7 @@ class SerializerGenerator < Rails::Generators::NamedBase private - def attributes_names - attributes.map { |a| a.name.to_sym.inspect } - end + def attributes_names + attributes.map { |a| a.name.to_sym.inspect } + end end diff --git a/spec/lib/extensions/active_record_spec.rb b/spec/lib/extensions/active_record_spec.rb index cbe7c1a..fcc28bc 100644 --- a/spec/lib/extensions/active_record_spec.rb +++ b/spec/lib/extensions/active_record_spec.rb @@ -2,11 +2,10 @@ require 'spec_helper' require 'active_record' require 'sqlite3' -describe 'active record' do - +RSpec.describe 'active record' do # Setup DB before(:all) do - @db_file = "test.db" + @db_file = 'test.db' # Open a database db = SQLite3::Database.new @db_file @@ -48,13 +47,12 @@ describe 'active record' do end ActiveRecord::Base.establish_connection( - :adapter => 'sqlite3', - :database => @db_file + adapter: 'sqlite3', + database: @db_file ) end context 'has one patch' do - it 'has account_id method for a supplier' do expect(Supplier.first.respond_to?(:account_id)).to be true expect(Supplier.first.account_id).to eq @account_id @@ -63,7 +61,6 @@ describe 'active record' do it 'has account_id method return nil if account not present' do expect(Supplier.find(@supplier_id_without_account).account_id).to eq nil end - end # Clean up DB @@ -72,10 +69,10 @@ describe 'active record' do end end -describe 'active record has_one through' do +RSpec.describe 'active record has_one through' do # Setup DB before(:all) do - @db_file = "test_two.db" + @db_file = 'test_two.db' # Open a database db = SQLite3::Database.new @db_file @@ -130,8 +127,8 @@ describe 'active record has_one through' do end ActiveRecord::Base.establish_connection( - :adapter => 'sqlite3', - :database => @db_file + adapter: 'sqlite3', + database: @db_file ) end @@ -139,7 +136,7 @@ describe 'active record has_one through' do it 'has an forest_id' do expect(Fruit.find(3).respond_to?(:forest_id)).to be true expect(Fruit.find(3).forest_id).to eq 1 - expect(Fruit.find(3).forest.name).to eq "sherwood" + expect(Fruit.find(3).forest.name).to eq 'sherwood' end it 'has nil if tree id not available' do diff --git a/spec/lib/helpers_spec.rb b/spec/lib/helpers_spec.rb index 4cf73fc..e3f3b99 100644 --- a/spec/lib/helpers_spec.rb +++ b/spec/lib/helpers_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe FastJsonapi do +RSpec.describe FastJsonapi do describe '.call_proc' do context 'with a Proc' do context 'with no parameters' do diff --git a/spec/lib/instrumentation/as_notifications_negative_spec.rb b/spec/lib/instrumentation/as_notifications_negative_spec.rb index de144de..f0ae50a 100644 --- a/spec/lib/instrumentation/as_notifications_negative_spec.rb +++ b/spec/lib/instrumentation/as_notifications_negative_spec.rb @@ -1,10 +1,9 @@ require 'spec_helper' -describe FastJsonapi::ObjectSerializer do +RSpec.describe FastJsonapi::ObjectSerializer do include_context 'movie class' context 'instrument' do - before(:each) do options = {} options[:meta] = { total: 2 } @@ -15,7 +14,6 @@ describe FastJsonapi::ObjectSerializer do end context 'serializable_hash' do - it 'should send not notifications' do events = [] @@ -31,11 +29,9 @@ describe FastJsonapi::ObjectSerializer do expect(serialized_hash.key?(:meta)).to eq(true) expect(serialized_hash.key?(:included)).to eq(true) end - end context 'serialized_json' do - it 'should send not notifications' do events = [] @@ -49,9 +45,6 @@ describe FastJsonapi::ObjectSerializer do expect(json.length).to be > 50 end - end - end - end diff --git a/spec/lib/instrumentation/as_notifications_spec.rb b/spec/lib/instrumentation/as_notifications_spec.rb index e4993bb..dff96a1 100644 --- a/spec/lib/instrumentation/as_notifications_spec.rb +++ b/spec/lib/instrumentation/as_notifications_spec.rb @@ -1,16 +1,15 @@ require 'spec_helper' -describe FastJsonapi::ObjectSerializer do +RSpec.describe FastJsonapi::ObjectSerializer do include_context 'movie class' context 'instrument' do - before(:all) do require 'fast_jsonapi/instrumentation' end after(:all) do - [ :serialized_json, :serializable_hash ].each do |m| + [:serialized_json, :serializable_hash].each do |m| alias_command = "alias_method :#{m}, :#{m}_without_instrumentation" FastJsonapi::ObjectSerializer.class_eval(alias_command) @@ -29,7 +28,6 @@ describe FastJsonapi::ObjectSerializer do end context 'serializable_hash' do - it 'should send notifications' do events = [] @@ -51,11 +49,9 @@ describe FastJsonapi::ObjectSerializer do expect(serialized_hash.key?(:meta)).to eq(true) expect(serialized_hash.key?(:included)).to eq(true) end - end context 'serialized_json' do - it 'should send notifications' do events = [] @@ -75,9 +71,6 @@ describe FastJsonapi::ObjectSerializer do expect(json.length).to be > 50 end - end - end - end diff --git a/spec/lib/object_serializer_attribute_param_spec.rb b/spec/lib/object_serializer_attribute_param_spec.rb index 3765a8c..7704887 100644 --- a/spec/lib/object_serializer_attribute_param_spec.rb +++ b/spec/lib/object_serializer_attribute_param_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' -describe FastJsonapi::ObjectSerializer do +RSpec.describe FastJsonapi::ObjectSerializer do include_context 'movie class' - context "params option" do + context 'params option' do let(:hash) { serializer.serializable_hash } before(:context) do @@ -18,8 +18,8 @@ describe FastJsonapi::ObjectSerializer do params[:user] ? movie.viewed?(params[:user]) : false end - attribute :no_param_attribute do |movie| - "no-param-attribute" + attribute :no_param_attribute do |_movie| + 'no-param-attribute' end end @@ -30,77 +30,77 @@ describe FastJsonapi::ObjectSerializer do Object.send(:remove_const, User) if Object.constants.include?(User) end - context "enforces a hash only params" do + context 'enforces a hash only params' do let(:params) { User.new([]) } - it "fails when creating a serializer with an object as params" do - expect(-> { MovieSerializer.new(movie, {params: User.new([])}) }).to raise_error(ArgumentError) + it 'fails when creating a serializer with an object as params' do + expect(-> { MovieSerializer.new(movie, { params: User.new([]) }) }).to raise_error(ArgumentError) end - it "succeeds creating a serializer with a hash" do - expect(-> { MovieSerializer.new(movie, {params: {current_user: User.new([])}}) }).not_to raise_error + it 'succeeds creating a serializer with a hash' do + expect(-> { MovieSerializer.new(movie, { params: { current_user: User.new([]) } }) }).not_to raise_error end end - context "passing params to the serializer" do - let(:params) { {user: User.new([movie.id])} } - let(:options_with_params) { {params: params} } + context 'passing params to the serializer' do + let(:params) { { user: User.new([movie.id]) } } + let(:options_with_params) { { params: params } } - context "with a single record" do + context 'with a single record' do let(:serializer) { MovieSerializer.new(movie, options_with_params) } - it "handles attributes that use params" do + it 'handles attributes that use params' do expect(hash[:data][:attributes][:viewed]).to eq(true) end it "handles attributes that don't use params" do - expect(hash[:data][:attributes][:no_param_attribute]).to eq("no-param-attribute") + expect(hash[:data][:attributes][:no_param_attribute]).to eq('no-param-attribute') end end - context "with a list of records" do + context 'with a list of records' do let(:movies) { build_movies(3) } let(:user) { User.new(movies.map { |m| [true, false].sample ? m.id : nil }.compact) } - let(:params) { {user: user} } + let(:params) { { user: user } } let(:serializer) { MovieSerializer.new(movies, options_with_params) } - it "has 3 items" do + it 'has 3 items' do hash[:data].length == 3 end - it "handles passing params to a list of resources" do + it 'handles passing params to a list of resources' do param_attribute_values = hash[:data].map { |data| [data[:id], data[:attributes][:viewed]] } expected_values = movies.map { |m| [m.id.to_s, user.viewed.include?(m.id)] } expect(param_attribute_values).to eq(expected_values) end - it "handles attributes without params" do + it 'handles attributes without params' do no_param_attribute_values = hash[:data].map { |data| data[:attributes][:no_param_attribute] } - expected_values = (1..3).map { "no-param-attribute" } + expected_values = (1..3).map { 'no-param-attribute' } expect(no_param_attribute_values).to eq(expected_values) end end end - context "without passing params to the serializer" do - context "with a single movie" do + context 'without passing params to the serializer' do + context 'with a single movie' do let(:serializer) { MovieSerializer.new(movie) } - it "handles param attributes" do + it 'handles param attributes' do expect(hash[:data][:attributes][:viewed]).to eq(false) end it "handles attributes that don't use params" do - expect(hash[:data][:attributes][:no_param_attribute]).to eq("no-param-attribute") + expect(hash[:data][:attributes][:no_param_attribute]).to eq('no-param-attribute') end end - context "with multiple movies" do + context 'with multiple movies' do let(:serializer) { MovieSerializer.new(build_movies(3)) } - it "handles attributes with params" do + it 'handles attributes with params' do param_attribute_values = hash[:data].map { |data| data[:attributes][:viewed] } expect(param_attribute_values).to eq([false, false, false]) @@ -108,7 +108,7 @@ describe FastJsonapi::ObjectSerializer do it "handles attributes that don't use params" do no_param_attribute_values = hash[:data].map { |data| data[:attributes][:no_param_attribute] } - expected_attribute_values = (1..3).map { "no-param-attribute" } + expected_attribute_values = (1..3).map { 'no-param-attribute' } expect(no_param_attribute_values).to eq(expected_attribute_values) end diff --git a/spec/lib/object_serializer_caching_spec.rb b/spec/lib/object_serializer_caching_spec.rb index 8eb185a..42a409e 100644 --- a/spec/lib/object_serializer_caching_spec.rb +++ b/spec/lib/object_serializer_caching_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe FastJsonapi::ObjectSerializer do +RSpec.describe FastJsonapi::ObjectSerializer do include_context 'movie class' context 'when caching has_many' do diff --git a/spec/lib/object_serializer_class_methods_spec.rb b/spec/lib/object_serializer_class_methods_spec.rb index aaa87f2..e9b1a15 100644 --- a/spec/lib/object_serializer_class_methods_spec.rb +++ b/spec/lib/object_serializer_class_methods_spec.rb @@ -1,14 +1,13 @@ require 'spec_helper' -describe FastJsonapi::ObjectSerializer do - +RSpec.describe FastJsonapi::ObjectSerializer do include_context 'movie class' describe '#has_many' do subject(:relationship) { serializer.relationships_to_serialize[:roles] } before do - serializer.has_many *children + serializer.has_many(*children) end after do @@ -16,7 +15,6 @@ describe FastJsonapi::ObjectSerializer do end context 'with namespace' do - before do class AppName::V1::RoleSerializer include FastJsonapi::ObjectSerializer @@ -41,7 +39,6 @@ describe FastJsonapi::ObjectSerializer do end context 'without namespace' do - before do class RoleSerializer include FastJsonapi::ObjectSerializer @@ -151,7 +148,7 @@ describe FastJsonapi::ObjectSerializer do subject(:relationship) { MovieSerializer.relationships_to_serialize[:area] } before do - MovieSerializer.belongs_to *parent + MovieSerializer.belongs_to(*parent) end after do @@ -159,7 +156,6 @@ describe FastJsonapi::ObjectSerializer do end context 'with overrides' do - before do class MyAreaSerializer include FastJsonapi::ObjectSerializer @@ -173,7 +169,6 @@ describe FastJsonapi::ObjectSerializer do end context 'without overrides' do - before do class AreaSerializer include FastJsonapi::ObjectSerializer @@ -239,7 +234,7 @@ describe FastJsonapi::ObjectSerializer do subject(:relationship) { MovieSerializer.relationships_to_serialize[:area] } before do - MovieSerializer.has_one *partner + MovieSerializer.has_one(*partner) end after do @@ -247,7 +242,6 @@ describe FastJsonapi::ObjectSerializer do end context 'with overrides' do - before do class MyAreaSerializer include FastJsonapi::ObjectSerializer @@ -261,7 +255,6 @@ describe FastJsonapi::ObjectSerializer do end context 'without overrides' do - before do class AreaSerializer include FastJsonapi::ObjectSerializer @@ -465,7 +458,7 @@ describe FastJsonapi::ObjectSerializer do end it 'returns correct hash when serializable_hash is called' do - expect(serializable_hash[:data][:meta]).to eq ({ years_since_release: year_since_release_calculator(movie.release_year) }) + expect(serializable_hash[:data][:meta]).to eq({ years_since_release: year_since_release_calculator(movie.release_year) }) end end @@ -481,7 +474,7 @@ describe FastJsonapi::ObjectSerializer do end it 'returns correct hash when serializable_hash is called' do - expect(serializable_hash[:data][:meta]).to eq ({ years_since_release: year_since_release_calculator(movie.release_year) }) + expect(serializable_hash[:data][:meta]).to eq({ years_since_release: year_since_release_calculator(movie.release_year) }) end end @@ -502,9 +495,7 @@ describe FastJsonapi::ObjectSerializer do context 'with block calling instance method on serializer' do before do - MovieSerializer.link(:self) do |movie_object| - movie_object.url - end + MovieSerializer.link(:self, &:url) end let(:url) { "http://movies.com/#{movie.id}" } diff --git a/spec/lib/object_serializer_fields_spec.rb b/spec/lib/object_serializer_fields_spec.rb index 697f209..b2bd50a 100644 --- a/spec/lib/object_serializer_fields_spec.rb +++ b/spec/lib/object_serializer_fields_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe FastJsonapi::ObjectSerializer do +RSpec.describe FastJsonapi::ObjectSerializer do include_context 'movie class' let(:fields) do diff --git a/spec/lib/object_serializer_inheritance_spec.rb b/spec/lib/object_serializer_inheritance_spec.rb index b0e27e6..d42179b 100644 --- a/spec/lib/object_serializer_inheritance_spec.rb +++ b/spec/lib/object_serializer_inheritance_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' -describe FastJsonapi::ObjectSerializer do - +RSpec.describe FastJsonapi::ObjectSerializer do after(:all) do classes_to_remove = %i[ User @@ -31,7 +30,7 @@ describe FastJsonapi::ObjectSerializer do set_id :uuid attributes :first_name, :last_name - attribute :full_name do |user, params| + attribute :full_name do |user, _params| "#{user.first_name} #{user.last_name}" end @@ -95,7 +94,6 @@ describe FastJsonapi::ObjectSerializer do end context 'when testing inheritance of attributes' do - it 'includes parent attributes' do subclass_attributes = EmployeeSerializer.attributes_to_serialize superclass_attributes = UserSerializer.attributes_to_serialize @@ -116,7 +114,6 @@ describe FastJsonapi::ObjectSerializer do end it 'doesnt change parent class attributes' do - EmployeeSerializer expect(UserSerializer.attributes_to_serialize).not_to have_key(:location) end @@ -140,7 +137,7 @@ describe FastJsonapi::ObjectSerializer do e = Employee.new e.country_id = 1 relationships_hash = EmployeeSerializer.new(e).serializable_hash[:data][:relationships][:country] - expect(relationships_hash).to include(data: { id: "1", type: :country }) + expect(relationships_hash).to include(data: { id: '1', type: :country }) end it 'includes child relationships' do @@ -148,7 +145,6 @@ describe FastJsonapi::ObjectSerializer do end it 'doesnt change parent class attributes' do - EmployeeSerializer expect(UserSerializer.relationships_to_serialize.keys).not_to include(:account) end @@ -161,7 +157,6 @@ describe FastJsonapi::ObjectSerializer do context 'when test inheritence of other attributes' do it 'inherits the tranform method' do - EmployeeSerializer expect(UserSerializer.transform_method).to eq EmployeeSerializer.transform_method end end diff --git a/spec/lib/object_serializer_polymorphic_spec.rb b/spec/lib/object_serializer_polymorphic_spec.rb index e1abd5e..0121f1a 100644 --- a/spec/lib/object_serializer_polymorphic_spec.rb +++ b/spec/lib/object_serializer_polymorphic_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe FastJsonapi::ObjectSerializer do +RSpec.describe FastJsonapi::ObjectSerializer do class List attr_accessor :id, :name, :items end diff --git a/spec/lib/object_serializer_relationship_links_spec.rb b/spec/lib/object_serializer_relationship_links_spec.rb index e45f9d7..fd1bd9f 100644 --- a/spec/lib/object_serializer_relationship_links_spec.rb +++ b/spec/lib/object_serializer_relationship_links_spec.rb @@ -1,13 +1,13 @@ require 'spec_helper' -describe FastJsonapi::ObjectSerializer do +RSpec.describe FastJsonapi::ObjectSerializer do include_context 'movie class' - context "params option" do + context 'params option' do let(:hash) { serializer.serializable_hash } - context "generating links for a serializer relationship" do - let(:params) { { } } + context 'generating links for a serializer relationship' do + let(:params) { {} } let(:options_with_params) { { params: params } } let(:relationship_url) { "http://movies.com/#{movie.id}/relationships/actors" } let(:related_url) { "http://movies.com/movies/#{movie.name.parameterize}/actors/" } @@ -15,42 +15,41 @@ describe FastJsonapi::ObjectSerializer do before(:context) do class MovieSerializer has_many :actors, lazy_load_data: false, links: { - self: :actors_relationship_url, - related: -> (object, params = {}) { - "#{params.has_key?(:secure) ? "https" : "http"}://movies.com/movies/#{object.name.parameterize}/actors/" + self: :actors_relationship_url, + related: lambda { |object, params = {}| + "#{params.key?(:secure) ? 'https' : 'http'}://movies.com/movies/#{object.name.parameterize}/actors/" } } end end - context "with a single record" do + context 'with a single record' do let(:serializer) { MovieSerializer.new(movie, options_with_params) } let(:links) { hash[:data][:relationships][:actors][:links] } - it "handles relationship links that call a method" do + it 'handles relationship links that call a method' do expect(links).to be_present expect(links[:self]).to eq(relationship_url) end - it "handles relationship links that call a proc" do + it 'handles relationship links that call a proc' do expect(links).to be_present expect(links[:related]).to eq(related_url) end - context "with serializer params" do + context 'with serializer params' do let(:params) { { secure: true } } - let(:secure_related_url) { related_url.gsub("http", "https") } + let(:secure_related_url) { related_url.gsub('http', 'https') } - it "passes the params to the link serializer correctly" do + it 'passes the params to the link serializer correctly' do expect(links).to be_present expect(links[:related]).to eq(secure_related_url) end end end - end - context "lazy loading relationship data" do + context 'lazy loading relationship data' do before(:context) do class LazyLoadingMovieSerializer < MovieSerializer has_many :actors, lazy_load_data: true, links: { @@ -62,13 +61,13 @@ describe FastJsonapi::ObjectSerializer do let(:serializer) { LazyLoadingMovieSerializer.new(movie) } let(:actor_hash) { hash[:data][:relationships][:actors] } - it "does not include the :data key" do + it 'does not include the :data key' do expect(actor_hash).to be_present expect(actor_hash).not_to have_key(:data) end end - context "including lazy loaded relationships" do + context 'including lazy loaded relationships' do before(:context) do class LazyLoadingMovieSerializer < MovieSerializer has_many :actors, lazy_load_data: true, links: { @@ -80,13 +79,13 @@ describe FastJsonapi::ObjectSerializer do let(:serializer) { LazyLoadingMovieSerializer.new(movie, include: [:actors]) } let(:actor_hash) { hash[:data][:relationships][:actors] } - it "includes the :data key" do + it 'includes the :data key' do expect(actor_hash).to be_present expect(actor_hash).to have_key(:data) end end - context "relationship links defined by a method on the object" do + context 'relationship links defined by a method on the object' do before(:context) do class Movie def relationship_links @@ -103,7 +102,7 @@ describe FastJsonapi::ObjectSerializer do let(:links) { hash[:data][:relationships][:actors][:links] } let(:relationship_url) { "http://movies.com/#{movie.id}/relationships/actors" } - it "generates relationship links in the object" do + it 'generates relationship links in the object' do expect(links).to be_present expect(links[:self]).to eq(relationship_url) end diff --git a/spec/lib/object_serializer_relationship_param_spec.rb b/spec/lib/object_serializer_relationship_param_spec.rb index d016814..8b3309d 100644 --- a/spec/lib/object_serializer_relationship_param_spec.rb +++ b/spec/lib/object_serializer_relationship_param_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' -describe FastJsonapi::ObjectSerializer do +RSpec.describe FastJsonapi::ObjectSerializer do include_context 'movie class' - context "params option" do + context 'params option' do let(:hash) { serializer.serializable_hash } before(:context) do @@ -22,39 +22,39 @@ describe FastJsonapi::ObjectSerializer do end end - context "passing params to the serializer" do - let(:params) { {authorized: true} } - let(:options_with_params) { {params: params} } + context 'passing params to the serializer' do + let(:params) { { authorized: true } } + let(:options_with_params) { { params: params } } - context "with a single record" do + context 'with a single record' do let(:serializer) { MovieSerializer.new(movie, options_with_params) } - it "handles relationships that use params" do - ids = hash[:data][:relationships][:agencies][:data].map{|a| a[:id]} + it 'handles relationships that use params' do + ids = hash[:data][:relationships][:agencies][:data].map { |a| a[:id] } ids.map!(&:to_i) - expect(ids).to eq [0,1,2] + expect(ids).to eq [0, 1, 2] end it "handles relationships that don't use params" do - expect(hash[:data][:relationships][:secondary_agency][:data]).to include({id: 1.to_s}) + expect(hash[:data][:relationships][:secondary_agency][:data]).to include({ id: 1.to_s }) end end - context "with a list of records" do + context 'with a list of records' do let(:movies) { build_movies(3) } - let(:params) { {authorized: true} } + let(:params) { { authorized: true } } let(:serializer) { MovieSerializer.new(movies, options_with_params) } - it "handles relationship params when passing params to a list of resources" do - relationships_hashes = hash[:data].map{|a| a[:relationships][:agencies][:data]}.uniq.flatten - expect(relationships_hashes.map{|a| a[:id].to_i}).to contain_exactly 0,1,2 + it 'handles relationship params when passing params to a list of resources' do + relationships_hashes = hash[:data].map { |a| a[:relationships][:agencies][:data] }.uniq.flatten + expect(relationships_hashes.map { |a| a[:id].to_i }).to contain_exactly 0, 1, 2 - uniq_count = hash[:data].map{|a| a[:relationships][:primary_agency] }.uniq.count + uniq_count = hash[:data].map { |a| a[:relationships][:primary_agency] }.uniq.count expect(uniq_count).to eq 1 end - it "handles relationships without params" do - uniq_count = hash[:data].map{|a| a[:relationships][:secondary_agency] }.uniq.count + it 'handles relationships without params' do + uniq_count = hash[:data].map { |a| a[:relationships][:secondary_agency] }.uniq.count expect(uniq_count).to eq 1 end end diff --git a/spec/lib/object_serializer_relationship_serializer_proc_spec.rb b/spec/lib/object_serializer_relationship_serializer_proc_spec.rb index 92a6ebb..b1924be 100644 --- a/spec/lib/object_serializer_relationship_serializer_proc_spec.rb +++ b/spec/lib/object_serializer_relationship_serializer_proc_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' -describe FastJsonapi::ObjectSerializer do - +RSpec.describe FastJsonapi::ObjectSerializer do class Person attr_accessor :id, :name, :assets end @@ -20,13 +19,13 @@ describe FastJsonapi::ObjectSerializer do attributes :name set_key_transform :dash - has_many :assets, serializer: -> (object) do + has_many :assets, serializer: lambda { |object| if object.is_a?(House) HouseSerializer elsif object.is_a?(Car) CarSerializer end - end + } end class HouseSerializer @@ -43,7 +42,6 @@ describe FastJsonapi::ObjectSerializer do set_key_transform :dash end - let(:house) do house = House.new house.id = 123 @@ -83,7 +81,7 @@ describe FastJsonapi::ObjectSerializer do person.id = 1 person.name = 'Bob' person.assets = [house, car] - person_hash = PersonSerializer.new(person, { include: [ :assets ] }).to_hash + person_hash = PersonSerializer.new(person, { include: [:assets] }).to_hash relationships = person_hash[:data][:relationships] house_relationship = relationships[:assets][:data][0] diff --git a/spec/lib/object_serializer_spec.rb b/spec/lib/object_serializer_spec.rb index 70c6531..9a90332 100644 --- a/spec/lib/object_serializer_spec.rb +++ b/spec/lib/object_serializer_spec.rb @@ -1,8 +1,7 @@ require 'spec_helper' -describe FastJsonapi::ObjectSerializer do +RSpec.describe FastJsonapi::ObjectSerializer do include_context 'movie class' - include_context 'group class' let(:movies) { build_movies(2) } @@ -43,7 +42,7 @@ describe FastJsonapi::ObjectSerializer do expect(serializable_hash[:included]).to be_instance_of(Array) expect(serializable_hash[:included].length).to eq include_object_total - (0..include_object_total-1).each do |include| + (0..include_object_total - 1).each do |include| expect(serializable_hash[:included][include]).to be_instance_of(Hash) end @@ -52,7 +51,7 @@ describe FastJsonapi::ObjectSerializer do expect(serializable_hash[:included]).to be_instance_of(Array) expect(serializable_hash[:included].length).to eq include_object_total - (0..include_object_total-1).each do |include| + (0..include_object_total - 1).each do |include| expect(serializable_hash[:included][include]).to be_instance_of(Hash) end end @@ -101,7 +100,7 @@ describe FastJsonapi::ObjectSerializer do it 'returns correct json when belongs_to returns nil and there is a block for the relationship' do movie.owner_id = nil - json = MovieSerializer.new(movie, {include: [:owner]}).serialized_json + json = MovieSerializer.new(movie, { include: [:owner] }).serialized_json serializable_hash = JSON.parse(json) expect(serializable_hash['data']['relationships']['owner']['data']).to be nil end @@ -190,7 +189,6 @@ describe FastJsonapi::ObjectSerializer do subject { MovieSerializer.new(movie, options).serializable_hash } it 'returns all actors in includes' do - expect( subject[:included].select { |i| i[:type] == :actor }.map { |i| i[:id] } ).to eq( @@ -210,7 +208,7 @@ describe FastJsonapi::ObjectSerializer do expect(serializable_hash[:included]).to be_instance_of(Array) expect(serializable_hash[:included].length).to eq include_object_total - (0..include_object_total-1).each do |include| + (0..include_object_total - 1).each do |include| expect(serializable_hash[:included][include]).to be_instance_of(Hash) end @@ -219,7 +217,7 @@ describe FastJsonapi::ObjectSerializer do expect(serializable_hash[:included]).to be_instance_of(Array) expect(serializable_hash[:included].length).to eq include_object_total - (0..include_object_total-1).each do |include| + (0..include_object_total - 1).each do |include| expect(serializable_hash[:included][include]).to be_instance_of(Hash) end end @@ -355,9 +353,7 @@ describe FastJsonapi::ObjectSerializer do context 'when serializing included, serialize any links' do before do - ActorSerializer.link(:self) do |actor_object| - actor_object.url - end + ActorSerializer.link(:self, &:url) end subject(:serializable_hash) do options = {} @@ -447,7 +443,7 @@ describe FastJsonapi::ObjectSerializer do movie.release_year = 1970 json = MovieOptionalRecordDataSerializer.new(movie).serialized_json serializable_hash = JSON.parse(json) - expect(serializable_hash['data']['attributes'].has_key?('release_year')).to be_falsey + expect(serializable_hash['data']['attributes'].key?('release_year')).to be_falsey end end @@ -463,23 +459,23 @@ describe FastJsonapi::ObjectSerializer do movie.release_year = 1970 json = MovieOptionalRecordDataWithLambdaSerializer.new(movie).serialized_json serializable_hash = JSON.parse(json) - expect(serializable_hash['data']['attributes'].has_key?('release_year')).to be_falsey + expect(serializable_hash['data']['attributes'].key?('release_year')).to be_falsey end end context 'when optional attributes are determined by params data' do it 'returns optional attribute when attribute is included' do movie.director = 'steven spielberg' - json = MovieOptionalParamsDataSerializer.new(movie, { params: { admin: true }}).serialized_json + json = MovieOptionalParamsDataSerializer.new(movie, { params: { admin: true } }).serialized_json serializable_hash = JSON.parse(json) expect(serializable_hash['data']['attributes']['director']).to eq 'steven spielberg' end it "doesn't return optional attribute when attribute is not included" do movie.director = 'steven spielberg' - json = MovieOptionalParamsDataSerializer.new(movie, { params: { admin: false }}).serialized_json + json = MovieOptionalParamsDataSerializer.new(movie, { params: { admin: false } }).serialized_json serializable_hash = JSON.parse(json) - expect(serializable_hash['data']['attributes'].has_key?('director')).to be_falsey + expect(serializable_hash['data']['attributes'].key?('director')).to be_falsey end end @@ -487,23 +483,23 @@ describe FastJsonapi::ObjectSerializer do it 'returns optional relationship when relationship is included' do json = MovieOptionalRelationshipSerializer.new(movie).serialized_json serializable_hash = JSON.parse(json) - expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_truthy + expect(serializable_hash['data']['relationships'].key?('actors')).to be_truthy end - context "when relationship is not included" do - let(:json) { + context 'when relationship is not included' do + let(:json) do MovieOptionalRelationshipSerializer.new(movie, options).serialized_json - } - let(:options) { + end + let(:options) do {} - } - let(:serializable_hash) { + end + let(:serializable_hash) do JSON.parse(json) - } + end it "doesn't return optional relationship" do movie.actor_ids = [] - expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_falsey + expect(serializable_hash['data']['relationships'].key?('actors')).to be_falsey end it "doesn't include optional relationship" do @@ -511,7 +507,6 @@ describe FastJsonapi::ObjectSerializer do options[:include] = [:actors] expect(serializable_hash['included']).to be_blank end - end end @@ -519,23 +514,23 @@ describe FastJsonapi::ObjectSerializer do it 'returns optional relationship when relationship is included' do json = MovieOptionalRelationshipWithLambdaSerializer.new(movie).serialized_json serializable_hash = JSON.parse(json) - expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_truthy + expect(serializable_hash['data']['relationships'].key?('actors')).to be_truthy end - context "when relationship is not included" do - let(:json) { + context 'when relationship is not included' do + let(:json) do MovieOptionalRelationshipWithLambdaSerializer.new(movie, options).serialized_json - } - let(:options) { + end + let(:options) do {} - } - let(:serializable_hash) { + end + let(:serializable_hash) do JSON.parse(json) - } + end it "doesn't return optional relationship" do movie.actor_ids = [] - expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_falsey + expect(serializable_hash['data']['relationships'].key?('actors')).to be_falsey end it "doesn't include optional relationship" do @@ -543,12 +538,11 @@ describe FastJsonapi::ObjectSerializer do options[:include] = [:actors] expect(serializable_hash['included']).to be_blank end - end end context 'when include has frozen array' do - let(:options) { { include: [:actors].freeze }} + let(:options) { { include: [:actors].freeze } } let(:json) { MovieOptionalRelationshipSerializer.new(movie, options).serialized_json } it 'does not raise and error' do @@ -558,24 +552,24 @@ describe FastJsonapi::ObjectSerializer do context 'when optional relationships are determined by params data' do it 'returns optional relationship when relationship is included' do - json = MovieOptionalRelationshipWithParamsSerializer.new(movie, { params: { admin: true }}).serialized_json + json = MovieOptionalRelationshipWithParamsSerializer.new(movie, { params: { admin: true } }).serialized_json serializable_hash = JSON.parse(json) - expect(serializable_hash['data']['relationships'].has_key?('owner')).to be_truthy + expect(serializable_hash['data']['relationships'].key?('owner')).to be_truthy end - context "when relationship is not included" do - let(:json) { + context 'when relationship is not included' do + let(:json) do MovieOptionalRelationshipWithParamsSerializer.new(movie, options).serialized_json - } - let(:options) { - { params: { admin: false }} - } - let(:serializable_hash) { + end + let(:options) do + { params: { admin: false } } + end + let(:serializable_hash) do JSON.parse(json) - } + end it "doesn't return optional relationship" do - expect(serializable_hash['data']['relationships'].has_key?('owner')).to be_falsey + expect(serializable_hash['data']['relationships'].key?('owner')).to be_falsey end it "doesn't include optional relationship" do diff --git a/spec/lib/object_serializer_struct_spec.rb b/spec/lib/object_serializer_struct_spec.rb index cf703ec..c6d4570 100644 --- a/spec/lib/object_serializer_struct_spec.rb +++ b/spec/lib/object_serializer_struct_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe FastJsonapi::ObjectSerializer do +RSpec.describe FastJsonapi::ObjectSerializer do include_context 'movie class' context 'when testing object serializer with ruby struct' do diff --git a/spec/lib/serialization_core_spec.rb b/spec/lib/serialization_core_spec.rb index 0d8d011..897c869 100644 --- a/spec/lib/serialization_core_spec.rb +++ b/spec/lib/serialization_core_spec.rb @@ -1,12 +1,12 @@ require 'spec_helper' -describe FastJsonapi::ObjectSerializer do - include_context "movie class" +RSpec.describe FastJsonapi::ObjectSerializer do + include_context 'movie class' include_context 'group class' context 'when testing class methods of serialization core' do it 'returns correct hash when id_hash is called' do - inputs = [{id: 23, record_type: :movie}, {id: 'x', record_type: 'person'}] + inputs = [{ id: 23, record_type: :movie }, { id: 'x', record_type: 'person' }] inputs.each do |hash| result_hash = MovieSerializer.send(:id_hash, hash[:id], hash[:record_type]) expect(result_hash[:id]).to eq hash[:id].to_s @@ -39,14 +39,14 @@ describe FastJsonapi::ObjectSerializer do relationships_hash = MovieSerializer.send(:relationships_hash, movie) relationship_names = relationships_hash.keys.sort relationships_hashes = MovieSerializer.relationships_to_serialize.values - expected_names = relationships_hashes.map{|relationship| relationship.key}.sort + expected_names = relationships_hashes.map(&:key).sort expect(relationship_names).to eq expected_names end it 'returns correct values when relationships_hash is called' do relationships_hash = MovieSerializer.relationships_hash(movie) - actors_hash = movie.actor_ids.map { |id| {id: id.to_s, type: :actor} } - owner_hash = {id: movie.owner_id.to_s, type: :user} + actors_hash = movie.actor_ids.map { |id| { id: id.to_s, type: :actor } } + owner_hash = { id: movie.owner_id.to_s, type: :user } expect(relationships_hash[:actors][:data]).to match_array actors_hash expect(relationships_hash[:owner][:data]).to eq owner_hash end diff --git a/spec/shared/contexts/group_context.rb b/spec/shared/contexts/group_context.rb index 38f3cb8..4c56257 100644 --- a/spec/shared/contexts/group_context.rb +++ b/spec/shared/contexts/group_context.rb @@ -1,5 +1,4 @@ RSpec.shared_context 'group class' do - # Person, Group Classes and serializers before(:context) do # models @@ -58,8 +57,8 @@ RSpec.shared_context 'group class' do person = Person.new person.id = 1 - person.last_name = "Last Name 1" - person.first_name = "First Name 1" + person.last_name = 'Last Name 1' + person.first_name = 'First Name 1' child_group = Group.new child_group.id = 2 diff --git a/spec/shared/contexts/movie_context.rb b/spec/shared/contexts/movie_context.rb index 5a8e645..ca3b88e 100644 --- a/spec/shared/contexts/movie_context.rb +++ b/spec/shared/contexts/movie_context.rb @@ -1,5 +1,4 @@ RSpec.shared_context 'movie class' do - # Movie, Actor Classes and serializers before(:context) do # models @@ -45,13 +44,14 @@ RSpec.shared_context 'movie class' do def owner return unless owner_id + ow = Owner.new ow.id = owner_id ow end def cache_key - "#{id}" + id.to_s end def local_name(locale = :english) @@ -127,7 +127,7 @@ RSpec.shared_context 'movie class' do attr_accessor :id, :name, :movie_ids def movies - movie_ids.map.with_index do |id, i| + movie_ids.map.with_index do m = Movie.new m.id = 232 m.name = 'test movie' @@ -170,7 +170,7 @@ RSpec.shared_context 'movie class' do # director attr is not mentioned intentionally attributes :name, :release_year has_many :actors - belongs_to :owner, record_type: :user do |object, params| + belongs_to :owner, record_type: :user do |object, _params| object.owner end belongs_to :movie_type @@ -190,7 +190,7 @@ RSpec.shared_context 'movie class' do end class OptionalDownloadableMovieSerializer < MovieSerializer - link(:download, if: Proc.new { |record, params| params && params[:signed_url] }) do |movie, params| + link(:download, if: proc { |_record, params| params && params[:signed_url] }) do |_movie, params| params[:signed_url] end end @@ -247,10 +247,12 @@ RSpec.shared_context 'movie class' do class AwardSerializer include FastJsonapi::ObjectSerializer attributes :id, :title - attribute :year, if: Proc.new { |record, params| - params[:include_award_year].present? ? - params[:include_award_year] : + attribute :year, if: proc { |_record, params| + if params[:include_award_year].present? + params[:include_award_year] + else false + end } belongs_to :actor end @@ -308,7 +310,7 @@ RSpec.shared_context 'movie class' do include FastJsonapi::ObjectSerializer set_type :movie attributes :name - attribute :release_year, if: Proc.new { |record| record.release_year >= 2000 } + attribute :release_year, if: proc { |record| record.release_year >= 2000 } end class MovieOptionalRecordDataWithLambdaSerializer @@ -322,14 +324,14 @@ RSpec.shared_context 'movie class' do include FastJsonapi::ObjectSerializer set_type :movie attributes :name - attribute :director, if: Proc.new { |record, params| params[:admin] == true } + attribute :director, if: proc { |_record, params| params[:admin] == true } end class MovieOptionalRelationshipSerializer include FastJsonapi::ObjectSerializer set_type :movie attributes :name - has_many :actors, if: Proc.new { |record| record.actors.any? } + has_many :actors, if: proc { |record| record.actors.any? } end class MovieOptionalRelationshipWithLambdaSerializer @@ -343,14 +345,14 @@ RSpec.shared_context 'movie class' do include FastJsonapi::ObjectSerializer set_type :movie attributes :name - belongs_to :owner, record_type: :user, if: Proc.new { |record, params| params[:admin] == true } + belongs_to :owner, record_type: :user, if: proc { |_record, params| params[:admin] == true } end class MovieOptionalAttributeContentsWithParamsSerializer include FastJsonapi::ObjectSerializer set_type :movie attributes :name - attribute :director do |record, params| + attribute :director do |_record, params| data = {} data[:first_name] = 'steven' data[:last_name] = 'spielberg' if params[:admin] @@ -359,7 +361,6 @@ RSpec.shared_context 'movie class' do end end - # Namespaced MovieSerializer before(:context) do # namespaced model stub @@ -423,9 +424,6 @@ RSpec.shared_context 'movie class' do end let(:movie_struct) do - - agency = AgencyStruct - actors = [] 3.times.each do |id| @@ -436,7 +434,7 @@ RSpec.shared_context 'movie class' do m[:id] = 23 m[:name] = 'struct movie' m[:release_year] = 1987 - m[:actor_ids] = [1,2,3] + m[:actor_ids] = [1, 2, 3] m[:owner_id] = 3 m[:movie_type_id] = 2 m[:actors] = actors @@ -467,13 +465,13 @@ RSpec.shared_context 'movie class' do end let(:movie_type) do - movie + movie - mt = MovieType.new - mt.id = movie.movie_type_id - mt.name = 'Foreign Thriller' - mt.movie_ids = [movie.id] - mt + mt = MovieType.new + mt.id = movie.movie_type_id + mt.name = 'Foreign Thriller' + mt.movie_ids = [movie.id] + mt end let(:supplier) do