summaryrefslogtreecommitdiff
path: root/lib/static_page.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/static_page.rb')
-rw-r--r--lib/static_page.rb156
1 files changed, 156 insertions, 0 deletions
diff --git a/lib/static_page.rb b/lib/static_page.rb
new file mode 100644
index 0000000..12d4948
--- /dev/null
+++ b/lib/static_page.rb
@@ -0,0 +1,156 @@
+require 'lib/property_set'
+require 'i18n'
+require 'pathname'
+
+class StaticPage
+
+ class PageArray < Array
+ def limit(num)
+ PageArray.new(self[0..(num-1)])
+ end
+ def order_by(attr, locale=I18n.locale)
+ array = sort do |a,b|
+ a_prop = a.props.locale(locale).send(attr)
+ b_prop = b.props.locale(locale).send(attr)
+ if a_prop.nil? && b_prop.nil?
+ 0
+ elsif a_prop.nil?
+ 1
+ elsif b_prop.nil?
+ -1
+ else
+ a_prop <=> b_prop
+ end
+ end
+ array.delete_if do |page|
+ page.props.locale(locale).send(attr).nil?
+ end
+ end
+ end
+
+ attr_accessor :path, :children, :name, :props, :parent
+
+ def self.find(filter)
+ if filter =~ /\//
+ path = filter.split('/').map{|segment| segment.gsub(/[^0-9a-z_-]/, '')}
+ page = @@pages[path.join('/')]
+ if page
+ return page
+ else
+ return @@pages[path.last]
+ end
+ else
+ @@pages[filter]
+ end
+ end
+
+ def self.all
+ @@pages_array
+ end
+
+ def self.load(directory)
+ @@pages = {}
+ @@page_array = PageArray.new
+ @@root_directory = directory
+ @@relative_root_directory = relative_to_rails_view_root(directory)
+ scan_directory(directory) do |page|
+ @@pages[page.name] ||= page
+ @@pages[page.path.join('/')] = page
+ @@page_array << page
+ end
+ end
+
+ def initialize(parent, name)
+ @children = []
+ @name = name
+ if parent
+ @parent = parent
+ @parent.add_child(self)
+ @path = [@parent.path, @name].flatten.compact
+ @props = load_properties(file_path)
+ else
+ @path = []
+ end
+ end
+
+ def add_child(page)
+ @children << page
+ end
+
+ def all_children
+ PageArray.new(child_tree.flatten.compact)
+ end
+
+ #
+ # e.g. /home/user/dev/leap-public-site/app/views/pages/about-us/contact
+ #
+ def file_path
+ "#{@@root_directory}/#{@path.join('/')}"
+ end
+
+ #
+ # e.g. pages/about-us/contact/en
+ #
+ def template_path(locale=I18n.locale)
+ "#{@@relative_root_directory}/#{@path.join('/')}/#{locale}"
+ end
+
+ def inspect
+ "<'#{@path.join('/')}' #{children.inspect}>"
+ end
+
+ def title
+ begin
+ I18n.t!('pages.' + @name, :raise => true)
+ rescue I18n::MissingTranslationData
+ props.title
+ end
+ end
+
+ protected
+
+ def child_tree
+ [self, children.collect{|child| child.child_tree}]
+ end
+
+ private
+
+ def self.scan_directory(directory, parent=nil, &block)
+ parent ||= StaticPage.new(nil, 'root')
+ Dir.chdir directory do
+ Dir.glob("*").each do |child_dir|
+ next unless File.directory?(child_dir)
+ page = StaticPage.new(parent, child_dir)
+ yield page
+ scan_directory(child_dir, page, &block)
+ end
+ end
+ end
+
+ def self.relative_to_rails_view_root(absolute_path)
+ if Rails.root
+ absolute = Pathname.new(absolute_path)
+ rails_view_root = Pathname.new(Rails.root + 'app/views')
+ absolute.relative_path_from(rails_view_root).to_s
+ end
+ end
+
+ def load_properties(file_path)
+ props = PropertySet.new(self)
+ Dir.glob(file_path + '/*.haml') do |content_file_path|
+ locale = File.basename(content_file_path).sub(File.extname(content_file_path),'')
+ #variable_header = ""
+ #p content_file_path
+ #File.open(content_file_path) do |f|
+ # while (line = f.gets) =~ /^- @/
+ # variable_header << line
+ # end
+ #end
+ props.eval(locale, File.read(content_file_path))
+ end
+ return props
+ end
+
+end
+
+