Module: Ecm::Cms::DatabaseResolver

Included in:
PageResolver, PartialResolver, TemplateResolver
Defined in:
lib/ecm/cms/database_resolver.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Include hook for class methods



5
6
7
# File 'lib/ecm/cms/database_resolver.rb', line 5

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#assert_slashs(prefix) ⇒ Object



71
72
73
74
75
76
# File 'lib/ecm/cms/database_resolver.rb', line 71

def assert_slashs(prefix)
  output = prefix.dup
  output << '/' unless output.end_with?('/')
  output = '/' << output unless output.start_with?('/')
  output
end

#build_sourceObject



83
84
85
# File 'lib/ecm/cms/database_resolver.rb', line 83

def build_source
  fail 'call to abstract method #build_source'
end

#find_templates(name, prefix, partial, details, outside_app_allowed = false) ⇒ Object

instance methods go here



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ecm/cms/database_resolver.rb', line 14

def find_templates(name, prefix, partial, details, outside_app_allowed = false)
  return [] unless resolve(partial)

  conditions = {
    pathname: assert_slashs(prefix.to_s),
    basename: normalize_basename(name),
    locale: normalize_array(details[:locale]).first,
    format: normalize_array(details[:formats]).first,
    handler: normalize_array(details[:handlers])
  }

  format = conditions.delete(:format)
  locale = conditions.delete(:locale)

  query  = template_class.constantize.where(conditions)

  # 2) Check for templates with the given format or format is nil
  query = query.where(["format = ? OR format = '' OR format IS NULL", format])

  # 3) Ensure templates with format come first
  query = query.order('format DESC')

  # 4) Check for templates with the given locale or locale is nil
  query = query.where(["locale = ? OR locale = '' OR locale IS NULL", locale])

  # 5) Ensure templates with locale come first
  query = query.order('locale DESC')

  # 6) Now trigger the query passing on conditions to initialization
  query.map do |record|
    initialize_template(record, details)
  end
end

#initialize_template(record, details) ⇒ Object

Initialize an ActionView::Template object based on the record found.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ecm/cms/database_resolver.rb', line 49

def initialize_template(record, details)
  source     = build_source(record)
  identifier = "#{record.class} - #{record.id} - #{record.pathname}#{record.basename}"
  handler    = ::ActionView::Template.registered_template_handler(record.handler)

  # 5) Check for the record.format, if none is given, try the template
  # handler format and fallback to the one given on conditions
  format   = record.format && Mime[record.format]
  format ||= handler.default_format if handler.respond_to?(:default_format)
  format ||= details[:formats]

  details = {
    format: format,
    updated_at: record.updated_at,
    virtual_path: "#{record.pathname}#{record.basename}"
  }

  details[:layout] = record.layout if record.respond_to?(:layout) && record.layout.present?

  ::ActionView::Template.new(source, identifier, handler, details)
end

#normalize_array(array) ⇒ Object

Normalize arrays by converting all symbols to strings.



79
80
81
# File 'lib/ecm/cms/database_resolver.rb', line 79

def normalize_array(array)
  array.map(&:to_s)
end

#normalize_basename(_basename) ⇒ Object



87
88
89
# File 'lib/ecm/cms/database_resolver.rb', line 87

def normalize_basename(_basename)
  fail 'call to abstract method #normalize_basename'
end

#resolve(_partial_flag) ⇒ Object



91
92
93
# File 'lib/ecm/cms/database_resolver.rb', line 91

def resolve(_partial_flag)
  fail 'call to abstract method #resolve'
end

#template_classObject



95
96
97
# File 'lib/ecm/cms/database_resolver.rb', line 95

def template_class
  fail 'call to abstract method #template_class'
end