lib/dato/local/items_repo.rb in dato-0.2.7 vs lib/dato/local/items_repo.rb in dato-0.3.0

- old
+ new

@@ -48,13 +48,13 @@ def collection_item_types item_types - single_instance_item_types end def items_of_type(item_type) - method, singleton = item_type_methods[item_type] + method = item_type_methods[item_type] - if singleton + if item_type.singleton Array(@collections_by_type[method]) else @collections_by_type[method] end end @@ -62,10 +62,11 @@ private def build_cache! build_item_type_methods! build_collections_by_type! + build_singletons_by_type! end def build_item_type_methods! @item_type_methods = {} @@ -74,44 +75,61 @@ .map(&:pluralize) clashing_keys = singleton_keys & collection_keys item_types.each do |item_type| - singleton = item_type.singleton pluralized_api_key = item_type.api_key.pluralize - method = singleton ? item_type.api_key : pluralized_api_key + method = if item_type.singleton + item_type.api_key + else + pluralized_api_key + end + if clashing_keys.include?(pluralized_api_key) - suffix = singleton ? 'instance' : 'collection' + suffix = item_type.singleton ? 'instance' : 'collection' method = "#{method}_#{suffix}" end - @item_type_methods[item_type] = [method.to_sym, singleton] + @item_type_methods[item_type] = method.to_sym end end def build_collections_by_type! item_types.each do |item_type| - method, singleton = item_type_methods[item_type] - - @collections_by_type[method] = if singleton + method = item_type_methods[item_type] + @collections_by_type[method] = if item_type.singleton nil else ItemCollection.new end end item_entities.each do |item_entity| item = Item.new(item_entity, self) - method, singleton = item_type_methods[item_entity.item_type] + method = item_type_methods[item_entity.item_type] - if singleton - @collections_by_type[method] = item - else + unless item_entity.item_type.singleton @collections_by_type[method].push item end @items_by_id[item.id] = item + end + + item_types.each do |item_type| + method = item_type_methods[item_type] + if !item_type.singleton && item_type.sortable + @collections_by_type[method].sort_by!(&:position) + end + end + end + + def build_singletons_by_type! + item_types.each do |item_type| + method = item_type_methods[item_type] + if item_type.singleton + @collections_by_type[method] = @items_by_id[item_type.singleton_item.id] + end end end def item_entities entities_repo.find_entities_of_type('item')