sync site metafields

did committed Feb 10, 2016
commit 7ef80d024d9b896e444e1b4ccd0c9c1ea3726155
Showing 4 changed files with 79 additions and 2 deletions
locomotive/wagon/commands/sync_command.rb b/lib/locomotive/wagon/commands/sync_command.rb +1 -1
@@ @@ -15,7 +15,7 @@ module Locomotive::Wagon
class SyncCommand < Struct.new(:env, :path, :options, :shell)
- RESOURCES = %w(pages content_entries translations).freeze
+ RESOURCES = %w(site pages content_entries translations).freeze
include ApiConcern
include DeployFileConcern
locomotive/wagon/commands/sync_sub_commands/sync_site_command.rb b/lib/locomotive/wagon/commands/sync_sub_commands/sync_site_command.rb +31 -0
@@ @@ -0,0 +1,31 @@
+ module Locomotive::Wagon
+
+ class SyncSiteCommand < PullSiteCommand
+
+ include Locomotive::Wagon::BaseConcern
+
+ def _sync
+ attributes = current_site.attributes.slice('metafields')
+
+ # convert to hash + download assets and use the asset local version
+ decode_metafields(attributes)
+
+ # modify the config/site.yml file accordingly
+ replace_metafields_in_file(attributes['metafields'])
+ end
+
+ protected
+
+ def replace_metafields_in_file(metafields)
+ return if metafields.blank?
+
+ content = File.read(File.join(path, 'config', 'site.yml'))
+
+ content.gsub!(/^metafields:\n.+\n\s+.*?\n/m, { 'metafields' => metafields }.to_yaml.to_s.gsub(/\A---\n/, ''))
+
+ File.write(File.join(path, 'config', 'site.yml'), content)
+ end
+
+ end
+
+ end
locomotive/wagon/tools/yaml_ext.rb b/lib/locomotive/wagon/tools/yaml_ext.rb +1 -1
@@ @@ -4,7 +4,7 @@ module Locomotive
module YamlExt
def self.transform(hash, &block)
- return if hash.blank?
+ return if hash.blank? || !hash.respond_to?(:has_key?)
hash.each do |key, value|
case value
spec/unit/tools/yaml_ext_spec.rb +46 -0
@@ @@ -0,0 +1,46 @@
+ # encoding: utf-8
+
+ require 'spec_helper'
+
+ require_relative '../../../lib/locomotive/wagon/tools/yaml_ext.rb'
+
+ describe Locomotive::Wagon::YamlExt do
+
+ describe '.transform' do
+
+ let(:hash) { nil }
+ let(:block) { -> (value) { value + '!' } }
+
+ subject { described_class.transform(hash, &block); hash }
+
+ it { expect(subject).to eq nil }
+
+ describe 'simple hash' do
+
+ let(:hash) { { 'foo' => 'a', 'bar' => 'b' } }
+
+ it { expect(subject['foo']).to eq 'a!' }
+ it { expect(subject['bar']).to eq 'b!' }
+
+ end
+
+ describe 'hash of hashes' do
+
+ let(:hash) { { 'foo' => { 'bar' => 'a' } } }
+
+ it { expect(subject['foo']['bar']).to eq 'a!' }
+
+ end
+
+ describe 'hash with an array' do
+
+ let(:hash) { { 'foo' => [{ 'bar' => 'a' }, 2] } }
+
+ it { expect(subject['foo'][0]['bar']).to eq 'a!' }
+
+ end
+
+
+ end
+
+ end