gollum_rails 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/gollum_rails.rb CHANGED
@@ -6,9 +6,6 @@ require 'rubygems'
6
6
  # external
7
7
  require 'gollum-lib'
8
8
 
9
- # patches
10
- require 'grit/git-ruby/internal/pack'
11
- require 'core_ext/string'
12
9
 
13
10
  # GollumRails is a RubyGem for extending Rails and the Gollum wiki powered by github
14
11
  # It has the ability to combine the benefits from a git powered wiki with Rails.
@@ -23,7 +20,7 @@ module GollumRails
23
20
  autoload :Setup, 'gollum_rails/setup'
24
21
 
25
22
  # GollumRails version string
26
- VERSION = '1.0.4'
23
+ VERSION = '1.0.5'
27
24
 
28
25
  # Simplified error
29
26
  class Error < StandardError; end
@@ -31,13 +28,9 @@ module GollumRails
31
28
  # All Gollum internal exceptions will be redirected to this
32
29
  class GollumInternalError < Error
33
30
 
34
- # The classes name, that raised the exception
35
- attr_accessor :name
36
31
 
37
- # The messsage, brought by the class
32
+ attr_accessor :name
38
33
  attr_accessor :message
39
-
40
- # The target, the class wanted to interact with
41
34
  attr_accessor :target
42
35
 
43
36
  # modifies content for throwing an exception
@@ -20,7 +20,11 @@ module GollumRails
20
20
  attr_writer :page_class
21
21
 
22
22
  # Sets the wiki class used by all instances
23
- attr_writer :wiki_class
23
+ attr_writer :wiki_path
24
+
25
+ # Sets the wiki options
26
+ attr_writer :wiki_options
27
+
24
28
 
25
29
  # Sets the applications status
26
30
  attr_writer :enabled
@@ -38,13 +42,18 @@ module GollumRails
38
42
  def page_class
39
43
  @page_class || Page
40
44
  end
45
+
46
+ # Gets the wiki options
47
+ def wiki_options
48
+ @wiki_options || {}
49
+ end
41
50
 
42
51
  # Gets the Globally used Page class or use a new one if not defined
43
52
  #
44
53
  #
45
54
  # Returns the internal page class or a fresh ::Gollum::Page
46
- def wiki_class
47
- @wiki_class || Wiki
55
+ def wiki_path
56
+ @wiki_path || ""
48
57
  end
49
58
 
50
59
  end
@@ -18,63 +18,99 @@ module GollumRails
18
18
 
19
19
  Connector.page_class = self
20
20
 
21
- # Gets / Sets current page
22
- attr_accessor :page
23
-
24
- # Gets / Sets the wiki
25
- attr_accessor :wiki
26
-
27
- # Initializer
28
- def initialize
29
- @wiki = Connector.wiki_class
21
+ class << self
22
+
23
+ # == Parses a given filepath e.g. '/test/page'
24
+ # Name is page
25
+ # path is /test
26
+ #
27
+ # Returns a Hash
28
+ def parse_path(name)
29
+ path = '/'
30
+ name = name.to_s
31
+ if name.include?('/')
32
+ name = name[1..-1] if name[0] == "/"
33
+ content = name.split('/')
34
+ name = content.pop
35
+ path = '/'+content.join('/')
36
+ end
37
+ { path: path, name: name }
38
+ end
39
+
40
+ # finds all versions of a page
41
+ #
42
+ # name - the pagename to search
43
+ # wiki - instance of Gollum::Wiki
44
+ # version - optional - The pages version
45
+ #
46
+ # Returns the Gollum::Page class
47
+ def find_page(name, wiki, version=nil)
48
+ wiki.clear_cache
49
+ path_data = parse_path(name)
50
+ wiki.paged(path_data[:name], path_data[:path], exact = true, version)
51
+ end
30
52
  end
31
53
 
32
-
33
- # creates a new Page
54
+ # == Creates a new page
34
55
  #
35
- # name - String
36
- # type - Symbol
37
- # content - Text
38
- # commit - Hash or instance of Committer
56
+ # name - The name of the page
57
+ # content - The content of the page
58
+ # wiki - An instance of Gollum::Wiki
59
+ # type - A filetype as symbol (optional)
60
+ # commit - Commit Hash
39
61
  #
40
- # Returns the commit id
41
- def new_page( name, content,type=:markdown, commit={} )
42
- @wiki.write_page name.to_s, type, content, commit if name
43
- @page = @wiki.page name
44
- @page
62
+ # Returns the page
63
+ def new_page( name, content, wiki, type=:markdown, commit={} )
64
+ path_data = self.class.parse_path(name)
65
+ wiki.write_page( path_data[:name], type, content, commit, path_data[:path].gsub!(/^\//, "").gsub!(/(\/)+$/,'') || "" )
66
+ wiki.clear_cache
67
+ self.class.find_page( name, wiki )
45
68
  end
46
69
 
47
- # updates an existing page
70
+ # == Updates an existing page
71
+ #
72
+ # page - An instance of Gollum::Page
73
+ # wiki - An instance of Gollum::Wiki
74
+ # content - New content
75
+ # commit - Commit Hash
76
+ # name - A new String (optional)
77
+ # format - A filetype as symbol (optional)
78
+ #
79
+ # Returns the page
80
+ def update_page( page, wiki, content=nil, commit={}, name=nil, format=nil)
81
+ wiki.clear_cache
82
+ return page if page.nil?
83
+ name ||= page.name
84
+ format = (format || page.format).to_sym
85
+ content ||= page.raw_data
86
+
87
+ wiki.update_page(page,name,format,content.to_s,commit) unless ((!content||page.raw_data == content) && page.format == format)
88
+
89
+ self.class.find_page( mixin(page.url_path, name), wiki )
90
+ end
91
+
92
+ # == Preview page
48
93
  #
49
- # new - Hash with changed data
50
- # commit - Hash or instance of Committer
51
- # old - also an instance of self
94
+ # wiki - An instance of Gollum::Wiki
95
+ # content - New content
96
+ # name - A String
97
+ # format - A filetype as symbol (optional)
52
98
  #
53
- # Returns the commit id
54
- def update_page( new, commit={}, old=nil)
55
- if new.is_a?(Hash)
56
- commit_id = @wiki.update_page (old||@page),
57
- new[:name]||@page.name,
58
- new[:format]||@page.format,
59
- new[:content]||@page.raw_data,
60
- commit
61
- else
62
- raise Error.new "commit must be a Hash. #{new.class} given", :crit
63
- end
64
-
65
- # this is very ugly. Shouldn't gollum return the new page?
66
- @page = @page.find(new[:name]||@page.name, commit_id)
67
- @page
99
+ def preview_page(wiki, name, content, format=:markdown)
100
+ page = wiki.preview_page(name,content,format)
101
+ page.formatted_data
68
102
  end
69
103
 
70
- # deletes an existing page
104
+ # == Deletes an existing page
71
105
  #
72
- # page - instance of self
73
- # commit - Hash or instance of Committer
106
+ # page - Gollum::Page
107
+ # wiki - Gollum::Wiki
108
+ # commit - Commit Hash
74
109
  #
75
110
  # Returns the commit id
76
- def delete_page( commit={}, page = nil )
77
- @wiki.delete_page (page||@page), commit
111
+ def delete_page( page,wiki,commit={} )
112
+ wiki.clear_cache
113
+ wiki.delete_page(page, commit)
78
114
  end
79
115
 
80
116
  # renames an existing page
@@ -94,7 +130,8 @@ module GollumRails
94
130
  #
95
131
  # Returns the Gollum::Page class
96
132
  def find_page(name)
97
- @wiki.page ::Gollum::Page.cname(name)
133
+
134
+ self.class.find_page(name)
98
135
  end
99
136
 
100
137
  # moves an existing page
@@ -170,6 +207,16 @@ module GollumRails
170
207
  raise Error.new "page cannot be empty for #{__method__}", :high
171
208
  end
172
209
  end
210
+
211
+ private
212
+
213
+ # replaces old filename with new
214
+ def mixin(old_url, new_name)
215
+ url = old_url.split("/")
216
+ url.pop
217
+ url << new_name
218
+ url.join("/")
219
+ end
173
220
 
174
221
  end
175
222
  end
@@ -6,27 +6,28 @@ module GollumRails
6
6
  # TODO: doc
7
7
  class Wiki
8
8
 
9
- # Gets / Sets the git path or object
10
- attr_accessor :git
11
-
12
9
  # Initializes the class
13
10
  #
14
11
  # location - String or Grit::Repo
15
- def initialize(location)
16
- @git = location
17
- if location.is_a? ::String
18
- con = ::Gollum::Wiki.new @git
12
+ def initialize(location, options={})
13
+
14
+ Connector.wiki_options = options
15
+
16
+ if location.is_a?(::String)
17
+ con = location
19
18
  else
20
- con= ::Gollum::Wiki.new @git.path
19
+ con = location.path
21
20
  end
22
- Connector.wiki_class = con
21
+
22
+ Connector.wiki_path = con
23
+
23
24
  end
24
25
 
25
26
  # Static call from within any other class
26
27
  #
27
28
  # Returns a new instance of this class
28
- def self.wiki(location)
29
- Wiki.new(location)
29
+ def self.wiki(location, options={})
30
+ Wiki.new(location, options)
30
31
  end
31
32
 
32
33
  # Forwards unknown methods to Gollum::Wiki
@@ -12,14 +12,12 @@ module GollumRails
12
12
  # * new
13
13
  # * save
14
14
  # * delete
15
- # * find_or_initialize_by_naname
15
+ # * find_or_initialize_by_name
16
16
  #
17
17
  class Page
18
- extend ::ActiveModel::Callbacks
19
- include ::ActiveModel::Validations
20
- include ::ActiveModel::Conversion
21
- extend ::ActiveModel::Naming
22
-
18
+ extend ActiveModel::Naming
19
+ include ActiveModel::Conversion
20
+ include ActiveModel::Validations
23
21
 
24
22
  # Callback for save
25
23
  define_model_callbacks :save
@@ -29,25 +27,32 @@ module GollumRails
29
27
 
30
28
  # Callback for delete
31
29
  define_model_callbacks :delete
32
-
30
+
31
+ # Callback for initialize
32
+ define_model_callbacks :initialize
33
+
34
+ # Callback for create
35
+ define_model_callbacks :create
36
+
37
+ # Callback for commit
38
+ define_model_callbacks :commit
39
+
33
40
  # static
34
41
  class << self
35
- # Gets / Sets the gollum page
36
- #
37
- attr_accessor :gollum_page
38
-
39
- # Sets the validator
40
- attr_writer :validator
41
-
42
+
42
43
  # Finds an existing page or creates it
43
44
  #
44
45
  # name - The name
45
46
  # commit - Hash
46
47
  #
47
48
  # Returns self
48
- def find_or_initialize_by_name(name, commit)
49
- result_for_find = self.find(name)
50
- self.create({:format => :markdown, :name => name, :content => ".", :commit => commit })
49
+ def find_or_initialize_by_name(name, commit={})
50
+ result_for_find = find(name)
51
+ unless result_for_find.nil?
52
+ result_for_find
53
+ else
54
+ new(:format => :markdown, :name => name, :content => ".", :commit => commit)
55
+ end
51
56
  end
52
57
 
53
58
  # Checks if the fileformat is supported
@@ -56,67 +61,88 @@ module GollumRails
56
61
  #
57
62
  # Returns true or false
58
63
  def format_supported?(format)
59
- supported = ['ascii', 'github-markdown','markdown', 'creole', 'org', 'pod', 'rdoc']
60
- format.in?(supported)
64
+ Gollum::Markup.formats.include?(format.to_sym)
61
65
  end
62
66
 
63
67
  # first creates an instance of itself and executes the save function.
64
68
  #
65
- # hash - Hash containing the page data
69
+ # data - Hash containing the page data
66
70
  #
67
71
  #
68
72
  # Returns an instance of Gollum::Page or false
69
- def create(hash)
70
- page = Page.new hash
73
+ def create(data)
74
+ page = Page.new(data)
71
75
  page.save
72
76
  end
73
77
 
74
78
 
75
79
  # calls `create` on current class. If returned value is nil an exception will be thrown
76
80
  #
77
- # hash - Hash containing the page data
81
+ # data - Hash of Data containing all necessary stuff
82
+ # TODO write this stuff
78
83
  #
79
84
  #
80
85
  # Returns an instance of Gollum::Page
81
- def create!(hash)
82
- action = self.create(hash)
83
- action
86
+ def create!(data)
87
+ page = Page.new(data)
88
+ page.save!
84
89
  end
85
90
 
86
91
  # Finds a page based on the name and specified version
87
92
  #
88
93
  # name - the name of the page
94
+ # version - optional - The pages version
89
95
  #
90
96
  # Return an instance of Gollum::Page
91
- def find(name)
92
- page = GollumRails::Adapters::Gollum::Page.new
93
- page.find_page name
97
+ def find(name, version=nil)
98
+ page = Adapters::Gollum::Page.find_page(name, wiki, version)
99
+
100
+ return new( :gollum_page => page ) unless page.nil?
101
+ return nil
102
+
94
103
  end
95
104
 
96
105
  # Gets all pages in the wiki
97
106
  def all
98
- self.wiki.pages
107
+ wiki.pages
99
108
  end
109
+
100
110
  alias_method :find_all, :all
101
111
 
102
112
  # Gets the wiki instance
103
113
  def wiki
104
- @wiki || Adapters::Gollum::Connector.wiki_class
114
+ @wiki ||= ::Gollum::Wiki.new(Adapters::Gollum::Connector.wiki_path, Adapters::Gollum::Connector.wiki_options)
115
+ end
116
+
117
+ # TODO: implement more of this (format, etc)
118
+ #
119
+ def method_missing(name, *args)
120
+ if name =~ /^find_by_(name)$/
121
+ self.find(args.first)
122
+ else
123
+ raise NoMethodError, "Method #{name} was not found"
124
+ end
105
125
  end
106
126
 
107
127
  end
108
128
 
109
-
129
+ # Gets / Sets the gollum page
130
+ #
131
+ attr_accessor :gollum_page
132
+
110
133
  # Initializes a new Page
111
134
  #
112
135
  # attrs - Hash of attributes
113
136
  #
114
137
  # commit must be given to perform any page action!
115
138
  def initialize(attrs = {})
116
- if Adapters::Gollum::Connector.enabled
117
- attrs.each{|k,v| self.public_send("#{k}=",v)} if attrs
118
- else
119
- raise GollumInternalError, 'gollum_rails is not enabled!'
139
+ run_callbacks :initialize do
140
+ if Adapters::Gollum::Connector.enabled
141
+ attrs.each{|k,v| self.public_send("#{k}=",v)} if attrs
142
+ update_attrs if attrs[:gollum_page]
143
+ else
144
+ raise GollumInternalError, 'gollum_rails is not enabled!'
145
+ end
120
146
  end
121
147
  end
122
148
 
@@ -126,10 +152,10 @@ module GollumRails
126
152
 
127
153
 
128
154
  # Gets / Sets the pages name
129
- attr_accessor :name
155
+ attr_writer :name
130
156
 
131
157
  # Gets / Sets the contents content
132
- attr_accessor :content
158
+ attr_writer :content
133
159
 
134
160
  # Gets / Sets the commit Hash
135
161
  attr_accessor :commit
@@ -145,14 +171,24 @@ module GollumRails
145
171
 
146
172
  # Gets the pages format
147
173
  def format
148
- @format.to_sym
174
+ (@format || @gollum_page.format).to_sym
149
175
  end
150
176
 
177
+ def name
178
+ @name ||= @gollum_page.name
179
+ end
180
+
181
+ def content
182
+ @content ||= @gollum_page.content
183
+ end
151
184
 
152
185
  # Gets the page class
153
186
  def page
154
- @page ||= Adapters::Gollum::Connector.page_class.new
187
+ Adapters::Gollum::Page.new
155
188
  end
189
+
190
+ # Gollum Page
191
+ attr_accessor :gollum_page
156
192
 
157
193
  #############
158
194
  # activemodel
@@ -181,58 +217,77 @@ module GollumRails
181
217
  # Returns an instance of Gollum::Page or false
182
218
  def save
183
219
  run_callbacks :save do
184
- return false unless valid?
220
+ return nil unless valid?
185
221
  begin
186
- page.new_page(name,content,format,commit)
222
+ @gollum_page = page.new_page(name,content,wiki,format,commit)
187
223
  rescue ::Gollum::DuplicatePageError => e
188
- page.page = page.find_page(name)
224
+ @gollum_page = Adapters::Gollum::Page.find_page(name, wiki)
189
225
  end
190
- return page.page
226
+ return self
191
227
  end
192
228
  end
193
229
 
194
- # aliasing save
230
+ # == Save without exception handling
231
+ # raises errors everytime something is wrong
195
232
  #
196
- # TODO:
197
- # * implement full method!
198
- alias_method :save!, :save
233
+ def save!
234
+ run_callbacks :save do
235
+ raise StandardError, "record is not valid" unless valid?
236
+ raise StandardError, "commit could not be empty" if commit == {}
237
+ @gollum_page = page.new_page(name,content,wiki,format,commit)
238
+ return self
239
+ end
240
+ end
199
241
 
200
- # Updates an existing page (or created)
242
+ # == Updates an existing page (or created)
201
243
  #
202
- # hash - Hash containing the attributes, you want to update
244
+ # content - optional. If given the content of the gollum_page will be updated
245
+ # name - optional. If given the name of gollum_page will be updated
246
+ # format - optional. Updates the format. Uses markdown as default
203
247
  # commit - optional. If given this commit will be used instead of that one, used
204
248
  # to initialize the instance
205
249
  #
206
250
  #
207
- # Returns an instance of Gollum::Page
208
- def update_attributes(hash, commit=nil)
251
+ # Returns an instance of GollumRails::Page
252
+ def update_attributes(content=nil,name=nil,format=:markdown, commit=nil)
209
253
  run_callbacks :update do
210
- page.update_page hash, get_right_commit(commit)
254
+ @gollum_page = page.update_page(gollum_page, wiki, content, get_right_commit(commit), name, format)
211
255
  end
212
256
  end
213
-
214
- # Deletes current page (also available static. See below)
257
+
258
+ # == Deletes current page (also available static. See below)
215
259
  #
216
260
  # commit - optional. If given this commit will be used instead of that one, used
217
261
  # to initialize the instance
218
262
  #
219
263
  # Returns the commit id of the current action as String
220
- def delete(commit=nil)
264
+ def destroy(commit=nil)
221
265
  run_callbacks :delete do
222
- page.delete_page get_right_commit(commit)
266
+ page.delete_page(gollum_page, wiki, get_right_commit(commit))
223
267
  end
224
268
  end
225
269
 
270
+ # == Deletes current page (also available static. See below)
271
+ #
272
+ # commit - optional. If given this commit will be used instead of that one, used
273
+ # to initialize the instance
274
+ #
275
+ # Returns the commit id of the current action as String
276
+ def delete(commit=nil)
277
+ destroy(commit)
278
+ end
279
+
226
280
  # checks if entry already has been saved
227
281
  #
228
282
  #
229
283
  def persisted?
230
- return true if page.page.instance_of?(::Gollum::Page)
284
+ return true if gollum_page
231
285
  return false
232
286
  end
233
- # Previews the page - Mostly used if you want to see what you do before saving
287
+
288
+ # == Previews the page - Mostly used if you want to see what you do before saving
234
289
  #
235
- # This is an extremely performant method!
290
+ # This is an extremely fast method!
236
291
  # 1 rendering attempt take depending on the content about 0.001 (simple markdown)
237
292
  # upto 0.004 (1000 chars markdown) seconds, which is quite good
238
293
  #
@@ -242,16 +297,91 @@ module GollumRails
242
297
  #
243
298
  # Returns a String
244
299
  def preview(format=:markdown)
245
- preview = self.class.wiki.preview_page @name, @content, format
246
- preview.formatted_data
300
+ page.preview_page( wiki, name, content, format )
247
301
  end
248
302
 
249
-
303
+ # == Gets the url for current page from Gollum::Page
304
+ #
305
+ # Returns a String
306
+ def url
307
+ gollum_page.url_path
308
+ end
309
+
310
+ # == Gets the title for current Gollum::Page
311
+ #
312
+ # Returns a String
313
+ def title
314
+ gollum_page.title
315
+ end
316
+
317
+ # == Gets formatted_data for current Gollum::Page
318
+ #
319
+ # Returns a String
320
+ def html_data
321
+ gollum_page.formatted_data
322
+ end
323
+
324
+
325
+ # == Gets raw_data for current Gollum::Page
326
+ #
327
+ # Returns a String
328
+ def raw_data
329
+ gollum_page.raw_data
330
+ end
331
+
332
+ # == Gets the history of current gollum_page
333
+ #
334
+ # Returns an Array
335
+ def history
336
+ return nil unless persisted?
337
+ gollum_page.versions
338
+ end
339
+
340
+ # == Gets the last modified by Gollum::Committer
341
+ #
342
+ # Returns a String
343
+ def last_changed_by
344
+ "%s <%s>" % [history.last.author.name, history.last.author.email]
345
+ end
346
+
347
+ # == Compare 2 Commits.
348
+ #
349
+ # sha1 - SHA1
350
+ # sha2 - SHA1
351
+ def compare_commits(sha1,sha2=nil)
352
+ Page.wiki.full_reverse_diff_for(@gollum_page,sha1,sha2)
353
+ end
354
+
355
+ # == The pages filename, based on the name and the format
356
+ #
357
+ # Returns a String
358
+ def filename
359
+ Page.wiki.page_file_name(@name, @format)
360
+ end
361
+
362
+ # == Checks if current page is a subpage
363
+ def sub_page?
364
+ return nil unless persisted?
365
+ @gollum_page.sub_page
366
+ end
367
+
368
+ # == Gets the version of current commit
369
+ #
370
+ def current_version(long=false)
371
+ return nil unless persisted?
372
+ unless long
373
+ @gollum_page.version_short
374
+ else
375
+ @gollum_page.version.to_s
376
+ end
377
+
378
+ end
379
+
250
380
  #######
251
381
  private
252
382
  #######
253
383
 
254
- # Get the right commit out of 2 commits
384
+ # == Gets the right commit out of 2 commits
255
385
  #
256
386
  # commit_local - local commit Hash
257
387
  #
@@ -261,6 +391,19 @@ module GollumRails
261
391
  com = commit_local if !commit_local.nil?
262
392
  return com
263
393
  end
264
-
394
+
395
+ # == Updates local attributes from gollum_page class
396
+ #
397
+ def update_attrs
398
+ @name = gollum_page.name
399
+ @content= gollum_page.raw_data
400
+ @format = gollum_page.format
401
+ end
402
+
403
+ # == To static
404
+ def wiki
405
+ self.class.wiki
406
+ end
407
+
265
408
  end
266
409
  end