gollum_rails 1.4.6 → 1.4.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -2
- data/Gemfile.lock +6 -4
- data/Guardfile +1 -0
- data/gollum_rails.gemspec +16 -12
- data/lib/gollum_rails.rb +30 -9
- data/lib/gollum_rails/adapters/gollum/page.rb +0 -134
- data/lib/gollum_rails/adapters/gollum/wiki.rb +0 -7
- data/lib/gollum_rails/callbacks.rb +47 -0
- data/lib/gollum_rails/core.rb +158 -0
- data/lib/gollum_rails/finders.rb +62 -0
- data/lib/gollum_rails/orm.rb +11 -0
- data/lib/gollum_rails/page.rb +80 -399
- data/lib/gollum_rails/persistance.rb +165 -0
- data/lib/gollum_rails/store.rb +60 -0
- data/lib/gollum_rails/validation.rb +9 -0
- data/spec/gollum_rails/orm_spec.rb +25 -0
- data/spec/gollum_rails/page_spec.rb +62 -33
- metadata +70 -14
- data/lib/gollum_rails/adapters/activemodel.rb +0 -36
- data/lib/gollum_rails/adapters/activemodel/boolean.rb +0 -15
- data/lib/gollum_rails/adapters/activemodel/error.rb +0 -27
- data/lib/gollum_rails/adapters/activemodel/naming.rb +0 -42
- data/spec/gollum_rails/adapters/activemodel/error_spec.rb +0 -11
- data/spec/gollum_rails/adapters/activemodel/naming_spec.rb +0 -27
- data/spec/gollum_rails/adapters/activemodel/validation_unused.rb +0 -102
- data/spec/gollum_rails/adapters/gollum/error_spec.rb +0 -7
@@ -0,0 +1,62 @@
|
|
1
|
+
module GollumRails
|
2
|
+
module Finders
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
module ClassMethods
|
5
|
+
# Finds an existing page or creates it
|
6
|
+
#
|
7
|
+
# name - The name
|
8
|
+
# commit - Hash
|
9
|
+
#
|
10
|
+
# Returns self
|
11
|
+
def find_or_initialize_by_name(name, commit={})
|
12
|
+
result_for_find = find(name)
|
13
|
+
unless result_for_find.nil?
|
14
|
+
result_for_find
|
15
|
+
else
|
16
|
+
new(:format => :markdown, :name => name, :content => ".", :commit => commit)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Finds a page based on the name and specified version
|
21
|
+
#
|
22
|
+
# name - the name of the page
|
23
|
+
# version - optional - The pages version
|
24
|
+
#
|
25
|
+
# Return an instance of Gollum::Page
|
26
|
+
def find(name, version=nil)
|
27
|
+
page = Adapters::Gollum::Page.find_page(name, wiki, version)
|
28
|
+
|
29
|
+
return new( :gollum_page => page ) unless page.nil?
|
30
|
+
return nil
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
# == Searches the wiki for files CONTENT!
|
35
|
+
#
|
36
|
+
# string - Searchstring
|
37
|
+
#
|
38
|
+
# Returns an Array
|
39
|
+
def search(string)
|
40
|
+
wiki.search(string)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Gets all pages in the wiki
|
44
|
+
def all
|
45
|
+
wiki.pages
|
46
|
+
end
|
47
|
+
|
48
|
+
alias_method :find_all, :all
|
49
|
+
|
50
|
+
# TODO: implement more of this (format, etc)
|
51
|
+
#
|
52
|
+
def method_missing(name, *args)
|
53
|
+
if name =~ /^find_by_(name)$/
|
54
|
+
self.find(args.first)
|
55
|
+
else super
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/gollum_rails/page.rb
CHANGED
@@ -1,408 +1,89 @@
|
|
1
|
-
require 'thread'
|
2
|
-
|
3
1
|
module GollumRails
|
4
|
-
|
5
|
-
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
2
|
+
|
3
|
+
|
4
|
+
# = GollumRails
|
5
|
+
#
|
6
|
+
# Gollum Rails was initially designed to integrate the Gollum wiki software
|
7
|
+
# into your Rails application without any mounts or extra applications to run.
|
8
|
+
#
|
9
|
+
# Now you can use your completely own layout without struggling with gollum forks.
|
10
|
+
#
|
11
|
+
# Just integrate this gem into your Gemfile and you are good to go:
|
12
|
+
#
|
13
|
+
# gem install gollum_rails
|
14
|
+
#
|
15
|
+
# or in your Gemfile:
|
16
|
+
#
|
17
|
+
# gem 'gollum_rails'
|
18
|
+
#
|
19
|
+
#
|
20
|
+
# If you want to use this gem with Rails version prior 4.0.0 please check out the rails3
|
21
|
+
# branch on Github:
|
22
|
+
#
|
23
|
+
# https://github.com/nirnanaaa/gollum_rails/tree/rails3
|
24
|
+
#
|
25
|
+
#
|
26
|
+
# == Initialization
|
27
|
+
#
|
28
|
+
# To make full use of GollumRails you need to enable it in an initializer by genating it:
|
29
|
+
#
|
30
|
+
# rails g gollum_rails:install
|
31
|
+
#
|
32
|
+
# Now you can add the path to your Repository in there.
|
33
|
+
#
|
34
|
+
# Also you need to set the <tt>startup</tt> boolean to <tt>true</tt>.
|
35
|
+
#
|
36
|
+
#
|
37
|
+
#
|
38
|
+
# == Page model
|
39
|
+
#
|
40
|
+
# You also need a model à la ActiveRecord by calling:
|
41
|
+
#
|
42
|
+
# rails g gollum_rails:model Page
|
43
|
+
#
|
44
|
+
#
|
45
|
+
# == Creation of a Page
|
46
|
+
#
|
47
|
+
# <b>Each action in a Git repository needs a Commit, which identifies the author!</b>
|
48
|
+
#
|
49
|
+
# commit = { name: 'nirnanaa', email: 'mosny@zyg.li', message: 'created page page'}
|
50
|
+
# Page.create(name: 'test_page', content: 'content', format: :markdown, commit: commit)
|
51
|
+
#
|
52
|
+
# Page.create!(name: 'test_page', content: 'content', format: :markdown, commit: commit)
|
53
|
+
#
|
54
|
+
# The <tt>!</tt> version of the method throws an error on failure.
|
55
|
+
#
|
56
|
+
#
|
57
|
+
# page = Page.new(name: 'test_page', content: 'content', format: :markdown, commit: commit)
|
58
|
+
# page.save
|
59
|
+
#
|
60
|
+
# # OR
|
61
|
+
#
|
62
|
+
# page.save!
|
63
|
+
#
|
64
|
+
#
|
65
|
+
#
|
16
66
|
#
|
17
67
|
class Page
|
18
|
-
include ::ActiveModel::Model
|
19
|
-
|
20
|
-
|
21
|
-
# Callback for save
|
22
|
-
define_model_callbacks :save
|
23
|
-
|
24
|
-
# Callback for update
|
25
|
-
define_model_callbacks :update
|
26
|
-
|
27
|
-
# Callback for delete
|
28
|
-
define_model_callbacks :delete
|
29
|
-
|
30
|
-
# Callback for initialize
|
31
|
-
define_model_callbacks :initialize
|
32
|
-
|
33
|
-
# Callback for create
|
34
|
-
define_model_callbacks :create
|
35
|
-
|
36
|
-
# Callback for commit
|
37
|
-
define_model_callbacks :commit
|
38
|
-
|
39
|
-
# static
|
40
|
-
class << self
|
41
|
-
|
42
|
-
# Finds an existing page or creates it
|
43
|
-
#
|
44
|
-
# name - The name
|
45
|
-
# commit - Hash
|
46
|
-
#
|
47
|
-
# Returns self
|
48
|
-
def find_or_initialize_by_name(name, commit={})
|
49
|
-
result_for_find = find(name)
|
50
|
-
unless result_for_find.nil?
|
51
|
-
result_for_find
|
52
|
-
else
|
53
|
-
new(:format => :markdown, :name => name, :content => ".", :commit => commit)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
# Checks if the fileformat is supported
|
58
|
-
#
|
59
|
-
# format - String
|
60
|
-
#
|
61
|
-
# Returns true or false
|
62
|
-
def format_supported?(format)
|
63
|
-
Gollum::Markup.formats.include?(format.to_sym)
|
64
|
-
end
|
65
|
-
|
66
|
-
# first creates an instance of itself and executes the save function.
|
67
|
-
#
|
68
|
-
# data - Hash containing the page data
|
69
|
-
#
|
70
|
-
#
|
71
|
-
# Returns an instance of Gollum::Page or false
|
72
|
-
def create(data)
|
73
|
-
page = Page.new(data)
|
74
|
-
page.save
|
75
|
-
end
|
76
|
-
|
77
|
-
|
78
|
-
# calls `create` on current class. If returned value is nil an exception will be thrown
|
79
|
-
#
|
80
|
-
# data - Hash of Data containing all necessary stuff
|
81
|
-
# TODO write this stuff
|
82
|
-
#
|
83
|
-
#
|
84
|
-
# Returns an instance of Gollum::Page
|
85
|
-
def create!(data)
|
86
|
-
page = Page.new(data)
|
87
|
-
page.save!
|
88
|
-
end
|
89
|
-
|
90
|
-
# Finds a page based on the name and specified version
|
91
|
-
#
|
92
|
-
# name - the name of the page
|
93
|
-
# version - optional - The pages version
|
94
|
-
#
|
95
|
-
# Return an instance of Gollum::Page
|
96
|
-
def find(name, version=nil)
|
97
|
-
page = Adapters::Gollum::Page.find_page(name, wiki, version)
|
98
|
-
|
99
|
-
return new( :gollum_page => page ) unless page.nil?
|
100
|
-
return nil
|
101
|
-
|
102
|
-
end
|
103
|
-
|
104
|
-
# Gets all pages in the wiki
|
105
|
-
def all
|
106
|
-
wiki.pages
|
107
|
-
end
|
108
|
-
|
109
|
-
alias_method :find_all, :all
|
110
|
-
|
111
|
-
# Gets the wiki instance
|
112
|
-
def wiki
|
113
|
-
@wiki ||= ::Gollum::Wiki.new(Adapters::Gollum::Connector.wiki_path, Adapters::Gollum::Connector.wiki_options)
|
114
|
-
end
|
115
|
-
|
116
|
-
# TODO: implement more of this (format, etc)
|
117
|
-
#
|
118
|
-
def method_missing(name, *args)
|
119
|
-
if name =~ /^find_by_(name)$/
|
120
|
-
self.find(args.first)
|
121
|
-
else
|
122
|
-
raise NoMethodError, "Method #{name} was not found"
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
end
|
127
|
-
|
128
|
-
# Gets / Sets the gollum page
|
129
|
-
#
|
130
|
-
attr_accessor :gollum_page
|
131
|
-
|
132
|
-
# Initializes a new Page
|
133
|
-
#
|
134
|
-
# attrs - Hash of attributes
|
135
|
-
#
|
136
|
-
# commit must be given to perform any page action!
|
137
|
-
def initialize(attrs = {})
|
138
|
-
run_callbacks :initialize do
|
139
|
-
if Adapters::Gollum::Connector.enabled
|
140
|
-
attrs.each{|k,v| self.public_send("#{k}=",v)} if attrs
|
141
|
-
update_attrs if attrs[:gollum_page]
|
142
|
-
else
|
143
|
-
raise GollumInternalError, 'gollum_rails is not enabled!'
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
#########
|
149
|
-
# Setters
|
150
|
-
#########
|
151
|
-
|
152
|
-
|
153
|
-
# Gets / Sets the pages name
|
154
|
-
attr_writer :name
|
155
|
-
|
156
|
-
# Gets / Sets the contents content
|
157
|
-
attr_writer :content
|
158
|
-
|
159
|
-
# Gets / Sets the commit Hash
|
160
|
-
attr_accessor :commit
|
161
|
-
|
162
|
-
# Sets the format
|
163
|
-
attr_writer :format
|
164
|
-
|
165
|
-
|
166
|
-
#########
|
167
|
-
# Getters
|
168
|
-
#########
|
169
|
-
|
170
|
-
|
171
|
-
# Gets the pages format
|
172
|
-
def format
|
173
|
-
(@format || @gollum_page.format).to_sym
|
174
|
-
end
|
175
|
-
|
176
|
-
def name
|
177
|
-
@name ||= @gollum_page.name
|
178
|
-
end
|
179
|
-
|
180
|
-
def content
|
181
|
-
@content ||= @gollum_page.content
|
182
|
-
end
|
183
|
-
|
184
|
-
# Gets the page class
|
185
|
-
def page
|
186
|
-
Adapters::Gollum::Page.new
|
187
|
-
end
|
188
|
-
|
189
|
-
# Gollum Page
|
190
|
-
attr_accessor :gollum_page
|
191
|
-
|
192
|
-
#############
|
193
|
-
# activemodel
|
194
|
-
#############
|
195
|
-
|
196
|
-
# Handles the connection betweet plain activemodel and Gollum
|
197
|
-
# Saves current page in GIT wiki
|
198
|
-
# If another page with the same name is existing, gollum_rails
|
199
|
-
# will detect it and returns that page instead.
|
200
|
-
#
|
201
|
-
# Examples:
|
202
|
-
#
|
203
|
-
# obj = GollumRails::Page.new <params>
|
204
|
-
# @article = obj.save
|
205
|
-
# # => Gollum::Page
|
206
|
-
#
|
207
|
-
# @article.name
|
208
|
-
# whatever name you have entered OR the name of the previous
|
209
|
-
# created page
|
210
|
-
#
|
211
|
-
#
|
212
|
-
# TODO:
|
213
|
-
# * overriding for creation(duplicates)
|
214
|
-
# * do not alias save! on save
|
215
|
-
#
|
216
|
-
# Returns an instance of Gollum::Page or false
|
217
|
-
def save
|
218
|
-
run_callbacks :save do
|
219
|
-
return nil unless valid?
|
220
|
-
begin
|
221
|
-
@gollum_page = page.new_page(name,content,wiki,format,commit)
|
222
|
-
rescue ::Gollum::DuplicatePageError => e
|
223
|
-
@gollum_page = Adapters::Gollum::Page.find_page(name, wiki)
|
224
|
-
end
|
225
|
-
return self
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
# == Save without exception handling
|
230
|
-
# raises errors everytime something is wrong
|
231
|
-
#
|
232
|
-
def save!
|
233
|
-
run_callbacks :save do
|
234
|
-
raise StandardError, "record is not valid" unless valid?
|
235
|
-
raise StandardError, "commit could not be empty" if commit == {}
|
236
|
-
@gollum_page = page.new_page(name,content,wiki,format,commit)
|
237
|
-
return self
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
# == Updates an existing page (or created)
|
242
|
-
#
|
243
|
-
# content - optional. If given the content of the gollum_page will be updated
|
244
|
-
# name - optional. If given the name of gollum_page will be updated
|
245
|
-
# format - optional. Updates the format. Uses markdown as default
|
246
|
-
# commit - optional. If given this commit will be used instead of that one, used
|
247
|
-
# to initialize the instance
|
248
|
-
#
|
249
|
-
#
|
250
|
-
# Returns an instance of GollumRails::Page
|
251
|
-
def update_attributes(content=nil,name=nil,format=:markdown, commit=nil)
|
252
|
-
run_callbacks :update do
|
253
|
-
@gollum_page = page.update_page(gollum_page, wiki, content, get_right_commit(commit), name, format)
|
254
|
-
end
|
255
|
-
end
|
256
68
|
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
run_callbacks :delete do
|
265
|
-
page.delete_page(gollum_page, wiki, get_right_commit(commit))
|
266
|
-
end
|
267
|
-
end
|
268
|
-
|
269
|
-
# == Deletes current page (also available static. See below)
|
270
|
-
#
|
271
|
-
# commit - optional. If given this commit will be used instead of that one, used
|
272
|
-
# to initialize the instance
|
273
|
-
#
|
274
|
-
# Returns the commit id of the current action as String
|
275
|
-
def delete(commit=nil)
|
276
|
-
destroy(commit)
|
277
|
-
end
|
278
|
-
|
279
|
-
# checks if entry already has been saved
|
280
|
-
#
|
281
|
-
#
|
282
|
-
def persisted?
|
283
|
-
return true if gollum_page
|
284
|
-
return false
|
69
|
+
if ActiveModel::VERSION::MAJOR == 4
|
70
|
+
include ActiveModel::Model
|
71
|
+
else
|
72
|
+
extend ActiveModel::Naming
|
73
|
+
extend ActiveModel::Callbacks
|
74
|
+
include ActiveModel::Conversion
|
75
|
+
include ActiveModel::Validations
|
285
76
|
end
|
286
77
|
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
#
|
294
|
-
# format - Specify the format you want to render with see {self.format_supported?}
|
295
|
-
# for formats
|
296
|
-
#
|
297
|
-
# Returns a String
|
298
|
-
def preview(format=:markdown)
|
299
|
-
page.preview_page( wiki, name, content, format )
|
300
|
-
end
|
78
|
+
include Core
|
79
|
+
include Store
|
80
|
+
include Validation
|
81
|
+
include Persistance
|
82
|
+
include Finders
|
83
|
+
include Callbacks
|
301
84
|
|
302
|
-
# == Gets the url for current page from Gollum::Page
|
303
|
-
#
|
304
|
-
# Returns a String
|
305
|
-
def url
|
306
|
-
gollum_page.url_path
|
307
|
-
end
|
308
|
-
|
309
|
-
# == Gets the title for current Gollum::Page
|
310
|
-
#
|
311
|
-
# Returns a String
|
312
|
-
def title
|
313
|
-
gollum_page.title
|
314
|
-
end
|
315
|
-
|
316
|
-
# == Gets formatted_data for current Gollum::Page
|
317
|
-
#
|
318
|
-
# Returns a String
|
319
|
-
def html_data
|
320
|
-
gollum_page.formatted_data
|
321
|
-
end
|
322
|
-
|
323
|
-
|
324
|
-
# == Gets raw_data for current Gollum::Page
|
325
|
-
#
|
326
|
-
# Returns a String
|
327
|
-
def raw_data
|
328
|
-
gollum_page.raw_data
|
329
|
-
end
|
330
|
-
|
331
|
-
# == Gets the history of current gollum_page
|
332
|
-
#
|
333
|
-
# Returns an Array
|
334
|
-
def history
|
335
|
-
return nil unless persisted?
|
336
|
-
gollum_page.versions
|
337
|
-
end
|
338
|
-
|
339
|
-
# == Gets the last modified by Gollum::Committer
|
340
|
-
#
|
341
|
-
# Returns a String
|
342
|
-
def last_changed_by
|
343
|
-
"%s <%s>" % [history.last.author.name, history.last.author.email]
|
344
|
-
end
|
345
|
-
|
346
|
-
# == Compare 2 Commits.
|
347
|
-
#
|
348
|
-
# sha1 - SHA1
|
349
|
-
# sha2 - SHA1
|
350
|
-
def compare_commits(sha1,sha2=nil)
|
351
|
-
Page.wiki.full_reverse_diff_for(@gollum_page,sha1,sha2)
|
352
|
-
end
|
353
|
-
|
354
|
-
# == The pages filename, based on the name and the format
|
355
|
-
#
|
356
|
-
# Returns a String
|
357
|
-
def filename
|
358
|
-
Page.wiki.page_file_name(@name, @format)
|
359
|
-
end
|
360
|
-
|
361
|
-
# == Checks if current page is a subpage
|
362
|
-
def sub_page?
|
363
|
-
return nil unless persisted?
|
364
|
-
@gollum_page.sub_page
|
365
|
-
end
|
366
|
-
|
367
|
-
# == Gets the version of current commit
|
368
|
-
#
|
369
|
-
def current_version(long=false)
|
370
|
-
return nil unless persisted?
|
371
|
-
unless long
|
372
|
-
@gollum_page.version_short
|
373
|
-
else
|
374
|
-
@gollum_page.version.to_s
|
375
|
-
end
|
376
|
-
|
377
|
-
end
|
378
|
-
|
379
|
-
#######
|
380
|
-
private
|
381
|
-
#######
|
382
|
-
|
383
|
-
# == Gets the right commit out of 2 commits
|
384
|
-
#
|
385
|
-
# commit_local - local commit Hash
|
386
|
-
#
|
387
|
-
# Returns local_commit > class_commit
|
388
|
-
def get_right_commit(commit_local)
|
389
|
-
com = commit if commit_local.nil?
|
390
|
-
com = commit_local if !commit_local.nil?
|
391
|
-
return com
|
392
|
-
end
|
393
|
-
|
394
|
-
# == Updates local attributes from gollum_page class
|
395
|
-
#
|
396
|
-
def update_attrs
|
397
|
-
@name = gollum_page.name
|
398
|
-
@content= gollum_page.raw_data
|
399
|
-
@format = gollum_page.format
|
400
|
-
end
|
401
|
-
|
402
|
-
# == To static
|
403
|
-
def wiki
|
404
|
-
self.class.wiki
|
405
|
-
end
|
406
|
-
|
407
85
|
end
|
86
|
+
|
87
|
+
ActiveSupport.run_load_hooks(:gollum, Page)
|
88
|
+
|
408
89
|
end
|