Class: Wiki::PagesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/wiki/pages_controller.rb

Instance Method Summary collapse

Instance Method Details

#add_tagObject



132
133
134
135
136
137
# File 'app/controllers/wiki/pages_controller.rb', line 132

def add_tag
  @page.tag_list.add(params[:tag])
  @page.save
  @page.__elasticsearch__.index_document
  render json: {}
end

#attachObject



106
107
108
109
110
111
112
113
# File 'app/controllers/wiki/pages_controller.rb', line 106

def attach
  @attachment = Attachment.create_from_uploaded_file(params[:file], current_user, attachable: @page)

  if @attachment.errors.none?
    @activity = @page.create_activity key: "page.attachment", 
      owner: current_user, recipient: @attachment
  end
end

#combinedObject



57
58
59
60
# File 'app/controllers/wiki/pages_controller.rb', line 57

def combined
  @subtree = @page.subtree.arrange
  render layout: "minimal"
end

#compareObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/wiki/pages_controller.rb', line 33

def compare
  version = @page.versions.select{|x| x.sha == params[:version]}.first
  filename = version.show.first.b_path

  #TODO: should cache the diffs (store on save page in delayed job)
  @old_html = version.repo.git.show({}, "#{version.sha}~1:#{filename}").force_encoding('UTF-8')
  @new_html = version.repo.git.show({}, "#{version.sha}:#{filename}").force_encoding('UTF-8')

  giri = Nokogiri::HTML(DaisyDiff.strings(@old_html, @new_html))
  @diff_html = giri.css("body")
  @diff_html.css(".diffpage-html-firstlast").remove
  @diff_html.css("script").remove
  @diff_html.xpath('//@onclick').remove
  @diff_html.xpath('//@onload').remove
  @diff_html.xpath('//@onerror').remove
  @diff_html.xpath('//@onabort').remove

  @diff_html.css("[changeid][id], img[changetype]").each do |element|
    element['data-toggle'] = "tooltip"
    element['data-title'] = "Here!"
    element['data-trigger'] = "manual"
  end
end

#createObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/wiki/pages_controller.rb', line 9

def create
  create_params = page_params
  if params[:markdown_body].present?
    create_params[:body] = redcarpet(params[:markdown_body])
  end
  @page = Page.new create_params.merge(
    creator: current_user, 
    parent_id: params[:parent_id]
  )
  if @page.save
    @page.update_to_gollum(current_user)
    @page.create_activity key: "page.created", owner: current_user
  end
end

#destroyObject



146
147
148
149
150
151
152
153
154
# File 'app/controllers/wiki/pages_controller.rb', line 146

def destroy
  parent = @page.parent
  @page.save_and_destroy(current_user)

  link = root_url
  link = wiki_page_path(parent) if parent

  redirect_to link, notice: "Page was deleted.", status: :see_other
end

#historyObject



30
31
# File 'app/controllers/wiki/pages_controller.rb', line 30

def history
end

#indexObject



5
6
7
# File 'app/controllers/wiki/pages_controller.rb', line 5

def index
  @roots = Page.roots
end

#newObject



82
83
84
# File 'app/controllers/wiki/pages_controller.rb', line 82

def new
  @page = Page.new creator: current_user
end

#remove_tagObject



139
140
141
142
143
144
# File 'app/controllers/wiki/pages_controller.rb', line 139

def remove_tag
  @page.tag_list.remove(params[:tag])
  @page.save
  @page.__elasticsearch__.index_document
  render json: {}
end

#reorderObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/wiki/pages_controller.rb', line 86

def reorder
  old_parent_id = @page.parent_id
  old_position = @page.position
  if @page.reorder!(
    parent_id: params[:parent_id],
    position: params[:position]
  )
    @activity = @page.create_activity key: "page.reorder", 
      owner: current_user, 
      parameters: {
        from_parent_id: old_parent_id,
        old_position: old_position,
        parent_id: params[:parent_id], 
        position: params[:position]
      }
  end

  render json: {}
end

#showObject



24
25
26
27
28
# File 'app/controllers/wiki/pages_controller.rb', line 24

def show
  @space = @page.root
  @subtree = @page.subtree.group_by(&:parent_id)
  @last_updated = @page.last_updated
end

#starObject



119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/wiki/pages_controller.rb', line 119

def star
  starred = Starred.where(user_id: current_user.id, starrable_id: @page, starrable_type: @page.class).first

  if starred && params[:to_star] == "false"
    starred.destroy
  end

  if !starred && params[:to_star] == "true"
    Starred.create(user: current_user, starrable: @page)
  end
  render json: {}
end

#subpages_dropdownObject



115
116
117
# File 'app/controllers/wiki/pages_controller.rb', line 115

def subpages_dropdown
  render partial: 'subpages_dropdown'
end

#updateObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/wiki/pages_controller.rb', line 62

def update
  update_params = params.require(params[:type]).permit(
    :title, :body, 
  )
  if params[:markdown_body].present?
    update_params[:body] = redcarpet(params[:markdown_body])
  end

  if (params[:timestamp].present? && @page.last_updated.present?) && 
    ((params[:timestamp].present? && !@page.last_updated.present?) ||
    @page.last_updated.try(:created_at) != Time.parse(params[:timestamp]))
    @page.errors.add(:base, "This page was edited by someone. Please refresh to get the updated changes. Refreshing the page will remove the your changes.")
  end

  if @page.errors.none? && @page.update_attributes(update_params)
    @page.update_to_gollum(current_user)
    @last_updated = @page.create_activity key: "page.update_details", owner: current_user
  end
end