lib/dato/local/items_repo.rb in dato-0.8.0 vs lib/dato/local/items_repo.rb in dato-0.8.1

- old
+ new

@@ -1,10 +1,10 @@ # frozen_string_literal: true -require 'active_support/core_ext/string' -require 'dato/local/item' -require 'dato/local/site' +require "active_support/core_ext/string" +require "dato/local/item" +require "dato/local/site" module Dato module Local class ItemsRepo attr_reader :entities_repo, :collections_by_type, :item_type_methods @@ -35,21 +35,21 @@ end end def site Site.new( - entities_repo.find_entities_of_type('site').first, - self + entities_repo.find_entities_of_type("site").first, + self, ) end def available_locales site.locales.map(&:to_sym) end def item_types - entities_repo.find_entities_of_type('item_type') + entities_repo.find_entities_of_type("item_type") end def single_instance_item_types item_types.select(&:singleton) end @@ -95,11 +95,11 @@ else pluralized_api_key end if clashing_keys.include?(pluralized_api_key) - suffix = item_type.singleton ? 'instance' : 'collection' + suffix = item_type.singleton ? "instance" : "collection" method = "#{method}_#{suffix}" end @item_type_methods[item_type] = method.to_sym end @@ -117,13 +117,11 @@ item_entities.each do |item_entity| item = Item.new(item_entity, self) method = item_type_methods[item_entity.item_type] - unless item_entity.item_type.singleton - @collections_by_type[method].push item - end + @collections_by_type[method].push item unless item_entity.item_type.singleton @items_by_id[item.id] = item if item_entity.respond_to?(:parent_id) && item_entity.parent_id @items_by_parent_id[item_entity.parent_id] ||= [] @@ -142,32 +140,28 @@ field = item_type.ordering_field.api_key nil_items, valid_items = @collections_by_type[method].partition do |item| item[field].nil? end @collections_by_type[method] = valid_items.sort_by { |item| item[field] } + nil_items - if item_type.ordering_direction == 'desc' - @collections_by_type[method].reverse! - end + @collections_by_type[method].reverse! if item_type.ordering_direction == "desc" end end end def build_singletons_by_type! item_types.each do |item_type| method = item_type_methods[item_type] next unless item_type.singleton - item = if item_type.singleton_item - @items_by_id[item_type.singleton_item.id] - end + item = (@items_by_id[item_type.singleton_item.id] if item_type.singleton_item) @collections_by_type[method] = item end end def item_entities - entities_repo.find_entities_of_type('item') + entities_repo.find_entities_of_type("item") end def method_missing(method, *arguments, &block) if collections_by_type.key?(method) && arguments.empty? collections_by_type[method] @@ -175,10 +169,10 @@ super end rescue NoMethodError message = [] message << "Undefined method `#{method}`" - message << 'Available DatoCMS collections/items:' + message << "Available DatoCMS collections/items:" message += collections_by_type.map do |key, _value| "* .#{key}" end raise NoMethodError, message.join("\n") end