Class: Scribo::Site

Inherits:
ApplicationRecord show all
Defined in:
app/models/scribo/site.rb

Constant Summary collapse

NEW_SITE_NAME =
'Untitled site'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

#to_liquid

Instance Attribute Details

#zip_fileObject

Returns the value of attribute zip_file.



14
15
16
# File 'app/models/scribo/site.rb', line 14

def zip_file
  @zip_file
end

Class Method Details

.all_translation_keysObject



144
145
146
147
148
149
150
151
152
153
154
# File 'app/models/scribo/site.rb', line 144

def all_translation_keys
  parser = Scribo::LiquidParser.new
  result = {}
  Scribo::Content.includes(:site).where(content_type: Scribo.config.supported_mime_types[:text]).each do |content|
    parts = parser.parse(content.data)
    [*parts].select { |part| part.is_a?(Hash) && part[:filter] == 't' }.each do |part|
      result[content.translation_scope + part[:value].to_s] = part[:value].to_s[1..-1].humanize
    end
  end
  result
end

.default(request: nil) ⇒ Object



156
157
158
# File 'app/models/scribo/site.rb', line 156

def default(request: nil)
  Scribo.config.default_site(request) || new
end

.for_host(host) ⇒ Object



140
141
142
# File 'app/models/scribo/site.rb', line 140

def for_host(host)
  where("properties->>'host' = ? OR properties->>'host' = '' OR properties->>'host' IS NULL", host).order(Arel.sql("COALESCE(LENGTH(scribo_sites.properties->>'host'), 0) DESC"))
end

.for_path(path) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/models/scribo/site.rb', line 125

def for_path(path)
  return none if path.blank?

  # Remove any segment which does not end in /
  search_path = File.dirname(path)

  paths = []
  paths.concat(search_path.split('/').map.with_index { |_, i| search_path.split('/')[0..i].join('/') }.reject(&:empty?))
  paths <<= '/'
  paths <<= path.gsub(%r[/$], '')

  where("properties->>'baseurl' IN (?) OR properties->>'baseurl' = '' OR properties->>'baseurl' IS NULL",
        paths.uniq).order(Arel.sql("COALESCE(LENGTH(scribo_sites.properties->>'baseurl'), 0) DESC"))
end

Instance Method Details

#baseurlObject



54
55
56
# File 'app/models/scribo/site.rb', line 54

def baseurl
  properties['baseurl'] || '/'
end

#collectionsObject



33
34
35
36
37
38
39
40
41
# File 'app/models/scribo/site.rb', line 33

def collections
  result = begin
    properties['collections'].to_h.keys.map(&:to_s)
  rescue StandardError
    []
  end
  result += %w[posts]
  result
end

#defaults_for(content) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/scribo/site.rb', line 81

def defaults_for(content)
  site_defaults = properties&.[]('defaults')
  return {} unless site_defaults
  return {} unless content.full_path
  return {} if !content.page? && !content.collection_name # scoping only possible for pages & collections

  props = site_defaults.select do |d|
    s = d['scope']
    next unless s

    result = false

    if s['path']
      result = true
      p = s['path']
      unless p.include?('*')
        p = "/#{p}" unless p.start_with?('/')
        p = "#{p}/" unless p.ends_with?('/')
        p += '*'
      end

      result &= File.fnmatch?(p, content.full_path.to_s, File::FNM_EXTGLOB)
    end

    result &= s['type'] == content.type if s['type']

    result
  end
  # Sort by longest scope array (ie most specific-ish) and return the first
  props = props.min_by { |p| -p['scope'].to_a.size }

  (props || {}).fetch('values', {})
end

#filter_cacheObject



20
21
22
# File 'app/models/scribo/site.rb', line 20

def filter_cache
  @filter_cache ||= {}
end

#output_collection?(collection) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'app/models/scribo/site.rb', line 43

def output_collection?(collection)
  return true if collection.to_s == 'posts'

  col = properties['collections'].to_h[collection.to_s]
  col&.[]('output') == true
end


29
30
31
# File 'app/models/scribo/site.rb', line 29

def perma_link
  properties['permalink'] || '/:year/:month/:day/:title:output_ext'
end

#propertiesObject



69
70
71
# File 'app/models/scribo/site.rb', line 69

def properties
  attributes['properties'].present? ? attributes['properties'] : { 'title' => NEW_SITE_NAME, 'baseurl' => '/' }
end

#reshuffle!Object



24
25
26
# File 'app/models/scribo/site.rb', line 24

def reshuffle!
  contents.roots.each(&:store_full_path)
end

#sass_dirObject



73
74
75
76
77
78
79
# File 'app/models/scribo/site.rb', line 73

def sass_dir
  result = properties.value_at_keypath('sass.sass_dir') || '/_sass/'
  result = "/#{result}" unless result.start_with?('/')
  result = "#{result}/" unless result.ends_with?('/')

  result
end

#thumbnailObject

This returns the full thumbnail URL



59
60
61
62
63
# File 'app/models/scribo/site.rb', line 59

def thumbnail
  return unless properties['thumbnail']

  url + baseurl + properties['thumbnail'].to_s
end

#titleObject



50
51
52
# File 'app/models/scribo/site.rb', line 50

def title
  properties['title'] || NEW_SITE_NAME
end

#total_sizeInteger

Calculates the total size of the site in bytes, including assets

Returns:

  • (Integer)

    size in bytes



120
121
122
# File 'app/models/scribo/site.rb', line 120

def total_size
  contents.map { |c| c.data ? c.data.size : c.asset.attachment&.download&.size || 0 }.sum
end

#urlObject



65
66
67
# File 'app/models/scribo/site.rb', line 65

def url
  properties['url'].to_s
end