page loader 2nd round
arnaud sellenet
committed Jun 10, 2014
commit 09faea1df033ee97fe8db7845dd794259d306c20
Showing 2
changed files with
62 additions
and 19 deletions
locomotive/steam/loaders/yml/pages_loader.rb b/lib/locomotive/steam/loaders/yml/pages_loader.rb
+51
-16
| @@ | @@ -19,7 +19,7 @@ module Locomotive |
| repository = @mapper.collection(:pages).repository | |
| all.each do |record| | |
| page = entity_class.new(record) | |
| - | repository.create page, :en |
| + | repository.create page |
| end | |
| end | |
| @@ | @@ -51,13 +51,12 @@ module Locomotive |
| [].tap do |page_records| | |
| position, last_dirname = nil, nil | |
| tree.each do |pagename, localizations| | |
| - | filepath = localizations.fetch(:default) |
| + | filepath = localizations.delete(: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 | |
| - | |
| - | page = page_attributes(filepath) |
| + | page = page_attributes(filepath, localizations) |
| page[:position] = position | |
| page_records << page | |
| @@ | @@ -76,16 +75,59 @@ module Locomotive |
| Dir.glob(File.join(root_dir, '**/*')).sort | |
| end | |
| - | def page_attributes(filepath) |
| + | def page_attributes(filepath, localizations) |
| {}.tap do |attributes| | |
| + | add_base_attributes(attributes, filepath) |
| + | add_localized_attributes(attributes, filepath, :en) |
| + | localizations.each { |locale, locale_file| add_localized_attributes(attributes, locale_file, locale) } |
| + | end |
| + | end |
| + | |
| + | def add_base_attributes(attributes, filepath) |
| + | attributes.merge! _base_attributes(filepath) |
| + | end |
| + | |
| + | def add_localized_attributes(attributes, filepath, locale) |
| + | attributes.deep_merge! _localized_attributes(filepath, locale) |
| + | end |
| + | |
| + | def _localized_attributes(filepath, locale) |
| + | fetch_raw_attrs(filepath).dup.select do |key, value| |
| + | %i{title slug fullpath redirect_url template |
| + | seo_title meta_keywords meta_description editable_elements} |
| + | .include? key |
| + | |
| + | end.map { |key, item| {key => { "#{locale}".to_sym => item }} }.reduce(:merge) |
| + | end |
| + | |
| + | def _base_attributes(filepath) |
| + | fetch_raw_attrs(filepath).dup.select do |key, value| |
| + | %i{content_type redirect_type handle listed searchable |
| + | templatized content_type published cache_strategy response_type |
| + | position} |
| + | .include? key |
| + | end |
| + | end |
| + | |
| + | def fetch_raw_attrs(filepath) |
| + | raw_attrs[filepath] |
| + | end |
| + | |
| + | def raw_attrs |
| + | @raw_attrs ||= Hash.new do |hsh, filepath| |
| + | attributes = {} |
| fullpath = self.filepath_to_fullpath(filepath) | |
| attributes[:title] = File.basename(fullpath).humanize | |
| attributes[:fullpath] = fullpath | |
| attributes[:template] = OpenStruct.new(raw_source: '') if File.directory?(filepath) | |
| - | attributes.merge(get_attributes_from_header(filepath)) unless File.directory?(filepath) |
| + | attributes.merge!(get_attributes_from_header(filepath)) unless File.directory?(filepath) |
| + | if content_type_slug = attributes.delete('content_type') |
| + | attributes[:templatized] = true |
| + | attributes[:content_type] = Locomotive::Models[:content_types][content_type_slug] |
| + | end |
| + | hsh[filepath] = attributes |
| end | |
| end | |
| - | |
| # Set attributes of a page from the information | |
| # stored in the header of the template (YAML matters). | |
| # It also stores the template. | |
| @@ | @@ -96,17 +138,10 @@ module Locomotive |
| def get_attributes_from_header(filepath) | |
| {}.tap do |attributes| | |
| template = Locomotive::Steam::Utils::YAMLFrontMattersTemplate.new(filepath) | |
| - | |
| + | attributes[:template] = template |
| if template.attributes | |
| - | attributes.merge(template.attributes) |
| - | |
| - | if content_type_slug = attributes.delete('content_type') |
| - | attributes[:templatized] = true |
| - | attributes[:content_type] = Locomotive::Models[:content_types][content_type_slug] |
| - | end |
| + | attributes.merge! template.attributes.symbolize_keys |
| end | |
| - | |
| - | attributes[:template] = template |
| end | |
| end | |
spec/unit/loaders/pages_loader_spec.rb
+11
-3
| @@ | @@ -4,20 +4,28 @@ describe Locomotive::Steam::Loader::Yml::PagesLoader do |
| let(:path) { default_fixture_site_path } | |
| - | subject { Locomotive::Steam::Loader::Yml::PagesLoader.new path, mapper } |
| + | let(:loader) { Locomotive::Steam::Loader::Yml::PagesLoader.new path, mapper } |
| + | subject { loader } |
| + | before { Locomotive::Models[:pages].current_locale = :en } |
| describe '#initialize' do | |
| it { should be_kind_of Object } | |
| end | |
| describe '#load!' do | |
| - | before { subject.load! } |
| + | before { loader.load! } |
| it 'loads pages in the pages Repository' do | |
| - | Locomotive::Models[:pages].all(:en).size.should > 0 |
| + | Locomotive::Models[:pages].all.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 | |
| + | |
| + | context 'records content' do |
| + | subject { Locomotive::Models[:pages]['index'] } |
| + | it { subject.title[:en].should eql 'Home page' } |
| + | end |
| end | |
| end | |