Class: Railspress::CMS::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/railspress/cms.rb

Instance Method Summary collapse

Constructor Details

#initializeQuery

Returns a new instance of Query.



11
12
13
14
# File 'lib/railspress/cms.rb', line 11

def initialize
  @group_name = nil
  @element_name = nil
end

Instance Method Details

#elementObject



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

def element
  return nil unless @group_name && @element_name

  cache_key = "#{@group_name}:#{@element_name}"
  cached = CMS.cache[cache_key]
  return cached if cached

  group = Railspress::ContentGroup.active.find_by(name: @group_name)
  return nil unless group

  found = group.content_elements.active.find_by(name: @element_name)
  CMS.cache[cache_key] = found if found
  found
rescue ActiveRecord::RecordNotFound
  nil
end

#find(group_name) ⇒ Object



16
17
18
19
# File 'lib/railspress/cms.rb', line 16

def find(group_name)
  @group_name = group_name
  self
end

#load(element_name) ⇒ Object



21
22
23
24
# File 'lib/railspress/cms.rb', line 21

def load(element_name)
  @element_name = element_name
  self
end

#valueObject



43
44
45
# File 'lib/railspress/cms.rb', line 43

def value
  element&.value
end