Class: CmsLite

Inherits:
Object
  • Object
show all
Defined in:
lib/cms_lite.rb,
lib/cms_lite/tasks.rb,
lib/cms_lite/exceptions.rb

Defined Under Namespace

Modules: Exceptions Classes: Tasks

Constant Summary collapse

CONTENT_PATH =
'content'
PAGES_PATH =
'pages'
PROTECTED_PAGES_PATH =
'protected-pages'
ROOT_PATH =

pages located in this directory will be served off the root of the website. ie www.example.com/my-page

'default'

Class Method Summary collapse

Class Method Details

.append_content_path(path, reload_routes = true) ⇒ Object



82
83
84
85
# File 'lib/cms_lite.rb', line 82

def append_content_path(path, reload_routes = true)
  content_paths << path
  setup_routes if reload_routes
end

.build_route_options(action, cms_route) ⇒ Object



72
73
74
75
76
# File 'lib/cms_lite.rb', line 72

def build_route_options(action, cms_route)
  options = { :controller => 'cms_lite', :action => action, :content_key => cms_route[:content_key] }
  options = options.merge(:content_page => cms_route[:content_page]) if cms_route[:content_page]
  options
end

.check_routesObject

This is a utitility method that makes sure the unprotected routes don’t interfere with the proctected routes



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cms_lite.rb', line 105

def check_routes
  bad_routes = []
  open_routes = cms_routes
  protected_routes = protected_cms_routes
  open_routes.each do |open_route|
    protected_routes.each do |protected_route|
      if open_route[:content_key] == protected_route[:content_key]
        bad_routes << open_route
      end
    end
  end
  bad_routes
end

.cms_layoutsObject



18
19
20
# File 'lib/cms_lite.rb', line 18

def cms_layouts
  @@layouts
end

.cms_layouts=(layouts) ⇒ Object



14
15
16
# File 'lib/cms_lite.rb', line 14

def cms_layouts=(layouts)
  @@layouts = layouts
end

.cms_routesObject



22
23
24
# File 'lib/cms_lite.rb', line 22

def cms_routes
  get_all_content_path_routes(PAGES_PATH)
end

.content_pathsObject



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

def content_paths
  @@content_paths ||= [CONTENT_PATH]
end

.get_all_content_path_routes(pages_path) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/cms_lite.rb', line 30

def get_all_content_path_routes(pages_path)
  routes = []
  content_paths.each do |path|
    routes += get_cms_routes(path, pages_path)
  end
  routes
end

.get_cms_routes(content_path, pages_path) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cms_lite.rb', line 38

def get_cms_routes(content_path, pages_path)
  cms_routes = []
  cms_lite_page_path = File.join(RAILS_ROOT, content_path, pages_path)
  Dir.glob("#{cms_lite_page_path}/*").each do |localization_directory|
    if File.directory?(localization_directory)
      Dir.glob("#{localization_directory}/*").each do |content_item|
        path = File.basename(content_item)
        content_key = content_item.gsub(localization_directory, '')
        if !content_key.blank?
          # pages found in the root path get root mapping
          if path == ROOT_PATH
            Dir.glob("#{content_item}/*").each do |root_page|
              root_page_key = File.basename(File.basename(root_page, ".*"), ".*") # for files that look like root.html.erb
              cms_route = { :uri => "/#{root_page_key}",
                            :content_page => "#{root_page_key}",
                            :content_key => content_key }
              if !cms_routes.include?(cms_route)
                cms_routes << cms_route
              end
            end
          else
            cms_route = { :uri => "/#{path}/*content_page",
                          :content_key => content_key }
            if !cms_routes.include?(cms_route)
              cms_routes << cms_route
            end
          end
        end
      end
    end
  end
  cms_routes
end

.get_translated_file(page, to, from) ⇒ Object



148
149
150
151
152
153
# File 'lib/cms_lite.rb', line 148

def get_translated_file(page, to, from)
  segments = page.split('/')
  index = segments.index(from)
  segments[index] = to
  segments.join('/')
end

.prepend_content_path(path, reload_routes = true) ⇒ Object



87
88
89
90
# File 'lib/cms_lite.rb', line 87

def prepend_content_path(path, reload_routes = true)
  content_paths.unshift(path)
  setup_routes if reload_routes
end

.protected_cms_routesObject



26
27
28
# File 'lib/cms_lite.rb', line 26

def protected_cms_routes
  get_all_content_path_routes(PROTECTED_PAGES_PATH)
end

.remove_content_path(path, reload_routes = true) ⇒ Object



92
93
94
95
# File 'lib/cms_lite.rb', line 92

def remove_content_path(path, reload_routes = true)
  content_paths.delete(path)
  setup_routes if reload_routes
end

.setup_routesObject



97
98
99
100
101
102
# File 'lib/cms_lite.rb', line 97

def setup_routes
  # HACK for some reason the app routes disappear when doing a reload.  Have to add them back here so they are the first to get loaded
  app_routes = File.join(RAILS_ROOT, *%w[config routes.rb])
  ActionController::Routing::Routes.add_configuration_file(app_routes)
  ActionController::Routing::Routes.reload
end

.translate_and_write_page(page_path, to, from) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cms_lite.rb', line 136

def translate_and_write_page(page_path, to, from)
  return if to == from
  return unless File.exist?(page_path)
  translated_filename = get_translated_file(page_path, to, from)
  return if File.exist?(translated_filename) # don't overwrite existing files
  text = IO.read(page_path)
  translated_text = Babelphish::Translator.translate(text, to, from)
  translated_directory = File.dirname(translated_filename)
  FileUtils.mkdir_p(translated_directory)
  File.open(translated_filename, 'w') { |f| f.write(translated_text) }
end

.translate_and_write_pages(path, language) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/cms_lite.rb', line 124

def translate_and_write_pages(path, language)
  Dir.glob("#{path}/*").each do |next_file|
    if File.directory?(next_file)
      translate_and_write_pages(next_file, language)
    else
      GoogleTranslate::LANGUAGES.each do |to|
        translate_and_write_page(next_file, to, language)
      end
    end
  end
end

.translate_pages(language = 'en') ⇒ Object



119
120
121
122
# File 'lib/cms_lite.rb', line 119

def translate_pages(language = 'en')
  translate_and_write_pages(File.join(RAILS_ROOT, CONTENT_PATH, PAGES_PATH, language), language)
  translate_and_write_pages(File.join(RAILS_ROOT, CONTENT_PATH, PROTECTED_PAGES_PATH, language), language)
end