Class: ManageableContent::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/manageable_content/manager.rb

Class Method Summary collapse

Class Method Details

.eligible_contents(key) ⇒ Object

Retrieves a list of eligible keys for a given Page key. This can be useful to check if a PageContent is still relevant based on the current configurations.

This will return a list of page keys with it’s corresponding content type (:string or :text).



87
88
89
90
91
92
93
94
95
96
# File 'lib/manageable_content/manager.rb', line 87

def self.eligible_contents(key)
  layout_content_keys = Controllers::Dsl.manageable_layout_content_keys[key] || {}
  content_keys        = begin
    "#{key.camelize}Controller".constantize.manageable_content_keys
  rescue NameError
    ManageableContent::Engine.config.custom_pages[key] || {}
  end

  layout_content_keys.merge(content_keys)
end

.eligible_controllersObject

Retrieves a list of Controllers eligible for having manageable content. A Controller is eligible if it has set contents with :manageable_content_for. This method is cached if Rails.configuration.cache_classes is true.



7
8
9
10
11
12
13
# File 'lib/manageable_content/manager.rb', line 7

def self.eligible_controllers
  if Rails.configuration.cache_classes
    @@eligible_controllers ||= find_eligible_controllers
  else
    find_eligible_controllers
  end
end

.eligible_customObject

Retrieves a list of custom pages eligible for having manageable content. A page is eligible if it is configured on ManageableContent::Engine.config.custom_pages. This method is cached if Rails.configuration.cache_classes is true.



18
19
20
21
22
23
24
# File 'lib/manageable_content/manager.rb', line 18

def self.eligible_custom
  if Rails.configuration.cache_classes
    @@eligible_custom ||= ManageableContent::Engine.config.custom_pages.keys.sort
  else
    ManageableContent::Engine.config.custom_pages.keys.sort
  end
end

.generate!Object

Generates a Page and PageContent for each Controller or custom page with manageable content keys, and the layout Page for manageable layout content keys. Custom pages can be defined within an initializer with the following code:

 ManageableContent::Engine.config.custom_pages = {
  "static/page1" => [:body],
  "static/page2" => [:body, :footer]
}


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/manageable_content/manager.rb', line 34

def self.generate!
  controllers = eligible_controllers

  Engine.config.locales.each do |locale|
    # layout page
    Controllers::Dsl.manageable_layout_content_keys.each_key do |layout|
      generate_page! layout,
                          locale,
                          Controllers::Dsl.manageable_layout_content_keys[layout]
    end

    # controllers pages
    controllers.each do |controller_class|
      generate_page! controller_class.controller_path,
                          locale,
                          controller_class.manageable_content_keys
    end

    # custom pages
    ManageableContent::Engine.config.custom_pages.each do |custom_page, content_keys|
      generate_page! custom_page,
                          locale,
                          content_keys
    end
  end

  controllers
end

.page(key, locale = I18n.locale) ⇒ Object

Retrieves a Page relation for the given key and locale. By default I18n.locale is used as the locale option.



78
79
80
# File 'lib/manageable_content/manager.rb', line 78

def self.page(key, locale = I18n.locale)
  Page.with_contents.where(:key => key, :locale => locale)
end

.pagesObject

Retrieves a Page relation with a filter for eligible Pages. A Page is eligible if the corresponding controller is still eligible (from the eligible_controllers method).

This method should be used to access a list of valid Pages instead of directly accessing the Page model.



69
70
71
72
73
74
# File 'lib/manageable_content/manager.rb', line 69

def self.pages
  eligible_keys = eligible_controllers.map {|controller_class| controller_class.controller_path } +
                    eligible_custom

  Page.where(:key => eligible_keys)
end