Class: DocumentationEditor::PagesController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ApplicationController
- DocumentationEditor::PagesController
- Defined in:
- app/controllers/documentation_editor/pages_controller.rb
Instance Method Summary collapse
- #commit ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #diff ⇒ Object
- #edit ⇒ Object
- #export ⇒ Object
- #history ⇒ Object
- #import ⇒ Object
- #index ⇒ Object
- #preview ⇒ Object
- #show ⇒ Object
- #source ⇒ Object
- #update ⇒ Object
- #upload_image ⇒ Object
- #upload_thumbnail ⇒ Object
- #versions ⇒ Object
Instance Method Details
#commit ⇒ Object
45 46 47 48 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 45 def commit r = @page.add_revision!(params[:data], params[:preview].to_s == 'false', respond_to?(:current_user) ? current_user.id : nil) render json: { id: r.id } end |
#create ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 50 def create p = Page.new p. = respond_to?(:current_user) ? current_user.id : nil p.title = params[:page][:title] p.slug = params[:page][:slug] p.description = params[:page][:description] p.languages_raw = params[:page][:languages_raw] p.section = params[:page][:section] p.save! redirect_to edit_page_path(p) end |
#destroy ⇒ Object
88 89 90 91 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 88 def destroy @page.destroy redirect_to :admin end |
#diff ⇒ Object
115 116 117 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 115 def diff render inline: Diffy::Diff.new(Revision.find(params[:prev]).content, Revision.find(params[:cur]).content, context: 5, include_plus_and_minus_in_html: true, include_diff_info: true).to_s(:html) end |
#edit ⇒ Object
27 28 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 27 def edit end |
#export ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 119 def export pages = Page.all.map do |p| { slug: p.slug, created_at: p.created_at, title: p.title, languages: p.languages, description: p.description, section: p.section, position: p.position, content: p.revisions.last.try(:content) } end send_data JSON.pretty_generate(pages), type: :json, disposition: 'attachment', filename: "export-#{DateTime.now}.json" end |
#history ⇒ Object
108 109 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 108 def history end |
#import ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 135 def import JSON.parse(params[:file][:content].read).each do |page| p = Page.where(slug: page['slug']).first || Page.new p.slug = page['slug'] p.created_at = page['created_at'] p.title = page['title'] p.languages = page['languages'] p.description = page['description'] p.section = page['section'] p.position = page['position'] p.save! p.add_revision!(page['content'], true) end redirect_to :admin end |
#index ⇒ Object
24 25 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 24 def index end |
#preview ⇒ Object
62 63 64 65 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 62 def preview @revision = @page.revisions.last @options = params end |
#show ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 67 def show @page = Page.find_by(slug: params[:slug]) if @page.nil? && params[:language].nil? && params[:slug] && (p = params[:slug].rindex '/') @page = Page.find_by!(slug: params[:slug][0..(p - 1)]) params[:language] = params[:slug][(p + 1)..-1] end raise ActiveRecord::RecordNotFound.new if @page.nil? @revision = @page.published_revision raise ActiveRecord::RecordNotFound.new if @revision.nil? @options = params @base_path = if params[:section] s = request.path.split('/') s.pop s.join('/') else request.path end render :preview end |
#source ⇒ Object
30 31 32 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 30 def source render json: { source: (@page.revisions.last.try(:content) || 'FIXME') } end |
#update ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 34 def update @page.position = params[:page][:position] unless params[:page][:position].nil? @page.title = params[:page][:title] unless params[:page][:title].nil? @page.slug = params[:page][:slug] unless params[:page][:slug].nil? @page.description = params[:page][:description] unless params[:page][:description].nil? @page.languages_raw = params[:page][:languages_raw] unless params[:page][:languages_raw].nil? @page.section = params[:page][:section] unless params[:page][:section].nil? @page.save! render nothing: true end |
#upload_image ⇒ Object
93 94 95 96 97 98 99 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 93 def upload_image image = Image.new image.caption = params[:caption] image.image = params[:file] image.save! render json: { id: image.id, url: image.image.url, caption: image.caption } end |
#upload_thumbnail ⇒ Object
101 102 103 104 105 106 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 101 def upload_thumbnail thumb = @page.build_thumbnail thumb.image = params[:file] @page.save! render json: { id: thumb.id, url: thumb.image.url } end |
#versions ⇒ Object
111 112 113 |
# File 'app/controllers/documentation_editor/pages_controller.rb', line 111 def versions render json: { revisions: @page.revisions.order('id DESC').all } end |