Module: TinyCMS::Node

Defined in:
lib/tiny_cms/node.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

@@uuid =
UUID.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tiny_cms/node.rb', line 14

def self.included model
  model.extend ClassMethods
  
  model.named_scope :include_tree, lambda { |depth|
    children = :children 
    2.upto(depth){ children = {:children => children} }
    {:include => children}
  }
  
  model.named_scope :by_path, lambda { |path|
    next {:conditions => {:permalink => path.last}} if path.size - 1 == 0
    parents = :parent
    2.upto(path.size - 1){ parents = {:parent => parents} }
    {:joins => parents, :conditions => {:permalink => path.last, 'parents_pages.permalink' => path[-2]}}
  }
  
  model.named_scope :roots,   :conditions => {:parent_id => nil}, :order => 'position'
  
  model.named_scope :by_parent_id, lambda { |parent_id|
    {:conditions => {:parent_id => parent_id}}
  }
  
  model.named_scope :exclude_by_id, lambda { |id| {:conditions => "id != #{id}"} }
  
  model.belongs_to :parent,   :class_name => model.to_s, :foreign_key => 'parent_id'
  model.has_many   :children, :class_name => model.to_s, :foreign_key => 'parent_id', :order => 'position', :dependent => :destroy
  
  model.validates_presence_of   :title
  model.validates_uniqueness_of :permalink, :scope  => [:parent_id]

  model.validates_associated :children

  model.before_validation :parameterize_permalink
  
  model.send :attr_accessor, :rel # Does nothing but jstree sends attribute
  
  model.send :define_method, :children_with_position= do |array|
    # TODO: Too unefficient
    transaction { array.each_with_index{ |child, index| child.update_attributes! :position => index, :parent_id => id } } rescue nil
    self.children_without_position = array
  end
  model.alias_method_chain :children=, :position

  if model.columns.find { |c| c.name == 'dynamic_route' }
    # dynamic routing
    model.before_save   :update_dynamic_route!
    model.after_destroy :remove_dynamic_route!
    model.find(:all, "dynamic_route IS NOT nil").each(&:update_dynamic_route!)
  end
end

Instance Method Details

#add_dynamic_route!Object

Dynamic routing



93
94
95
96
97
98
99
100
101
# File 'lib/tiny_cms/node.rb', line 93

def add_dynamic_route!
  controller, action      = dynamic_route.split('#')
  self.dynamic_route_uuid = @@uuid.generate
  action, params          = action.split('?') 
  route                   = {:controller => controller, :action => action, :dynamic_route_uuid => dynamic_route_uuid}
  route.merge! Rack::Utils.parse_query(params) if params
  new_route = ActionController::Routing::Routes.builder.build(path, route)
  ActionController::Routing::Routes.routes.unshift new_route
end


67
68
69
70
# File 'lib/tiny_cms/node.rb', line 67

def parameterize_permalink
  text = permalink.blank? ? title : permalink
  self.permalink = text.parameterize if text
end

#pathObject



72
73
74
# File 'lib/tiny_cms/node.rb', line 72

def path
  "#{ parent.path if parent }/#{ permalink }"
end

#remove_dynamic_route!Object



103
104
105
# File 'lib/tiny_cms/node.rb', line 103

def remove_dynamic_route!
  ActionController::Routing::Routes.routes.reject! { |r| r.instance_variable_get(:@requirements)[:dynamic_route_uuid] == dynamic_route_uuid } unless dynamic_route_uuid.blank?
end

#sectionObject



84
85
86
# File 'lib/tiny_cms/node.rb', line 84

def section
  self.class.by_parent_id parent_id
end

#siblingsObject



80
81
82
# File 'lib/tiny_cms/node.rb', line 80

def siblings
  section.exclude_by_id self.id
end

#to_hashObject



76
77
78
# File 'lib/tiny_cms/node.rb', line 76

def to_hash
  {:attributes => {'data-node-id' => id, 'data-permalink' => permalink, 'data-path' => path}, :data => title, :children => children.map{ |c| c.to_hash } }
end

#to_json(opts = {}) ⇒ Object



88
89
90
# File 'lib/tiny_cms/node.rb', line 88

def to_json opts = {}
  self.to_hash.to_json
end

#update_dynamic_route!Object



107
108
109
110
# File 'lib/tiny_cms/node.rb', line 107

def update_dynamic_route!
  remove_dynamic_route!
  add_dynamic_route! unless dynamic_route.blank?
end