Module: GollumRails::Persistance

Extended by:
ActiveSupport::Concern
Included in:
Page
Defined in:
lib/gollum_rails/persistance.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#create_or_updateObject

Creates a record or updates it!

Returns a Commit id string



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

def create_or_update
  if persisted?
    update_record
  else
    create_record
  end
end

#create_recordObject

Creates a record

Returns a Commit id



92
93
94
95
# File 'lib/gollum_rails/persistance.rb', line 92

def create_record
  wiki.write_page(canonicalized_filename, format, content, commit, path_name) 
  wiki.clear_cache
end

#delete(commit = nil) ⇒ Object

Deletes current page

commit - optional. If given this commit will be used instead of that one, used

to initialize the instance

Returns the commit id of the current action as String



149
150
151
# File 'lib/gollum_rails/persistance.rb', line 149

def delete(commit=nil)
  destroy(commit)
end

#destroy(commit = nil) ⇒ Object

Deletes current page

commit - optional. If given this commit will be used instead of that one, used

to initialize the instance

Returns the commit id of the current action as String



138
139
140
141
# File 'lib/gollum_rails/persistance.rb', line 138

def destroy(commit=nil)
  return false if @gollum_page.nil?
  wiki.delete_page(@gollum_page, get_right_commit(commit))
end

#persisted?Boolean

checks if entry already has been saved

Returns:

  • (Boolean)


156
157
158
159
# File 'lib/gollum_rails/persistance.rb', line 156

def persisted?
  return true if gollum_page
  return false
end

#saveObject

Handles the connection betweet plain activemodel and Gollum Saves current page in GIT wiki If another page with the same name is existing, gollum_rails will detect it and returns that page instead.

Examples:

obj = GollumRails::Page.new <params>
@article = obj.save
# => Gollum::Page

@article.name
whatever name you have entered OR the name of the previous
created page

TODO:

* overriding for creation(duplicates)
* do not alias save! on save

Returns an instance of Gollum::Page or false



57
58
59
60
61
62
63
64
65
66
# File 'lib/gollum_rails/persistance.rb', line 57

def save
  return nil unless valid?
  begin
    create_or_update
  rescue ::Gollum::DuplicatePageError => e
  end
  self.gollum_page = wiki.paged(file_name, path_name, true, wiki.ref)
  _update_page_attributes
  self
end

#save!Object

Save without exception handling

raises errors everytime something is wrong

Raises:

  • (StandardError)


71
72
73
74
75
76
# File 'lib/gollum_rails/persistance.rb', line 71

def save!
  raise StandardError, "record is not valid" unless valid?
  raise StandardError, "commit must not be empty" if commit == {}
  create_or_update
  self
end

#seperate_path(path) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/gollum_rails/persistance.rb', line 122

def seperate_path(path)
  path = File.split(name)
  if path.first == '/' || path.first == '.'
    folder = nil
  else
    folder = path.first
  end
end

#update_attributes(attributes) ⇒ Object

Updates an existing page (or created)

attributes - Hash of arguments

Returns an instance of GollumRails::Page



117
118
119
120
# File 'lib/gollum_rails/persistance.rb', line 117

def update_attributes(attributes)
  assign_attributes(attributes)
  save
end

#update_recordObject

Update a record

NYI

returns a Commit id



102
103
104
105
106
107
108
# File 'lib/gollum_rails/persistance.rb', line 102

def update_record
  wiki.update_page(self.gollum_page,
                   self.name,
                   self.format, 
                   self.content,
                   self.commit)
end