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 =
[]

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



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_timeObject

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

Raises:

  • (MongoMapper::DocumentNotFound)


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_classesObject

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_slotsObject



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

Returns:

  • (Boolean)


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

def root_template?
  @root_template
end

.root_templatesObject



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(options = {})
  self.where(options.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_countObject



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_classesObject



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_namesObject



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_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