Class: Noodall::Node

Inherits:
Object show all
Includes:
Canable::Ables, MongoMapper::Acts::Tree, MongoMapper::Document
Defined in:
lib/noodall/node.rb

Defined Under Namespace

Classes: SlotValidator

Constant Summary collapse

@@slots =
[]
@@root_templates =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hideObject

for publishing



37
38
39
# File 'lib/noodall/node.rb', line 37

def hide
  @hide
end

#movedObject

for redordering



38
39
40
# File 'lib/noodall/node.rb', line 38

def moved
  @moved
end

#previous_parent_idObject

for redordering



38
39
40
# File 'lib/noodall/node.rb', line 38

def previous_parent_id
  @previous_parent_id
end

#publishObject

for publishing



37
38
39
# File 'lib/noodall/node.rb', line 37

def publish
  @publish
end

Class Method Details

.all_template_classesObject

Returns a lst of all node template classes available in in the tree



411
412
413
414
415
416
417
418
# File 'lib/noodall/node.rb', line 411

def all_template_classes
  templates = []
  root_templates.each do |template|
    templates << template
    templates = templates + template.template_classes
  end
  templates.uniq
end

.all_template_namesObject



420
421
422
# File 'lib/noodall/node.rb', line 420

def all_template_names
  all_template_classes.map{|c| c.name.titleize }.sort
end

.current_timeObject

If rails style time zones are unavaiable fallback to standard now



462
463
464
# File 'lib/noodall/node.rb', line 462

def current_time
  Time.zone ? Time.zone.now : Time.now
end

Raises:

  • (MongoMapper::DocumentNotFound)


394
395
396
397
398
# File 'lib/noodall/node.rb', line 394

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_classesObject

Returns a list of classes that can have this model as a child



455
456
457
458
459
# File 'lib/noodall/node.rb', line 455

def parent_classes
  all_template_classes.find_all do |c|
    c.template_classes.include?(self)
  end
end

.possible_slotsObject



386
387
388
# File 'lib/noodall/node.rb', line 386

def possible_slots
  @@slots
end

.root_template!Object

DEPRECATED: Please use <tt>root_templates/tt> instead.



445
446
447
448
# File 'lib/noodall/node.rb', line 445

def root_template!
  warn "[DEPRECATION] `root_template` is deprecated.  Please use `root_templates` instead."
  @@root_templates << self
end

.root_template?Boolean

Returns:

  • (Boolean)


450
451
452
# File 'lib/noodall/node.rb', line 450

def root_template?
  @@root_templates.include?(self)
end

.root_templates(*templates) ⇒ Object

Set the Node templates that can be a root of a tree

Noodall::Node.root_templates Home, LandingPage

Returns a list of the root templates

Noodall::Node.root_templates # => [Home, LandingPage]



439
440
441
442
# File 'lib/noodall/node.rb', line 439

def root_templates(*templates)
  @@root_templates = templates unless templates.empty?
  @@root_templates
end

.roots(options = {}) ⇒ Object



390
391
392
# File 'lib/noodall/node.rb', line 390

def roots(options = {})
  self.where(options.reverse_merge({parent_id_field => nil})).order(tree_order)
end

.slot(slot_name, *allowed_components) ⇒ Object

Define a slot type and what components are allowed to be place in that slot type.

Generates methods in Noodall::Node models that allow you to set and read the number of slots of the name defined

Noodall::Node.slot :small, Gallery, Picture

class NicePage < Noodall::Node
  small_slots 3
end

NicePage.small_slots_count  # => 3

n = NicePage.new
n.small_slot_0 = Gallery.new(...)


355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/noodall/node.rb', line 355

def slot(slot_name, *allowed_components)
  if @@slots.include?(slot_name.to_sym)
    warn "[WARNING] Overriding slot definition"
  else
    @@slots << slot_name.to_sym
    puts "Noodall::Node Defined slot: #{slot_name}"
    define_singleton_method("#{slot_name}_slots") do |count|
      instance_variable_set("@#{slot_name}_slots_count", count)
      count.times do |i|
        slot_sym = "#{slot_name}_slot_#{i}".to_sym
        key slot_sym, Noodall::Component
        validates slot_sym, :slot => { :slot_type => slot_name }
        validates_associated slot_sym
      end
    end

    define_singleton_method("#{slot_name}_slot_components") do
      class_variable_get "@@#{slot_name}_slot_components".to_sym
    end

    define_singleton_method("#{slot_name}_slots_count") do
      instance_variable_get("@#{slot_name}_slots_count")
    end
  end
  class_variable_set "@@#{slot_name}_slot_components".to_sym, allowed_components
end

.slots(*args) ⇒ Object

DEPRECATED: Please use slot instead.

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


329
330
331
332
333
334
335
336
# File 'lib/noodall/node.rb', line 329

def slots(*args)
  warn "[DEPRECATION] `slots` is deprecated.  Please use `slot` instead."
  slots = args.map(&:to_sym).uniq

  slots.each do |s|
    slot(s)
  end
end

.slots_countObject



382
383
384
# File 'lib/noodall/node.rb', line 382

def slots_count
  @@slots.inject(0) { |total, slot| total + send("#{slot}_slots_count").to_i }
end

.sub_templates(*arr) ⇒ Object

Set the Node templates that can be a child of this templates in the tree



426
427
428
# File 'lib/noodall/node.rb', line 426

def sub_templates(*arr)
  @template_classes = arr
end

.template_classesObject



400
401
402
403
# File 'lib/noodall/node.rb', line 400

def template_classes
  return root_templates if self == Noodall::Node
  @template_classes || []
end

.template_namesObject



405
406
407
# File 'lib/noodall/node.rb', line 405

def template_names
  template_classes.map{|c| c.name.titleize }.sort
end

Instance Method Details

#admin_titleObject



165
166
167
# File 'lib/noodall/node.rb', line 165

def admin_title
  name
end

#all_groupsObject

CANS



122
123
124
# File 'lib/noodall/node.rb', line 122

def all_groups
  updatable_groups | destroyable_groups | publishable_groups | viewable_groups
end

#childrenObject



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

Returns:

  • (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

Returns:

  • (Boolean)


77
78
79
# File 'lib/noodall/node.rb', line 77

def expired?
  !published_to.nil? and published_to <= current_time
end

#first?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/noodall/node.rb', line 81

def first?
  position == 0
end

#has_draft?Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


85
86
87
# File 'lib/noodall/node.rb', line 85

def last?
  position == siblings.count
end

#move_higherObject



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_lowerObject



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

Returns:

  • (Boolean)


73
74
75
# File 'lib/noodall/node.rb', line 73

def pending?
  published_at.nil? or published_at >= current_time
end

#published?Boolean

Returns:

  • (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_childrenObject



47
48
49
# File 'lib/noodall/node.rb', line 47

def published_children
  self.children.select{|c| c.published? }
end

#self_and_siblingsObject



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

#siblingsObject



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

#slotsObject

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

#slugObject

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

#templateObject



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