Class: Railspress::CmsHelper::CMSQuery

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/railspress/cms_helper.rb

Overview

Chainable query class for content retrieval

Instance Method Summary collapse

Constructor Details

#initializeCMSQuery

Returns a new instance of CMSQuery.



43
44
45
46
# File 'app/helpers/railspress/cms_helper.rb', line 43

def initialize
  @group_name = nil
  @element_name = nil
end

Instance Method Details

#elementObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/railspress/cms_helper.rb', line 58

def element
  return nil unless @group_name && @element_name

  cache_key = "#{@group_name}:#{@element_name}"
  cached = CmsHelper.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)
  CmsHelper.cache[cache_key] = found if found
  found
rescue ActiveRecord::RecordNotFound
  nil
end

#find(group_name) ⇒ Object



48
49
50
51
# File 'app/helpers/railspress/cms_helper.rb', line 48

def find(group_name)
  @group_name = group_name
  self
end

#load(element_name) ⇒ Object



53
54
55
56
# File 'app/helpers/railspress/cms_helper.rb', line 53

def load(element_name)
  @element_name = element_name
  self
end

#valueObject



75
76
77
# File 'app/helpers/railspress/cms_helper.rb', line 75

def value
  element&.value
end