Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/assets/javascripts/articles.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,23 @@ renderMarkdown = (text, update_element, linenum) ->
setupRenderPreviewButton = (selector) ->
$(selector).on 'click', (e) ->
previewArea = $(".preview_area")
user_id = $('#article_user_id').val()
title = $('#article_title').val()
perma_link = $('#article_perma_link').val()
content = $('#article_content').val()
format = $('#article_format').val()
$. ajax
async: true
type: "POST"
url: rootPath + "articles/preview"
dataType: "html"
data:
article:
user_id: user_id
title: title
perma_link: perma_link
content: content
format: format
success: (html, status, xhr) ->
previewArea.empty()
previewArea.append(html)
Expand Down
37 changes: 21 additions & 16 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def new
@article.perma_link = User.current.ident + "-" + Time.now.to_s(:perma_link)
@article.user_id = User.current.id
@article.published_on = Time.now
@article.format = "GFM"
end

# GET /articles/1/edit
Expand Down Expand Up @@ -95,21 +96,25 @@ def destroy
end

def preview
title =
"""
<div class='title-bar'>
<span class='title'>
#{article_params[:title]}
</span>
</div>
"""
clear = "<div class='clear'></div>"

render text: title + Kramdown::Document.new(article_params[:content],
input: "GFM",
syntax_highlighter: :rouge,
syntax_highlighter_opts: {css_class: 'highlight'}
).to_html + clear
if Article.new(article_params).invalid?(:except_preview)
render text: "Bad request", status: 400
else
title =
"""
<div class='title-bar'>
<span class='title'>
#{article_params[:title]}
</span>
</div>
"""
clear = "<div class='clear'></div>"

render text: title + Kramdown::Document.new(article_params[:content],
input: article_params[:format],
syntax_highlighter: :rouge,
syntax_highlighter_opts: {css_class: 'highlight'}
).to_html + clear
end

end

Expand Down Expand Up @@ -157,7 +162,7 @@ def set_parameters_for_sidebar

# Never trust parameters from the scary internet, only allow the white list through.
def article_params
params.require(:article).permit(:user_id, :title, :perma_link, :content, :published_on, :approved, :count, :promote_headline)
params.require(:article).permit(:user_id, :title, :perma_link, :content, :published_on, :approved, :count, :promote_headline, :format)
end

#count up the number of view
Expand Down
3 changes: 2 additions & 1 deletion app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class Article < ActiveRecord::Base

validates :user_id, presence: true
validates :title, presence: true
validates :perma_link, presence: true, uniqueness: true
validates :perma_link, presence: true, uniqueness: true, unless: proc { [:except_preview].include?(validation_context) }
validates :format, presence: true, inclusion: {in: ["Kramdown", "GFM"]}

paginates_per 10

Expand Down
1 change: 1 addition & 0 deletions app/views/articles/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<% end %>

<%= f.hidden_field :user_id %>
<%= f.hidden_field :format %>

<div class="field form-group">
<%= f.label :title %><br>
Expand Down
2 changes: 1 addition & 1 deletion app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<div class="content">
<%= raw Kramdown::Document.new(@article.content,
input: "GFM",
input: @article.format,
syntax_highlighter: :rouge,
syntax_highlighter_opts: {css_class: 'highlight'}
).to_html %>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20160930052633_add_format_to_articles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddFormatToArticles < ActiveRecord::Migration
def change
add_column :articles, :format, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20151021042053) do
ActiveRecord::Schema.define(version: 20160930052633) do

create_table "articles", force: :cascade do |t|
t.integer "user_id"
Expand All @@ -24,6 +24,7 @@
t.boolean "promote_headline"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "format"
end

add_index "articles", ["perma_link"], name: "index_articles_on_perma_link", unique: true
Expand Down