Module: Classiccms::Helpers

Defined in:
lib/classiccms/helpers.rb

Instance Method Summary collapse

Instance Method Details

#add(*items) ⇒ Object

returns the html for add button



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/classiccms/helpers.rb', line 89

def add(*items)
  items.each do |item|
    if item.class == Array
      item[1] = get_parent_id(item[1])
    end
  end
  if @user != nil and @user.admin?(@routes)
    info = Base64.encode64(items.to_s.encrypt)
    show :add, {views: File.join(Classiccms::ROOT, 'views/cms')}, {encrypteddata: info}
  end
end

#cmsObject

insert the header for CMS to work



10
11
12
13
14
# File 'lib/classiccms/helpers.rb', line 10

def cms
  if @user != nil and @user.admin?(@routes)
    show :header, views: File.join(Classiccms::ROOT, 'views/cms')
  end
end

#content_for(key, *args, &block) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/classiccms/helpers.rb', line 53

def content_for(key, *args, &block)
  @sections ||= Hash.new{ |k,v| k[v] = [] }
  if block_given?
    @sections[key] << block
  else
    @sections[key].inject(''){ |content, block| content << block.call(*args) } if @sections.keys.include?(key)
  end
end

#edit(id) ⇒ Object

returns the html for edit button



102
103
104
105
106
107
108
109
# File 'lib/classiccms/helpers.rb', line 102

def edit(id)
  records = Base.where(_id: id)
  if records.count > 0 and @user != nil and @user.admin?(@routes)
    info = Base64.encode64 records.first.id.to_s.encrypt

    show :edit, {views: File.join(Classiccms::ROOT, 'views/cms')}, {encrypteddata: info}
  end
end

#get_unique_number(hash, number) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/classiccms/helpers.rb', line 81

def get_unique_number(hash, number)
  if hash[number] == nil
    return number
  else
    return get_unique_number(hash, number+1)
  end
end

#layout(section_name, position) ⇒ Object

renders a specific page



42
43
44
45
46
47
48
49
50
51
# File 'lib/classiccms/helpers.rb', line 42

def layout(section_name, position)
  id = get_parent_id(position)
  records = Base.where(:_id => id)
  if records.count > 0 and records.first.connections.where(:section => section_name, :file.ne => nil).count > 0
    file_name = records.first.connections.where(:section => section_name, :file.ne => nil).first.file
    show file_name, {views: ["app/views/#{records.first._type}", "app/views/#{records.first._type.downcase}"]}, {record: records.first}
  else
    '404'
  end
end

generates url for a document



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/classiccms/helpers.rb', line 27

def link(document, url)
  url = url.gsub(/[^a-zA-Z0-9\-\/]+/, '-').downcase
  slugs = Slug.where document_id: document.id
  if slugs.count > 0
    slug = slugs.first
  else
    slug = Slug.new
    slug.document_id = document.id
    slug.generate_id
    slug.save
  end
  "/#{slug.id}/#{url}"
end

#logoutObject

insert the logout button



17
18
19
20
21
# File 'lib/classiccms/helpers.rb', line 17

def logout
  if @user != nil and @user.admin?(@routes)
    show :logout, views: File.join(Classiccms::ROOT, 'views/cms')
  end
end

#partial(file_name) ⇒ Object



22
23
24
# File 'lib/classiccms/helpers.rb', line 22

def partial(file_name)
  show :"#{file_name}", {views: ['app/views']}
end

#pingObject

for the test suite. Lets remove this laterrr!



5
6
7
# File 'lib/classiccms/helpers.rb', line 5

def ping
  'pong'
end

#section(section_name, parent_id = nil) ⇒ Object

renders all child pages of the given position (id)



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/classiccms/helpers.rb', line 62

def section(section_name, parent_id = nil)
  html = ""
  parent_id = get_parent_id(parent_id)
  objects_to_render = {}
  Base.where(:'connections.parent_id' => parent_id, :'connections.section' => section_name, :'connections.file'.ne => nil).each do |record|
    connection = record.connections.where(:parent_id => parent_id, :section => section_name, :file.ne => nil).first

    if objects_to_render[connection.order_id] == nil
      number = connection.order_id
    else
      number = get_unique_number(objects_to_render, connection.order_id)
    end
    objects_to_render[number] = {:record => record, :connection => connection}
  end
  Hash[objects_to_render.sort].each do |k, object|
    html += show object[:connection].file, {views: ["app/views/#{object[:record]._type}", "app/views/#{object[:record]._type.downcase}"]}, {record: object[:record]}
  end
  return html
end

#sort(object) ⇒ Object

simple function that return id if user is allowed to sort the item



112
113
114
115
116
117
118
119
# File 'lib/classiccms/helpers.rb', line 112

def sort(object)
  if object.kind_of? Moped::BSON::ObjectId or object.kind_of? String
    object = Base.where(:_id => object).first
  end
  if @user != nil and @user.admin?(@routes)
    object.id.to_s
  end
end