lib/spontaneous/site/selectors.rb in spontaneous-0.2.0.beta4 vs lib/spontaneous/site/selectors.rb in spontaneous-0.2.0.beta5
- old
+ new
@@ -2,72 +2,78 @@
class Spontaneous::Site
module Selectors
extend Spontaneous::Concern
- module ClassMethods
- def root(content_model = Spontaneous::Content)
- content_model.root
- end
+ def home
+ model.root
+ end
- # roots returns the list of top-level pages
- # Only one of these is publicly visible and this is mapped to the
- # configured site domain.
- #
- # The rest are "hidden" roots.
- def roots(user = nil, content_model = Spontaneous::Content)
- domain = config.site_domain
- roots = pages_dataset(content_model).where(depth: 0).all
- pub, hidden = roots.partition { |p| p.root? }
- map = {}
- map[domain] = pub.first.id unless pub.empty?
- hidden.each { |p| map[p.path] = p.id }
- { "public" => domain, "roots" => map }
- end
+ # roots returns the list of top-level pages
+ # Only one of these is publicly visible and this is mapped to the
+ # configured site domain.
+ #
+ # The rest are "hidden" roots.
+ def roots(user = nil)
+ domain = config.site_domain
+ roots = pages_dataset.where(depth: 0).all
+ pub, hidden = roots.partition { |p| p.root? }
+ map = {}
+ map[domain] = pub.first.id unless pub.empty?
+ hidden.each { |p| map[p.path] = p.id }
+ { "public" => domain, "roots" => map }
+ end
- def pages(content_model = Spontaneous::Content)
- pages_dataset(content_model).all
- end
+ def pages
+ pages_dataset.all
+ end
- def pages_dataset(content_model = Spontaneous::Content)
- content_model::Page.order(:depth)
- end
+ def pages_dataset
+ model::Page.order(:depth)
+ end
- ID_PATH = /\A\d+\z/o
- PATH_PATH = /^[\/#]/o
- UID_PATH = /^\$/o
+ ID_SELECTOR = /\A\d+\z/o
+ PATH_SELECTOR = /\A[\/#]/o
+ UID_SELECTOR = /\A\$/o
- def [](path_or_uid)
- case path_or_uid
- when Fixnum, ID_PATH
- by_id(path_or_uid)
- when PATH_PATH
- by_path(path_or_uid)
- when UID_PATH
- by_uid(path_or_uid[1..-1])
- else
- by_uid(path_or_uid)
- end
- end
+ def [](selector)
+ fetch(selector)
+ end
- def by_id(id)
- Spontaneous::Content[id]
+ def fetch(selector)
+ case selector
+ when Symbol
+ by_uid(selector.to_s)
+ when Fixnum
+ by_id(selector)
+ when ID_SELECTOR
+ by_id(selector)
+ when PATH_SELECTOR
+ by_path(selector)
+ when UID_SELECTOR
+ by_uid(selector[1..-1])
+ else
+ by_uid(selector)
end
+ end
- def by_path(path)
- Spontaneous::Content.path(path)
- end
+ def by_id(id)
+ model.id(id)
+ end
- def by_uid(uid)
- Spontaneous::Content.uid(uid)
- end
+ def by_path(path)
+ model.path(path)
+ end
- def method_missing(method, *args)
- if page = self[method.to_s]
- page
- else
- super
- end
+ def by_uid(uid)
+ model.uid(uid)
+ end
+
+ def method_missing(method, *args)
+ if (page = fetch(method))
+ page
+ else
+ super
end
- end # ClassMethods
+ end
end
end