Class: Web

Inherits:
Object
  • Object
show all
Defined in:
app/models/web.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_wiki, name, address, password = nil) ⇒ Web

Returns a new instance of Web.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/web.rb', line 15

def initialize(parent_wiki, name, address, password = nil)
  self.address = address
  @wiki, @name, @password = parent_wiki, name, password

  set_compatible_defaults

  @pages = {}
  @allow_uploads = true
  @additional_style = nil
  @published = false
  @count_pages = false
  @allow_uploads = true
end

Instance Attribute Details

#additional_styleObject

Returns the value of attribute additional_style.



9
10
11
# File 'app/models/web.rb', line 9

def additional_style
  @additional_style
end

#addressObject

Returns the value of attribute address.



10
11
12
# File 'app/models/web.rb', line 10

def address
  @address
end

#allow_uploadsObject

Returns the value of attribute allow_uploads.



9
10
11
# File 'app/models/web.rb', line 9

def allow_uploads
  @allow_uploads
end

#brackets_onlyObject

All below getters know their default values. This is necessary to ensure compatibility with 0.9 storages, where they were not defined.



40
# File 'app/models/web.rb', line 40

def brackets_only() @brackets_only || false end

#colorObject



41
# File 'app/models/web.rb', line 41

def color() @color ||= '008B26' end

#count_pagesObject



42
# File 'app/models/web.rb', line 42

def count_pages()   @count_pages || false end

#markupObject



43
# File 'app/models/web.rb', line 43

def markup() @markup ||= :textile end

#max_upload_sizeObject



44
# File 'app/models/web.rb', line 44

def max_upload_size() @max_upload_size || 100; end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'app/models/web.rb', line 8

def name
  @name
end

#pagesObject

Returns the value of attribute pages.



8
9
10
# File 'app/models/web.rb', line 8

def pages
  @pages
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'app/models/web.rb', line 8

def password
  @password
end

#publishedObject

Returns the value of attribute published.



9
10
11
# File 'app/models/web.rb', line 9

def published
  @published
end

#safe_modeObject

Returns the value of attribute safe_mode.



8
9
10
# File 'app/models/web.rb', line 8

def safe_mode
  @safe_mode
end

Instance Method Details

#add_page(name, content, created_at, author) ⇒ Object



47
48
49
50
51
# File 'app/models/web.rb', line 47

def add_page(name, content, created_at, author)
  page = Page.new(self, name)
  page.revise(content, created_at, author)
  @pages[page.name] = page
end

#authorsObject



60
61
62
# File 'app/models/web.rb', line 60

def authors
  select.authors 
end

#categoriesObject



64
65
66
# File 'app/models/web.rb', line 64

def categories
  select.map { |page| page.categories }.flatten.uniq.sort
end

#has_file?(name) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'app/models/web.rb', line 72

def has_file?(name)
  wiki.file_yard(self).has_file?(name)
end

#has_page?(name) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/models/web.rb', line 68

def has_page?(name)
  pages[name]
end


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/web.rb', line 76

def make_file_link(mode, name, text, base_url)
  link = CGI.escape(name)
  case mode
  when :export
    if has_file?(name) then "<a class=\"existingWikiWord\" href=\"#{link}.html\">#{text}</a>"
    else "<span class=\"newWikiWord\">#{text}</span>" end
  when :publish
    if has_file?(name) then "<a class=\"existingWikiWord\" href=\"#{base_url}/published/#{link}\">#{text}</a>"
    else "<span class=\"newWikiWord\">#{text}</span>" end
  else 
    if has_file?(name)
      "<a class=\"existingWikiWord\" href=\"#{base_url}/file/#{link}\">#{text}</a>"
    else 
      "<span class=\"newWikiWord\">#{text}<a href=\"#{base_url}/file/#{link}\">?</a></span>"
    end
  end
end

Create a link for the given page name and link text based on the render mode in options and whether the page exists in the this web. The links a relative, and will work only if displayed on another WikiPage. It should not be used in menus, templates and such - instead, use link_to_page helper



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/web.rb', line 99

def make_link(name, text = nil, options = {})
  text = CGI.escapeHTML(text || WikiWords.separate(name))
  mode = options[:mode] || :show
  base_url = options[:base_url] || '..'
  link_type = options[:link_type] || :show
  case link_type.to_sym
  when :show
    make_page_link(mode, name, text, base_url)
  when :file
    make_file_link(mode, name, text, base_url)
  when :pic
    make_pic_link(mode, name, text, base_url)
  else
    raise "Unknown link type: #{link_type}"
  end
end


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/web.rb', line 116

def make_page_link(mode, name, text, base_url)
  link = CGI.escape(name)
  case mode.to_sym
  when :export
    if has_page?(name) then %{<a class="existingWikiWord" href="#{link}.html">#{text}</a>}
    else %{<span class="newWikiWord">#{text}</span>} end
  when :publish
    if has_page?(name) then %{<a class="existingWikiWord" href="#{base_url}/published/#{link}">#{text}</a>}
    else %{<span class="newWikiWord">#{text}</span>} end
  else 
    if has_page?(name)
      %{<a class="existingWikiWord" href="#{base_url}/show/#{link}">#{text}</a>}
    else 
      %{<span class="newWikiWord">#{text}<a href="#{base_url}/show/#{link}">?</a></span>}
    end
  end
end


134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/web.rb', line 134

def make_pic_link(mode, name, text, base_url)
  link = CGI.escape(name)
  case mode.to_sym
  when :export
    if has_file?(name) then %{<img alt="#{text}" src="#{link}" />}
    else %{<img alt="#{text}" src="no image" />} end
  when :publish
    if has_file?(name) then %{<img alt="#{text}" src="#{link}" />}
    else %{<span class="newWikiWord">#{text}</span>} end
  else 
    if has_file?(name) then %{<img alt="#{text}" src="#{base_url}/pic/#{link}" />}
    else %{<span class="newWikiWord">#{text}<a href="#{base_url}/pic/#{link}">?</a></span>} end
  end
end

#refresh_pages_with_references(page_name) ⇒ Object

Clears the display cache for all the pages with references to



150
151
152
153
154
# File 'app/models/web.rb', line 150

def refresh_pages_with_references(page_name)
  select.pages_that_reference(page_name).each { |page| 
    page.revisions.each { |revision| revision.clear_display_cache }
  }
end

#refresh_revisionsObject



156
157
158
# File 'app/models/web.rb', line 156

def refresh_revisions
  select.each { |page| page.revisions.each { |revision| revision.clear_display_cache } }
end

#remove_pages(pages_to_be_removed) ⇒ Object



160
161
162
# File 'app/models/web.rb', line 160

def remove_pages(pages_to_be_removed)
  pages.delete_if { |page_name, page| pages_to_be_removed.include?(page) }
end

#revised_onObject



164
165
166
# File 'app/models/web.rb', line 164

def revised_on
  select.most_recent_revision
end

#select(&condition) ⇒ Object



168
169
170
# File 'app/models/web.rb', line 168

def select(&condition)
  PageSet.new(self, @pages.values, condition)
end

#set_compatible_defaultsObject

Explicitly sets value of some web attributes to defaults, unless they are already set



30
31
32
33
34
35
36
37
# File 'app/models/web.rb', line 30

def set_compatible_defaults
  @markup = markup()
  @color = color()
  @safe_mode = safe_mode()
  @brackets_only = brackets_only()
  @max_upload_size = max_upload_size()
  @wiki = wiki
end

#wikiObject



45
# File 'app/models/web.rb', line 45

def wiki() @wiki ||= WikiService.instance; end