Class: Wiki::Attachment

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Dirty
Defined in:
app/models/wiki/attachment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Attachment

Returns a new instance of Attachment.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/wiki/attachment.rb', line 41

def initialize(params={})
  wiki = params[:wiki]
  gollum_file = params[:gollum_file]
  path = params[:path]

  if wiki
    @wiki = wiki
  end
  if gollum_file
    self.gollum_file = gollum_file
  elsif path
    safepath = path.downcase
    @path = safepath
    new_file = @wiki.find_gollum_file(safepath)
    if new_file
      self.gollum_file = new_file
    end
  end
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



13
14
15
# File 'app/models/wiki/attachment.rb', line 13

def content
  @content
end

#formatObject

Returns the value of attribute format.



13
14
15
# File 'app/models/wiki/attachment.rb', line 13

def format
  @format
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'app/models/wiki/attachment.rb', line 13

def name
  @name
end

#pathObject

Returns the value of attribute path.



13
14
15
# File 'app/models/wiki/attachment.rb', line 13

def path
  @path
end

#wikiObject

Returns the value of attribute wiki.



13
14
15
# File 'app/models/wiki/attachment.rb', line 13

def wiki
  @wiki
end

Instance Method Details

#destroy!(user) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/wiki/attachment.rb', line 90

def destroy!(user)
  commit_options = {name: user.name, email: user.email, message: "removed #{@path}"}
  Rails.logger.debug("removing #{@path}")

  committer = Gollum::Committer.new(@wiki.gollum_wiki, commit_options)
  committer.delete(@path)

  committer.after_commit do |index, _sha|
    dir = File.dirname(@path)
    dir = '' if dir == '.'

    @wiki.gollum_wiki.clear_cache
    committer.update_working_dir(dir, parent_path, @format)
  end

  @wiki.pull_repo
  committer.commit
  @wiki.push_repo
end

#filesystem_pathObject



73
74
75
# File 'app/models/wiki/attachment.rb', line 73

def filesystem_path
  return File.join(@wiki.base_path, @path)
end

#gollum_fileObject



69
70
71
# File 'app/models/wiki/attachment.rb', line 69

def gollum_file
  @gollum_file
end

#gollum_file=(file) ⇒ Object



61
62
63
64
65
66
67
# File 'app/models/wiki/attachment.rb', line 61

def gollum_file=(file)
  @gollum_file = file
  @name = file.name
  @format = File.extname(file.name).split('.').last.to_sym
  # filename = ::Attachment.basename(fullname, ext)
  @path = file.url_path + file.name
end

#idObject

Returns the value of attribute path.



14
15
16
# File 'app/models/wiki/attachment.rb', line 14

def path
  @path
end

#mime_typeObject



77
78
79
# File 'app/models/wiki/attachment.rb', line 77

def mime_type
  @gollum_file.mime_type
end

#parent_pathObject



85
86
87
88
# File 'app/models/wiki/attachment.rb', line 85

def parent_path
  parent_path = File.dirname(@path)
  (parent_path == '.') ? '' : parent_path + '/'
end

#persisted?Boolean

Returns:

  • (Boolean)


110
111
112
113
# File 'app/models/wiki/attachment.rb', line 110

def persisted?
  # TODO: make this a real dirty flag
  true
end

#to_paramObject



81
82
83
# File 'app/models/wiki/attachment.rb', line 81

def to_param
  @path
end

#to_sObject



26
27
28
# File 'app/models/wiki/attachment.rb', line 26

def to_s
  self.path
end

#update(hash) ⇒ Object



34
35
36
37
38
39
# File 'app/models/wiki/attachment.rb', line 34

def update(hash)
  self.name = hash[:name]
  self.content = hash[:content]

  return true
end