Class: Noodall::Node
- Includes:
- Canable::Ables, MongoMapper::Acts::Tree, MongoMapper::Document
- Defined in:
- lib/noodall/node.rb
Defined Under Namespace
Classes: SlotValidator
Constant Summary collapse
- @@slots =
[]
Instance Attribute Summary collapse
-
#hide ⇒ Object
for publishing.
-
#moved ⇒ Object
for redordering.
-
#previous_parent_id ⇒ Object
for redordering.
-
#publish ⇒ Object
for publishing.
Class Method Summary collapse
- .all_template_classes ⇒ Object
-
.current_time ⇒ Object
If rails style time zones are unavaiable fallback to standard now.
- .find_by_permalink(permalink) ⇒ Object
-
.parent_classes ⇒ Object
Returns a list of classes that can have this model as a child.
- .possible_slots ⇒ Object
- .root_template! ⇒ Object
- .root_template? ⇒ Boolean
- .root_templates ⇒ Object
- .roots(options = {}) ⇒ Object
-
.slots(*args) ⇒ Object
Set the names of the slots that will be avaiable to fill with components For each name new methods will be created;.
- .slots_count ⇒ Object
- .sub_templates(*arr) ⇒ Object
- .template_classes ⇒ Object
- .template_names ⇒ Object
Instance Method Summary collapse
- #admin_title ⇒ Object
-
#all_groups ⇒ Object
CANS.
- #children ⇒ Object
- #creatable_by?(user) ⇒ Boolean
- #expired? ⇒ Boolean
- #first? ⇒ Boolean
- #has_draft? ⇒ Boolean
- #in_site_map? ⇒ Boolean
- #last? ⇒ Boolean
- #move_higher ⇒ Object
- #move_lower ⇒ Object
-
#parent=(var) ⇒ Object
Allow parent to be set to none using a string.
- #pending? ⇒ Boolean
- #published? ⇒ Boolean
- #published_children ⇒ Object
- #self_and_siblings ⇒ Object
- #siblings ⇒ Object
-
#slots ⇒ Object
end.
-
#slug ⇒ Object
A slug for creating the permalink.
- #template ⇒ Object
- #template=(template_name) ⇒ Object
Instance Attribute Details
#previous_parent_id ⇒ Object
for redordering
38 39 40 |
# File 'lib/noodall/node.rb', line 38 def previous_parent_id @previous_parent_id end |
#publish ⇒ Object
for publishing
37 38 39 |
# File 'lib/noodall/node.rb', line 37 def publish @publish end |
Class Method Details
.all_template_classes ⇒ Object
382 383 384 385 386 387 388 389 |
# File 'lib/noodall/node.rb', line 382 def all_template_classes templates = [] template_classes.each do |template| templates << template templates = templates + template.template_classes end templates.uniq.collect{ |c| c.name.titleize }.sort end |
.current_time ⇒ Object
If rails style time zones are unavaiable fallback to standard now
414 415 416 |
# File 'lib/noodall/node.rb', line 414 def current_time Time.zone ? Time.zone.now : Time.now end |
.find_by_permalink(permalink) ⇒ Object
357 358 359 360 361 |
# File 'lib/noodall/node.rb', line 357 def find_by_permalink(permalink) node = find_one(:permalink => permalink.to_s, :published_at => { :$lte => current_time }) raise MongoMapper::DocumentNotFound if node.nil? or node.expired? node end |
.parent_classes ⇒ Object
Returns a list of classes that can have this model as a child
404 405 406 407 408 409 410 411 |
# File 'lib/noodall/node.rb', line 404 def parent_classes classes = [] ObjectSpace.each_object(Class) do |c| next unless c.ancestors.include?(Noodall::Node) and (c != Noodall::Node) and c.template_classes.include?(self) classes << c end classes end |
.possible_slots ⇒ Object
349 350 351 |
# File 'lib/noodall/node.rb', line 349 def possible_slots @@slots end |
.root_template! ⇒ Object
395 396 397 |
# File 'lib/noodall/node.rb', line 395 def root_template! @root_template = true end |
.root_template? ⇒ Boolean
399 400 401 |
# File 'lib/noodall/node.rb', line 399 def root_template? @root_template end |
.root_templates ⇒ Object
368 369 370 371 372 373 374 375 376 |
# File 'lib/noodall/node.rb', line 368 def root_templates return @root_templates if @root_templates classes = [] ObjectSpace.each_object(Class) do |c| next unless c.ancestors.include?(Noodall::Node) and (c != Noodall::Node) and c.root_template? classes << c end @root_templates = classes end |
.roots(options = {}) ⇒ Object
353 354 355 |
# File 'lib/noodall/node.rb', line 353 def roots( = {}) self.where(.reverse_merge({parent_id_field => nil})).order(tree_order) end |
.slots(*args) ⇒ Object
Set the names of the slots that will be avaiable to fill with components For each name new methods will be created;
<name>_slots(count)
This allow you to set the number of slots available in a template
<name>_slots_count(count)
Reads back the count you set
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/noodall/node.rb', line 327 def slots(*args) @@slots = args.map(&:to_sym).uniq @@slots.each do |slot| puts "Noodall::Node Defined slot: #{slot}" define_singleton_method("#{slot}_slots") do |count| instance_variable_set("@#{slot}_slots_count", count) count.times do |i| slot_sym = "#{slot}_slot_#{i}".to_sym key slot_sym, Noodall::Component validates slot_sym, :slot => { :slot_type => slot } validates_associated slot_sym end end define_singleton_method("#{slot}_slots_count") { instance_variable_get("@#{slot}_slots_count") } end end |
.slots_count ⇒ Object
345 346 347 |
# File 'lib/noodall/node.rb', line 345 def slots_count @@slots.inject(0) { |total, slot| total + send("#{slot}_slots_count").to_i } end |
.sub_templates(*arr) ⇒ Object
391 392 393 |
# File 'lib/noodall/node.rb', line 391 def sub_templates(*arr) @template_classes = arr end |
.template_classes ⇒ Object
363 364 365 366 |
# File 'lib/noodall/node.rb', line 363 def template_classes return root_templates if self == Noodall::Node @template_classes || [] end |
.template_names ⇒ Object
378 379 380 |
# File 'lib/noodall/node.rb', line 378 def template_names template_classes.collect{|c| c.name.titleize}.sort end |
Instance Method Details
#admin_title ⇒ Object
165 166 167 |
# File 'lib/noodall/node.rb', line 165 def admin_title name end |
#all_groups ⇒ Object
CANS
122 123 124 |
# File 'lib/noodall/node.rb', line 122 def all_groups updatable_groups | destroyable_groups | publishable_groups | viewable_groups end |
#children ⇒ Object
152 153 154 |
# File 'lib/noodall/node.rb', line 152 def children search_class.where(parent_id_field => self._id).order(tree_order) end |
#creatable_by?(user) ⇒ Boolean
140 141 142 |
# File 'lib/noodall/node.rb', line 140 def creatable_by?(user) parent.nil? or parent.updatable_by?(user) end |
#expired? ⇒ Boolean
77 78 79 |
# File 'lib/noodall/node.rb', line 77 def expired? !published_to.nil? and published_to <= current_time end |
#first? ⇒ Boolean
81 82 83 |
# File 'lib/noodall/node.rb', line 81 def first? position == 0 end |
#has_draft? ⇒ Boolean
69 70 71 |
# File 'lib/noodall/node.rb', line 69 def has_draft? !version_at(:latest).nil? && version_at(:latest).pos != version_number end |
#in_site_map? ⇒ Boolean
156 157 158 |
# File 'lib/noodall/node.rb', line 156 def in_site_map? Noodall::Site.contains?(self.permalink.to_s) end |
#last? ⇒ Boolean
85 86 87 |
# File 'lib/noodall/node.rb', line 85 def last? position == siblings.count end |
#move_higher ⇒ Object
94 95 96 97 |
# File 'lib/noodall/node.rb', line 94 def move_higher sibling = search_class.first(:position => {"$lt" => self.position}, parent_id_field => self[parent_id_field], :order => 'position DESC') switch_position(sibling) end |
#move_lower ⇒ Object
89 90 91 92 |
# File 'lib/noodall/node.rb', line 89 def move_lower sibling = search_class.first(:position => {"$gt" => self.position}, parent_id_field => self[parent_id_field], :order => 'position ASC') switch_position(sibling) end |
#parent=(var) ⇒ Object
Allow parent to be set to none using a string. Allows us to set the parent to nil easily via forms
52 53 54 55 |
# File 'lib/noodall/node.rb', line 52 def parent=(var) self[parent_id_field] = nil var == "none" ? super(nil) : super end |
#pending? ⇒ Boolean
73 74 75 |
# File 'lib/noodall/node.rb', line 73 def pending? published_at.nil? or published_at >= current_time end |
#published? ⇒ Boolean
65 66 67 |
# File 'lib/noodall/node.rb', line 65 def published? !published_at.nil? and published_at <= current_time and (published_to.nil? or published_to >= current_time) end |
#published_children ⇒ Object
47 48 49 |
# File 'lib/noodall/node.rb', line 47 def published_children self.children.select{|c| c.published? } end |
#self_and_siblings ⇒ Object
148 149 150 |
# File 'lib/noodall/node.rb', line 148 def self_and_siblings search_class.where(parent_id_field => self[parent_id_field]).order(tree_order) end |
#siblings ⇒ Object
144 145 146 |
# File 'lib/noodall/node.rb', line 144 def siblings search_class.where(:_id => {:$ne => self._id}, parent_id_field => self[parent_id_field]).order(tree_order) end |
#slots ⇒ Object
end
111 112 113 114 115 116 117 118 119 |
# File 'lib/noodall/node.rb', line 111 def slots slots = [] for slot_type in self.class.possible_slots.map(&:to_s) self.class.send("#{slot_type}_slots_count").to_i.times do |i| slots << self.send("#{slot_type}_slot_#{i}") end end slots.compact end |
#slug ⇒ Object
A slug for creating the permalink
161 162 163 |
# File 'lib/noodall/node.rb', line 161 def slug (self.name.blank? ? self.title : self.name).to_s.parameterize end |
#template ⇒ Object
57 58 59 |
# File 'lib/noodall/node.rb', line 57 def template self.class.name.titleize end |
#template=(template_name) ⇒ Object
61 62 63 |
# File 'lib/noodall/node.rb', line 61 def template=(template_name) self._type = template_name.gsub(' ','') unless template_name.blank? end |