Sha256: 678df65e3f01da23d4a9d71fbb6cbbc11fb51db87bc14ba6adcb3cb7bf94bb36

Contents?: true

Size: 1.81 KB

Versions: 16

Compression:

Stored size: 1.81 KB

Contents

module Locomotive::Wagon

  class SyncPagesCommand < PullPagesCommand

    include Locomotive::Wagon::BaseConcern

    def write_page(page, locale = nil)
      filepath = page_filepath(page, locale)

      if File.exists?(filepath)
        # only copy the content of the editable elements
        if attributes = editable_elements_attributes(page, locale)
          new_content = replace_editable_elements(filepath, dump(attributes))

          write_to_file(filepath, new_content)
        end
      else
        # pull the whole page
        super
      end
    end

    private

    def editable_elements_attributes(page, locale)
      list = page.editable_elements.inject({}) do |hash, el|
        hash["#{el['block']}/#{el['slug']}"] = replace_asset_urls(el['content']) if el['content']
        hash
      end

      list.empty? ? nil : { 'editable_elements' => list }
    end

    def replace_editable_elements(filepath, replacement)
      content   = File.read(File.join(path, filepath))
      existing  = content =~ /\A.*?editable_elements:.*?\n---/m

      content.gsub /\A---(.*?)\n---/m do |s|
        if existing
          s.gsub(/editable_elements:\n(.*?)\n(\S)/m) { |_s| "#{replacement}#{$2}" }
        else
          s.gsub(/---\Z/) { |_s| "#{replacement}\n---" }
        end
      end
    end

    def page_filepath(page, locale)
      fullpath = locale == default_locale ? page.fullpath : "#{fullpaths[page._id]}.#{locale}"

      # HAML template?
      if File.exists?(filepath = _page_filepath(fullpath, '.haml'))
        return filepath
      end

      # simple Liquid template OR the page does not exist in the local site
      _page_filepath(fullpath)
    end

    def _page_filepath(fullpath, additional_extension = '')
      File.join('app', 'views', 'pages', fullpath + ".liquid#{additional_extension}")
    end

  end

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
locomotivecms_wagon-2.2.0.rc2 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.2.0.rc1 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.2.0.beta1 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.1.1 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.1.0 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.1.0.rc5 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.1.0.rc4 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.1.0.rc3 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.0.1 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.0.0 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.0.0.rc8 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.0.0.rc7 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.0.0.rc6 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.0.0.rc5 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.0.0.rc4 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb
locomotivecms_wagon-2.0.0.rc3 lib/locomotive/wagon/commands/sync_sub_commands/sync_pages_command.rb