Class: Landable::Layout

Inherits:
Object
  • Object
show all
Defined in:
lib/landable/layout.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Layout

Returns a new instance of Layout.



7
8
9
# File 'lib/landable/layout.rb', line 7

def initialize(file)
  @file = file
end

Class Method Details

.allObject



47
48
49
# File 'lib/landable/layout.rb', line 47

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

.filesObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/landable/layout.rb', line 51

def files
  files = []

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

  files.flatten.uniq
end

.pathsObject



62
63
64
# File 'lib/landable/layout.rb', line 62

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

Instance Method Details

#descriptionObject



42
43
44
# File 'lib/landable/layout.rb', line 42

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

#processObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/landable/layout.rb', line 11

def process
  return if @processed

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

  path.sub!(%r{^\/}, '')

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

  @body = File.read(@file)

  @processed = true
end

#to_themeObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/landable/layout.rb', line 26

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