Class: Plum::Site

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.first_or_create_standalone!(skip_defaults: false) ⇒ Object



27
28
29
# File 'app/models/plum/site.rb', line 27

def self.first_or_create_standalone!(skip_defaults: false)
  first_or_create!(name: "My Site", theme_name: "default", skip_defaults: skip_defaults)
end

.for_owner!(owner, attributes = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/plum/site.rb', line 31

def self.for_owner!(owner, attributes = {})
  unless owner.respond_to?(:persisted?) && owner.persisted?
    raise ArgumentError, "Owner must be a persisted Active Record model"
  end

  find_or_create_by!(owner: owner) do |site|
    site.name = attributes.fetch(:name) { owner_default_name(owner) }
    site.theme_name = attributes.fetch(:theme_name, "default")
    site.skip_defaults = attributes.fetch(:skip_defaults, false)
    site.domain = attributes[:domain] if attributes.key?(:domain)
    site.settings = attributes[:settings] if attributes.key?(:settings)
    site.theme_settings = attributes[:theme_settings] if attributes.key?(:theme_settings)
    site.custom_css = attributes[:custom_css] if attributes.key?(:custom_css)
  end
end

Instance Method Details

#default_localeObject



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

def default_locale
  configured = settings.to_h["default_locale"].to_s
  locales.include?(configured) ? configured : locales.first
end

#localesObject



55
56
57
58
# File 'app/models/plum/site.rb', line 55

def locales
  configured = Array(settings.to_h["locales"]).map(&:to_s).select { |locale| locale.match?(/\A[a-z]{2}(?:-[A-Z]{2})?\z/) }
  configured.presence || [ "en" ]
end

#localized?Boolean

Returns:

  • (Boolean)


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

def localized?
  locales.many?
end

#themeObject



47
48
49
# File 'app/models/plum/site.rb', line 47

def theme
  ThemeRegistry.new.fetch(theme_name)
end

#theme_settingsObject



51
52
53
# File 'app/models/plum/site.rb', line 51

def theme_settings
  super || {}
end