Sha256: a4b84ab0ed1fb51e073af1b6f7cb9a5634ebeaaa7c3e0ce6898e900c8947ab9f

Contents?: true

Size: 1.51 KB

Versions: 6

Compression:

Stored size: 1.51 KB

Contents

# TODO: Custom resolver for db themes: http://blog.jgarciam.net/post/21098440864/database-stored-templates-on-ruby-on-rails
# TODO: Multiple extentions with tilt
# TODO: Allow converting db-backed Theme to file-backed Theme

module Landable
  class Layout
    def initialize(file)
      @file = file
    end

    def process
      return if @processed

      path = @file.dup
      self.class.paths.each { |p| path.sub!(p, '') }

      path.sub!(/^\//, '')

      @path, @extension = path.split('.html.', 2)

      @body = File.read(@file)

      @processed = true
    end

    def to_theme
      process unless @processed

      theme = Theme.where(file: @path).first_or_initialize
      theme.name          ||= @path.gsub('/', ' ').titlecase
      theme.extension       = @extension
      theme.description     = description if theme.description.blank? || theme.description =~ /^Defined in/
      theme.body            = @body
      theme.editable        = false
      theme.thumbnail_url ||= "http://placehold.it/300x200"

      theme.save!

      theme
    end

    def description
      "Defined in #@path.html.#@extension"
    end

    class << self
      def all
        files.map { |file| new(file) }
      end

      def files
        files = []

        paths.map do |path|
          files << Dir[path + "/**/[^_]*.html.*"]
          files << Dir[path + "/**/application*"]
        end

        files.flatten.uniq
      end

      def paths
        @paths ||= Dir[Rails.root.join('app/views/layouts').to_s]
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
landable-1.13.1 lib/landable/layout.rb
landable-1.12.3 lib/landable/layout.rb
landable-1.12.2 lib/landable/layout.rb
landable-1.12.1 lib/landable/layout.rb
landable-1.11.1 lib/landable/layout.rb
landable-1.11.0 lib/landable/layout.rb