get rid of temp variables

This commit is contained in:
Benjamin Roth 2018-02-02 14:27:37 +01:00 committed by Shishir Kakaraddi
parent a9c902b96f
commit 088d655998
2 changed files with 9 additions and 17 deletions

View File

@ -161,7 +161,7 @@ module FastJsonapi
key = options[:key] || relationship_name.to_s.dasherize.to_sym key = options[:key] || relationship_name.to_s.dasherize.to_sym
record_type = options[:record_type] || relationship_name.to_s.dasherize.to_sym record_type = options[:record_type] || relationship_name.to_s.dasherize.to_sym
end end
relationship = { add_relationship(name, {
key: key, key: key,
name: name, name: name,
id_method_name: options[:id_method_name] || (relationship_name.to_s + '_id').to_sym, id_method_name: options[:id_method_name] || (relationship_name.to_s + '_id').to_sym,
@ -170,8 +170,7 @@ module FastJsonapi
serializer: compute_serializer_name(serializer_key), serializer: compute_serializer_name(serializer_key),
relationship_type: :belongs_to, relationship_type: :belongs_to,
cached: options[:cached] || true cached: options[:cached] || true
} })
add_relationship(name, relationship)
end end
def has_one(relationship_name, options = {}) def has_one(relationship_name, options = {})
@ -183,7 +182,7 @@ module FastJsonapi
key = options[:key] || relationship_name.to_s.dasherize.to_sym key = options[:key] || relationship_name.to_s.dasherize.to_sym
record_type = options[:record_type] || relationship_name.to_s.dasherize.to_sym record_type = options[:record_type] || relationship_name.to_s.dasherize.to_sym
end end
relationship = { add_relationship(name, {
key: key, key: key,
name: name, name: name,
id_method_name: options[:id_method_name] || (relationship_name.to_s + '_id').to_sym, id_method_name: options[:id_method_name] || (relationship_name.to_s + '_id').to_sym,
@ -192,8 +191,7 @@ module FastJsonapi
serializer: compute_serializer_name(serializer_key), serializer: compute_serializer_name(serializer_key),
relationship_type: :has_one, relationship_type: :has_one,
cached: options[:cached] || false cached: options[:cached] || false
} })
add_relationship(name, relationship)
end end
def compute_serializer_name(serializer_key) def compute_serializer_name(serializer_key)

View File

@ -27,27 +27,23 @@ module FastJsonapi
end end
def attributes_hash(record) def attributes_hash(record)
attributes_hash = {} attributes_to_serialize.each_with_object({}) do |(key, method_name), attr_hash|
attributes_to_serialize.each do |key, method_name| attr_hash[key] = record.send(method_name)
attributes_hash[key] = record.send(method_name)
end end
attributes_hash
end end
def relationships_hash(record, relationships = nil) def relationships_hash(record, relationships = nil)
relationships_hash = {}
relationships = relationships_to_serialize if relationships.nil? relationships = relationships_to_serialize if relationships.nil?
relationships.each do |_k, relationship| relationships.each_with_object({}) do |(_k, relationship), hash|
name = relationship[:key] name = relationship[:key]
id_method_name = relationship[:id_method_name] id_method_name = relationship[:id_method_name]
record_type = relationship[:record_type] record_type = relationship[:record_type]
empty_case = relationship[:relationship_type] == :has_many ? [] : nil empty_case = relationship[:relationship_type] == :has_many ? [] : nil
relationships_hash[name] = { hash[name] = {
data: ids_hash(record.send(id_method_name), record_type) || empty_case data: ids_hash(record.send(id_method_name), record_type) || empty_case
} }
end end
relationships_hash
end end
def record_hash(record) def record_hash(record)
@ -76,8 +72,7 @@ module FastJsonapi
# includes handler # includes handler
def get_included_records(record, includes_list, known_included_objects) def get_included_records(record, includes_list, known_included_objects)
included_records = [] includes_list.each_with_object([]) do |item, included_records|
includes_list.each do |item|
object_method_name = @relationships_to_serialize[item][:object_method_name] object_method_name = @relationships_to_serialize[item][:object_method_name]
record_type = @relationships_to_serialize[item][:record_type] record_type = @relationships_to_serialize[item][:record_type]
serializer = @relationships_to_serialize[item][:serializer].to_s.constantize serializer = @relationships_to_serialize[item][:serializer].to_s.constantize
@ -92,7 +87,6 @@ module FastJsonapi
included_records << serializer.record_hash(inc_obj) included_records << serializer.record_hash(inc_obj)
end end
end end
included_records
end end
def has_permitted_includes(requested_includes) def has_permitted_includes(requested_includes)