load one entity per actual page
arnaud sellenet
committed Jun 03, 2014
commit cf2850ff083ef6f8d4255c29e3623798096c5871
Showing 4
changed files with
19 additions
and 18 deletions
locomotive/steam/loaders/yml/pages_loader.rb b/lib/locomotive/steam/loaders/yml/pages_loader.rb
+13
-16
| @@ | @@ -20,23 +20,11 @@ module Locomotive |
| all.each do |record| | |
| page = entity_class.new(record) | |
| repository.create page, :en | |
| - | |
| end | |
| end | |
| protected | |
| - | # Create a ordered list of pages from the Hash |
| - | # |
| - | # @return [ Array ] An ordered list of pages |
| - | # |
| - | def pages_to_list |
| - | # sort by fullpath first |
| - | list = self.pages.values.sort { |a, b| a.fullpath <=> b.fullpath } |
| - | # sort finally by depth |
| - | list.sort { |a, b| a.depth <=> b.depth } |
| - | end |
| - | |
| def build_relationships(parent, list) | |
| # do not use an empty template for other locales than the default one | |
| parent.set_default_template_for_each_locale(self.default_locale) | |
| @@ | @@ -62,10 +50,9 @@ module Locomotive |
| def all | |
| [].tap do |page_records| | |
| position, last_dirname = nil, nil | |
| - | |
| - | Dir.glob(File.join(root_dir, '**/*')).sort.each do |filepath| |
| - | next unless File.directory?(filepath) || filepath =~ /\.(#{Locomotive::Steam::TEMPLATE_EXTENSIONS.join('|')})$/ |
| - | |
| + | tree.each do |pagename, localizations| |
| + | filepath = localizations.fetch(:default) |
| + | #next unless File.directory?(filepath) || filepath =~ /\.(#{Locomotive::Steam::TEMPLATE_EXTENSIONS.join('|')})$/ |
| if last_dirname != File.dirname(filepath) | |
| position, last_dirname = 100, File.dirname(filepath) | |
| end | |
| @@ | @@ -79,6 +66,16 @@ module Locomotive |
| end | |
| end | |
| + | def tree |
| + | Locomotive::Steam::Utils::LocalizedTree |
| + | .new(all_files, Locomotive::Steam::TEMPLATE_EXTENSIONS) |
| + | .to_hash |
| + | end |
| + | |
| + | def all_files |
| + | Dir.glob(File.join(root_dir, '**/*')).sort |
| + | end |
| + | |
| def page_attributes(filepath) | |
| {}.tap do |attributes| | |
| fullpath = self.filepath_to_fullpath(filepath) | |
locomotive/steam/loaders/yml_loader.rb b/lib/locomotive/steam/loaders/yml_loader.rb
+1
-0
| @@ | @@ -1,6 +1,7 @@ |
| require_relative 'yml/site_loader' | |
| require_relative 'yml/pages_loader' | |
| require_relative 'yml/utils/yaml_front_matters_template' | |
| + | require_relative 'yml/utils/localized_tree' |
| module Locomotive | |
| module Steam | |
locomotive/steam/repositories/pages_repository.rb b/lib/locomotive/steam/repositories/pages_repository.rb
+1
-1
| @@ | @@ -5,7 +5,7 @@ module Locomotive |
| include Repository | |
| def [](path) | |
| - | matching_paths([paths]) |
| + | matching_paths([paths]).first |
| end | |
| def matching_paths(paths) | |
spec/unit/loaders/pages_loader_spec.rb
+4
-1
| @@ | @@ -15,6 +15,9 @@ describe Locomotive::Steam::Loader::Yml::PagesLoader do |
| it 'loads pages in the pages Repository' do | |
| Locomotive::Models[:pages].all(:en).size.should > 0 | |
| end | |
| + | it 'creates only one Entity for all locales' do |
| + | # TODO do not rely on repository |
| + | Locomotive::Models[:pages].matching_paths(['index']).size.should eq 1 |
| + | end |
| end | |
| - | |
| end | |