gollum_rails 1.4.0.rc2 → 1.4.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +6 -6
  3. data/Gemfile.lock +7 -10
  4. data/HISTORY.md +91 -91
  5. data/LICENSE +661 -661
  6. data/README.md +74 -74
  7. data/Rakefile +170 -170
  8. data/gollum_rails.gemspec +6 -4
  9. data/lib/core_ext/string.rb +3 -7
  10. data/lib/generators/gollum_rails/install/install_generator.rb +27 -27
  11. data/lib/generators/gollum_rails/install/templates/gollum_initializer.rb +22 -22
  12. data/lib/generators/gollum_rails/language/language_generator.rb +81 -81
  13. data/lib/generators/gollum_rails/model/model_generator.rb +51 -51
  14. data/lib/generators/gollum_rails/model/templates/model_template.erb +13 -13
  15. data/lib/gollum_rails.rb +26 -6
  16. data/lib/gollum_rails/adapters/activemodel.rb +36 -36
  17. data/lib/gollum_rails/adapters/activemodel/boolean.rb +15 -15
  18. data/lib/gollum_rails/adapters/activemodel/error.rb +27 -27
  19. data/lib/gollum_rails/adapters/activemodel/naming.rb +42 -42
  20. data/lib/gollum_rails/adapters/gollum.rb +54 -54
  21. data/lib/gollum_rails/adapters/gollum/.gitkeep +0 -0
  22. data/lib/gollum_rails/adapters/gollum/error.rb +19 -19
  23. data/lib/gollum_rails/adapters/gollum/page.rb +177 -177
  24. data/lib/gollum_rails/adapters/gollum/wiki.rb +42 -42
  25. data/lib/gollum_rails/page.rb +266 -266
  26. data/lib/gollum_rails/setup.rb +81 -81
  27. data/lib/grit/git-ruby/internal/pack.rb +397 -397
  28. data/spec/gollum_rails/adapters/activemodel/error_spec.rb +11 -11
  29. data/spec/gollum_rails/adapters/activemodel/naming_spec.rb +27 -27
  30. data/spec/gollum_rails/adapters/activemodel/validation_unused.rb +102 -102
  31. data/spec/gollum_rails/adapters/gollum/committer_spec.rb +0 -0
  32. data/spec/gollum_rails/adapters/gollum/connector_spec.rb +15 -15
  33. data/spec/gollum_rails/adapters/gollum/error_spec.rb +7 -7
  34. data/spec/gollum_rails/adapters/gollum/page_spec.rb +89 -89
  35. data/spec/gollum_rails/adapters/gollum/wiki_spec.rb +27 -27
  36. data/spec/gollum_rails/error_spec.rb +10 -0
  37. data/spec/gollum_rails/page_spec.rb +207 -207
  38. data/spec/gollum_rails/respository_spec.rb +0 -0
  39. data/spec/gollum_rails/setup_spec.rb +44 -44
  40. data/spec/gollum_rails/wiki_spec.rb +0 -0
  41. data/spec/gollum_rails_spec.rb +9 -0
  42. data/spec/spec.opts +3 -3
  43. data/spec/spec_helper.rb +43 -43
  44. metadata +4 -5
  45. data/lib/gollum_rails/modules/hash.rb +0 -33
  46. data/lib/gollum_rails/modules/loader.rb +0 -5
  47. data/spec/gollum_rails/modules/hash_spec.rb +0 -31
@@ -1,177 +1,177 @@
1
- module GollumRails
2
- module Adapters
3
- module Gollum
4
-
5
- # Main page class for the Gollum connector.
6
- #
7
- # It provides some awesome features for connecting gollum to gollum_rails such as:
8
- # * new_page
9
- # * find_page
10
- # * delete_page
11
- # * rename_page
12
- # * move_page
13
- # * first_page_commit
14
- # * page_creation time
15
- # * ...
16
- #
17
- class Page
18
-
19
- Connector.page_class = self
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
30
- end
31
-
32
-
33
- # creates a new Page
34
- #
35
- # name - String
36
- # type - Symbol
37
- # content - Text
38
- # commit - Hash or instance of Committer
39
- #
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
45
- end
46
-
47
- # updates an existing page
48
- #
49
- # new - Hash with changed data
50
- # commit - Hash or instance of Committer
51
- # old - also an instance of self
52
- #
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
68
- end
69
-
70
- # deletes an existing page
71
- #
72
- # page - instance of self
73
- # commit - Hash or instance of Committer
74
- #
75
- # Returns the commit id
76
- def delete_page( commit={}, page = nil )
77
- @wiki.delete_page (page||@page), commit
78
- end
79
-
80
- # renames an existing page
81
- #
82
- # page - instance of myself
83
- # newname - new pagename
84
- # commit - Hash or instance of Committer
85
- #
86
- # Returns the commit id
87
- def rename_page( page, newname, commit={} )
88
-
89
- end
90
-
91
- # finds all versions of a page
92
- #
93
- # name - the pagename to search
94
- #
95
- # Returns the Gollum::Page class
96
- def find_page(name)
97
- @wiki.page ::Gollum::Page.cname(name)
98
- end
99
-
100
- # moves an existing page
101
- #
102
- # TODO:
103
- # * implement
104
- def move_page()
105
- end
106
-
107
- # gets page last edit date
108
- #
109
- # Returns an instance of Time
110
- def page_last_edited_date
111
- if @page
112
- return @page.versions.first.authored_date
113
- else
114
- raise Error.new "page cannot be empty for #{__method__}", :high
115
- end
116
- end
117
-
118
- # gets the latest commit
119
- #
120
- # Returns an instance of Grit::Commit
121
- def page_last_commit
122
- if @page
123
- return @page.versions.first
124
- else
125
- raise Error.new "page cannot be empty for #{__method__}", :high
126
- end
127
- end
128
-
129
- # gets the creation date of the page
130
- #
131
- # Returns an instance of Time
132
- def page_created
133
- if @page
134
- return @page.versions.last.authored_date
135
- else
136
- raise Error.new "page cannot be empty for #{__method__}", :high
137
- end
138
-
139
- end
140
-
141
- # gets the first page commit
142
- #
143
- # Returns an instance of Grit::Commit
144
- def page_first_commit
145
- if @page
146
- return @page.versions.last
147
- else
148
- raise Error.new "page cannot be empty for #{__method__}", :high
149
- end
150
- end
151
-
152
- # gets a specific commit version
153
- #
154
- # Returns an instance of Grit::Commit
155
- def page_commit(id)
156
- if @page
157
- return @page.versions.each{|v| return v if v.id == id}
158
- else
159
- raise Error.new "page cannot be empty for #{__method__}", :high
160
- end
161
- end
162
-
163
- # gets a specific commit time
164
- #
165
- # Returns an instance of Time
166
- def page_commit_date(id)
167
- if @page
168
- return @page.versions.each{|v| return v.authored_date if v.id == id}
169
- else
170
- raise Error.new "page cannot be empty for #{__method__}", :high
171
- end
172
- end
173
-
174
- end
175
- end
176
- end
177
- end
1
+ module GollumRails
2
+ module Adapters
3
+ module Gollum
4
+
5
+ # Main page class for the Gollum connector.
6
+ #
7
+ # It provides some awesome features for connecting gollum to gollum_rails such as:
8
+ # * new_page
9
+ # * find_page
10
+ # * delete_page
11
+ # * rename_page
12
+ # * move_page
13
+ # * first_page_commit
14
+ # * page_creation time
15
+ # * ...
16
+ #
17
+ class Page
18
+
19
+ Connector.page_class = self
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
30
+ end
31
+
32
+
33
+ # creates a new Page
34
+ #
35
+ # name - String
36
+ # type - Symbol
37
+ # content - Text
38
+ # commit - Hash or instance of Committer
39
+ #
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
45
+ end
46
+
47
+ # updates an existing page
48
+ #
49
+ # new - Hash with changed data
50
+ # commit - Hash or instance of Committer
51
+ # old - also an instance of self
52
+ #
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
68
+ end
69
+
70
+ # deletes an existing page
71
+ #
72
+ # page - instance of self
73
+ # commit - Hash or instance of Committer
74
+ #
75
+ # Returns the commit id
76
+ def delete_page( commit={}, page = nil )
77
+ @wiki.delete_page (page||@page), commit
78
+ end
79
+
80
+ # renames an existing page
81
+ #
82
+ # page - instance of myself
83
+ # newname - new pagename
84
+ # commit - Hash or instance of Committer
85
+ #
86
+ # Returns the commit id
87
+ def rename_page( page, newname, commit={} )
88
+
89
+ end
90
+
91
+ # finds all versions of a page
92
+ #
93
+ # name - the pagename to search
94
+ #
95
+ # Returns the Gollum::Page class
96
+ def find_page(name)
97
+ @wiki.page ::Gollum::Page.cname(name)
98
+ end
99
+
100
+ # moves an existing page
101
+ #
102
+ # TODO:
103
+ # * implement
104
+ def move_page()
105
+ end
106
+
107
+ # gets page last edit date
108
+ #
109
+ # Returns an instance of Time
110
+ def page_last_edited_date
111
+ if @page
112
+ return @page.versions.first.authored_date
113
+ else
114
+ raise Error.new "page cannot be empty for #{__method__}", :high
115
+ end
116
+ end
117
+
118
+ # gets the latest commit
119
+ #
120
+ # Returns an instance of Grit::Commit
121
+ def page_last_commit
122
+ if @page
123
+ return @page.versions.first
124
+ else
125
+ raise Error.new "page cannot be empty for #{__method__}", :high
126
+ end
127
+ end
128
+
129
+ # gets the creation date of the page
130
+ #
131
+ # Returns an instance of Time
132
+ def page_created
133
+ if @page
134
+ return @page.versions.last.authored_date
135
+ else
136
+ raise Error.new "page cannot be empty for #{__method__}", :high
137
+ end
138
+
139
+ end
140
+
141
+ # gets the first page commit
142
+ #
143
+ # Returns an instance of Grit::Commit
144
+ def page_first_commit
145
+ if @page
146
+ return @page.versions.last
147
+ else
148
+ raise Error.new "page cannot be empty for #{__method__}", :high
149
+ end
150
+ end
151
+
152
+ # gets a specific commit version
153
+ #
154
+ # Returns an instance of Grit::Commit
155
+ def page_commit(id)
156
+ if @page
157
+ return @page.versions.each{|v| return v if v.id == id}
158
+ else
159
+ raise Error.new "page cannot be empty for #{__method__}", :high
160
+ end
161
+ end
162
+
163
+ # gets a specific commit time
164
+ #
165
+ # Returns an instance of Time
166
+ def page_commit_date(id)
167
+ if @page
168
+ return @page.versions.each{|v| return v.authored_date if v.id == id}
169
+ else
170
+ raise Error.new "page cannot be empty for #{__method__}", :high
171
+ end
172
+ end
173
+
174
+ end
175
+ end
176
+ end
177
+ end
@@ -1,42 +1,42 @@
1
- require 'grit'
2
- module GollumRails
3
- module Adapters
4
- module Gollum
5
-
6
- # TODO: doc
7
- class Wiki
8
-
9
- # Gets / Sets the git path or object
10
- attr_accessor :git
11
-
12
- # Initializes the class
13
- #
14
- # location - String or Grit::Repo
15
- def initialize(location)
16
- @git = location
17
- if location.is_a? ::String
18
- con = ::Gollum::Wiki.new @git
19
- else
20
- con= ::Gollum::Wiki.new @git.path
21
- end
22
- Connector.wiki_class = con
23
- end
24
-
25
- # Static call from within any other class
26
- #
27
- # Returns a new instance of this class
28
- def self.wiki(location)
29
- Wiki.new(location)
30
- end
31
-
32
- # Forwards unknown methods to Gollum::Wiki
33
- #
34
- # may throw an Argument error or method missing
35
- def self.method_missing(name, *args)
36
- Connector.wiki_class.send(name, *args)
37
- end
38
-
39
- end
40
- end
41
- end
42
- end
1
+ require 'grit'
2
+ module GollumRails
3
+ module Adapters
4
+ module Gollum
5
+
6
+ # TODO: doc
7
+ class Wiki
8
+
9
+ # Gets / Sets the git path or object
10
+ attr_accessor :git
11
+
12
+ # Initializes the class
13
+ #
14
+ # location - String or Grit::Repo
15
+ def initialize(location)
16
+ @git = location
17
+ if location.is_a? ::String
18
+ con = ::Gollum::Wiki.new @git
19
+ else
20
+ con= ::Gollum::Wiki.new @git.path
21
+ end
22
+ Connector.wiki_class = con
23
+ end
24
+
25
+ # Static call from within any other class
26
+ #
27
+ # Returns a new instance of this class
28
+ def self.wiki(location)
29
+ Wiki.new(location)
30
+ end
31
+
32
+ # Forwards unknown methods to Gollum::Wiki
33
+ #
34
+ # may throw an Argument error or method missing
35
+ def self.method_missing(name, *args)
36
+ Connector.wiki_class.send(name, *args)
37
+ end
38
+
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,266 +1,266 @@
1
- require 'thread'
2
-
3
- module GollumRails
4
-
5
- # Main class, used to interact with rails.
6
- #
7
- # Methods, which are available:
8
- # * find
9
- # * update_attributes
10
- # * find_by_*
11
- # * create
12
- # * new
13
- # * save
14
- # * delete
15
- # * find_or_initialize_by_naname
16
- #
17
- class Page
18
- extend ::ActiveModel::Callbacks
19
- include ::ActiveModel::Validations
20
- include ::ActiveModel::Conversion
21
- extend ::ActiveModel::Naming
22
-
23
-
24
- # Callback for save
25
- define_model_callbacks :save
26
-
27
- # Callback for update
28
- define_model_callbacks :update
29
-
30
- # Callback for delete
31
- define_model_callbacks :delete
32
-
33
- # static
34
- 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
- # 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 = self.find(name)
50
- self.create({:format => :markdown, :name => name, :content => ".", :commit => commit })
51
- end
52
-
53
- # Checks if the fileformat is supported
54
- #
55
- # format - String
56
- #
57
- # Returns true or false
58
- def format_supported?(format)
59
- supported = ['ascii', 'github-markdown','markdown', 'creole', 'org', 'pod', 'rdoc']
60
- format.in?(supported)
61
- end
62
-
63
- # first creates an instance of itself and executes the save function.
64
- #
65
- # hash - Hash containing the page data
66
- #
67
- #
68
- # Returns an instance of Gollum::Page or false
69
- def create(hash)
70
- page = Page.new hash
71
- page.save
72
- end
73
-
74
-
75
- # calls `create` on current class. If returned value is nil an exception will be thrown
76
- #
77
- # hash - Hash containing the page data
78
- #
79
- #
80
- # Returns an instance of Gollum::Page
81
- def create!(hash)
82
- action = self.create(hash)
83
- action
84
- end
85
-
86
- # Finds a page based on the name and specified version
87
- #
88
- # name - the name of the page
89
- #
90
- # Return an instance of Gollum::Page
91
- def find(name)
92
- page = GollumRails::Adapters::Gollum::Page.new
93
- page.find_page name
94
- end
95
-
96
- # Gets all pages in the wiki
97
- def all
98
- self.wiki.pages
99
- end
100
- alias_method :find_all, :all
101
-
102
- # Gets the wiki instance
103
- def wiki
104
- @wiki || Adapters::Gollum::Connector.wiki_class
105
- end
106
-
107
- end
108
-
109
-
110
- # Initializes a new Page
111
- #
112
- # attrs - Hash of attributes
113
- #
114
- # commit must be given to perform any page action!
115
- 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!'
120
- end
121
- end
122
-
123
- #########
124
- # Setters
125
- #########
126
-
127
-
128
- # Gets / Sets the pages name
129
- attr_accessor :name
130
-
131
- # Gets / Sets the contents content
132
- attr_accessor :content
133
-
134
- # Gets / Sets the commit Hash
135
- attr_accessor :commit
136
-
137
- # Sets the format
138
- attr_writer :format
139
-
140
-
141
- #########
142
- # Getters
143
- #########
144
-
145
-
146
- # Gets the pages format
147
- def format
148
- @format.to_sym
149
- end
150
-
151
-
152
- # Gets the page class
153
- def page
154
- @page ||= Adapters::Gollum::Connector.page_class.new
155
- end
156
-
157
- #############
158
- # activemodel
159
- #############
160
-
161
- # Handles the connection betweet plain activemodel and Gollum
162
- # Saves current page in GIT wiki
163
- # If another page with the same name is existing, gollum_rails
164
- # will detect it and returns that page instead.
165
- #
166
- # Examples:
167
- #
168
- # obj = GollumRails::Page.new <params>
169
- # @article = obj.save
170
- # # => Gollum::Page
171
- #
172
- # @article.name
173
- # whatever name you have entered OR the name of the previous
174
- # created page
175
- #
176
- #
177
- # TODO:
178
- # * overriding for creation(duplicates)
179
- # * do not alias save! on save
180
- #
181
- # Returns an instance of Gollum::Page or false
182
- def save
183
- run_callbacks :save do
184
- return false unless valid?
185
- begin
186
- page.new_page(name,content,format,commit)
187
- rescue ::Gollum::DuplicatePageError => e
188
- page.page = page.find_page(name)
189
- end
190
- return page.page
191
- end
192
- end
193
-
194
- # aliasing save
195
- #
196
- # TODO:
197
- # * implement full method!
198
- alias_method :save!, :save
199
-
200
- # Updates an existing page (or created)
201
- #
202
- # hash - Hash containing the attributes, you want to update
203
- # commit - optional. If given this commit will be used instead of that one, used
204
- # to initialize the instance
205
- #
206
- #
207
- # Returns an instance of Gollum::Page
208
- def update_attributes(hash, commit=nil)
209
- run_callbacks :update do
210
- page.update_page hash, get_right_commit(commit)
211
- end
212
- end
213
-
214
- # Deletes current page (also available static. See below)
215
- #
216
- # commit - optional. If given this commit will be used instead of that one, used
217
- # to initialize the instance
218
- #
219
- # Returns the commit id of the current action as String
220
- def delete(commit=nil)
221
- run_callbacks :delete do
222
- page.delete_page get_right_commit(commit)
223
- end
224
- end
225
-
226
- # checks if entry already has been saved
227
- #
228
- #
229
- def persisted?
230
- return true if page.page.instance_of?(::Gollum::Page)
231
- return false
232
- end
233
- # Previews the page - Mostly used if you want to see what you do before saving
234
- #
235
- # This is an extremely performant method!
236
- # 1 rendering attempt take depending on the content about 0.001 (simple markdown)
237
- # upto 0.004 (1000 chars markdown) seconds, which is quite good
238
- #
239
- #
240
- # format - Specify the format you want to render with see {self.format_supported?}
241
- # for formats
242
- #
243
- # Returns a String
244
- def preview(format=:markdown)
245
- preview = self.class.wiki.preview_page @name, @content, format
246
- preview.formatted_data
247
- end
248
-
249
-
250
- #######
251
- private
252
- #######
253
-
254
- # Get the right commit out of 2 commits
255
- #
256
- # commit_local - local commit Hash
257
- #
258
- # Returns local_commit > class_commit
259
- def get_right_commit(commit_local)
260
- com = commit if commit_local.nil?
261
- com = commit_local if !commit_local.nil?
262
- return com
263
- end
264
-
265
- end
266
- end
1
+ require 'thread'
2
+
3
+ module GollumRails
4
+
5
+ # Main class, used to interact with rails.
6
+ #
7
+ # Methods, which are available:
8
+ # * find
9
+ # * update_attributes
10
+ # * find_by_*
11
+ # * create
12
+ # * new
13
+ # * save
14
+ # * delete
15
+ # * find_or_initialize_by_naname
16
+ #
17
+ class Page
18
+ extend ::ActiveModel::Callbacks
19
+ include ::ActiveModel::Validations
20
+ include ::ActiveModel::Conversion
21
+ extend ::ActiveModel::Naming
22
+
23
+
24
+ # Callback for save
25
+ define_model_callbacks :save
26
+
27
+ # Callback for update
28
+ define_model_callbacks :update
29
+
30
+ # Callback for delete
31
+ define_model_callbacks :delete
32
+
33
+ # static
34
+ 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
+ # 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 = self.find(name)
50
+ self.create({:format => :markdown, :name => name, :content => ".", :commit => commit })
51
+ end
52
+
53
+ # Checks if the fileformat is supported
54
+ #
55
+ # format - String
56
+ #
57
+ # Returns true or false
58
+ def format_supported?(format)
59
+ supported = ['ascii', 'github-markdown','markdown', 'creole', 'org', 'pod', 'rdoc']
60
+ format.in?(supported)
61
+ end
62
+
63
+ # first creates an instance of itself and executes the save function.
64
+ #
65
+ # hash - Hash containing the page data
66
+ #
67
+ #
68
+ # Returns an instance of Gollum::Page or false
69
+ def create(hash)
70
+ page = Page.new hash
71
+ page.save
72
+ end
73
+
74
+
75
+ # calls `create` on current class. If returned value is nil an exception will be thrown
76
+ #
77
+ # hash - Hash containing the page data
78
+ #
79
+ #
80
+ # Returns an instance of Gollum::Page
81
+ def create!(hash)
82
+ action = self.create(hash)
83
+ action
84
+ end
85
+
86
+ # Finds a page based on the name and specified version
87
+ #
88
+ # name - the name of the page
89
+ #
90
+ # Return an instance of Gollum::Page
91
+ def find(name)
92
+ page = GollumRails::Adapters::Gollum::Page.new
93
+ page.find_page name
94
+ end
95
+
96
+ # Gets all pages in the wiki
97
+ def all
98
+ self.wiki.pages
99
+ end
100
+ alias_method :find_all, :all
101
+
102
+ # Gets the wiki instance
103
+ def wiki
104
+ @wiki || Adapters::Gollum::Connector.wiki_class
105
+ end
106
+
107
+ end
108
+
109
+
110
+ # Initializes a new Page
111
+ #
112
+ # attrs - Hash of attributes
113
+ #
114
+ # commit must be given to perform any page action!
115
+ 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!'
120
+ end
121
+ end
122
+
123
+ #########
124
+ # Setters
125
+ #########
126
+
127
+
128
+ # Gets / Sets the pages name
129
+ attr_accessor :name
130
+
131
+ # Gets / Sets the contents content
132
+ attr_accessor :content
133
+
134
+ # Gets / Sets the commit Hash
135
+ attr_accessor :commit
136
+
137
+ # Sets the format
138
+ attr_writer :format
139
+
140
+
141
+ #########
142
+ # Getters
143
+ #########
144
+
145
+
146
+ # Gets the pages format
147
+ def format
148
+ @format.to_sym
149
+ end
150
+
151
+
152
+ # Gets the page class
153
+ def page
154
+ @page ||= Adapters::Gollum::Connector.page_class.new
155
+ end
156
+
157
+ #############
158
+ # activemodel
159
+ #############
160
+
161
+ # Handles the connection betweet plain activemodel and Gollum
162
+ # Saves current page in GIT wiki
163
+ # If another page with the same name is existing, gollum_rails
164
+ # will detect it and returns that page instead.
165
+ #
166
+ # Examples:
167
+ #
168
+ # obj = GollumRails::Page.new <params>
169
+ # @article = obj.save
170
+ # # => Gollum::Page
171
+ #
172
+ # @article.name
173
+ # whatever name you have entered OR the name of the previous
174
+ # created page
175
+ #
176
+ #
177
+ # TODO:
178
+ # * overriding for creation(duplicates)
179
+ # * do not alias save! on save
180
+ #
181
+ # Returns an instance of Gollum::Page or false
182
+ def save
183
+ run_callbacks :save do
184
+ return false unless valid?
185
+ begin
186
+ page.new_page(name,content,format,commit)
187
+ rescue ::Gollum::DuplicatePageError => e
188
+ page.page = page.find_page(name)
189
+ end
190
+ return page.page
191
+ end
192
+ end
193
+
194
+ # aliasing save
195
+ #
196
+ # TODO:
197
+ # * implement full method!
198
+ alias_method :save!, :save
199
+
200
+ # Updates an existing page (or created)
201
+ #
202
+ # hash - Hash containing the attributes, you want to update
203
+ # commit - optional. If given this commit will be used instead of that one, used
204
+ # to initialize the instance
205
+ #
206
+ #
207
+ # Returns an instance of Gollum::Page
208
+ def update_attributes(hash, commit=nil)
209
+ run_callbacks :update do
210
+ page.update_page hash, get_right_commit(commit)
211
+ end
212
+ end
213
+
214
+ # Deletes current page (also available static. See below)
215
+ #
216
+ # commit - optional. If given this commit will be used instead of that one, used
217
+ # to initialize the instance
218
+ #
219
+ # Returns the commit id of the current action as String
220
+ def delete(commit=nil)
221
+ run_callbacks :delete do
222
+ page.delete_page get_right_commit(commit)
223
+ end
224
+ end
225
+
226
+ # checks if entry already has been saved
227
+ #
228
+ #
229
+ def persisted?
230
+ return true if page.page.instance_of?(::Gollum::Page)
231
+ return false
232
+ end
233
+ # Previews the page - Mostly used if you want to see what you do before saving
234
+ #
235
+ # This is an extremely performant method!
236
+ # 1 rendering attempt take depending on the content about 0.001 (simple markdown)
237
+ # upto 0.004 (1000 chars markdown) seconds, which is quite good
238
+ #
239
+ #
240
+ # format - Specify the format you want to render with see {self.format_supported?}
241
+ # for formats
242
+ #
243
+ # Returns a String
244
+ def preview(format=:markdown)
245
+ preview = self.class.wiki.preview_page @name, @content, format
246
+ preview.formatted_data
247
+ end
248
+
249
+
250
+ #######
251
+ private
252
+ #######
253
+
254
+ # Get the right commit out of 2 commits
255
+ #
256
+ # commit_local - local commit Hash
257
+ #
258
+ # Returns local_commit > class_commit
259
+ def get_right_commit(commit_local)
260
+ com = commit if commit_local.nil?
261
+ com = commit_local if !commit_local.nil?
262
+ return com
263
+ end
264
+
265
+ end
266
+ end