Sha256: ee828f921b9d9c8053c98c3a202599bd5403eb64d5823529632f52288b506f7b

Contents?: true

Size: 1.49 KB

Versions: 10

Compression:

Stored size: 1.49 KB

Contents

module JekyllAdmin
  module FileHelper

    # The file the user requested in the URL
    def requested_file
      find_by_path(path)
    end

    # The file ultimately written to disk
    # This may be the requested file, or in the case of a rename will be read
    # from the new path that was actually written to disk
    def written_file
      find_by_path(write_path)
    end

    # Write a file to disk with the given content
    def write_file(path, content)
      Jekyll.logger.debug "WRITING:", path
      path = sanitized_path(path)
      FileUtils.mkdir_p File.dirname(path)
      File.write(path, content)
      site.process
    end

    # Delete the file at the given path
    def delete_file(path)
      Jekyll.logger.debug "DELETING:", path
      FileUtils.rm_f sanitized_path(path)
      site.process
    end

    private

    def ensure_requested_file
      ensure_file(requested_file)
    end

    def ensure_written_file
      ensure_file(written_file)
    end

    def find_by_path(path)
      files = case namespace
              when "collections"
                collection.docs
              when "data"
                DataFile.all
              when "pages", "static_files"
                site.public_send(namespace.to_sym)
              else
                []
              end
      files.find { |f| sanitized_path(f.path) == path }
    end

    def ensure_file(file)
      render_404 if file.nil?
    end

    def ensure_directory
      render_404 unless Dir.exist?(directory_path)
    end
  end
end

Version data entries

10 entries across 10 versions & 5 rubygems

Version Path
jekyll-admin-josercc-0.5.5 lib/jekyll-admin-josercc/file_helper.rb
jekyll-admin-josercc-0.5.4 lib/jekyll-admin-josercc/file_helper.rb
jekyll-admin-josercc-josercc-0.5.4 lib/jekyll-admin-josercc/file_helper.rb
jekyll-admin-josercc-0.5.3 lib/jekyll-admin/file_helper.rb
jekyll-admin-josercc-0.5.2 lib/jekyll-admin/file_helper.rb
jekyll-admin-0.5.1 lib/jekyll-admin/file_helper.rb
jekyll-admin-0.5.0 lib/jekyll-admin/file_helper.rb
jekyll-admin-jekyll34-0.4.3 lib/jekyll-admin/file_helper.rb
jekyll-admin-0.4.1 lib/jekyll-admin/file_helper.rb
j1m_admin-0.0.1 lib/jekyll-admin/file_helper.rb