diff --git a/app/assets/javascripts/articles.coffee b/app/assets/javascripts/articles.coffee index 142336c..fe9f610 100644 --- a/app/assets/javascripts/articles.coffee +++ b/app/assets/javascripts/articles.coffee @@ -187,8 +187,11 @@ 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" @@ -196,8 +199,11 @@ setupRenderPreviewButton = (selector) -> 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) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index fbb5e46..acb18a3 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -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 @@ -95,21 +96,25 @@ def destroy end def preview - title = - """ -
- - #{article_params[:title]} - -
- """ - clear = "
" - - 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 = + """ +
+ + #{article_params[:title]} + +
+ """ + clear = "
" + + 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 @@ -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 diff --git a/app/models/article.rb b/app/models/article.rb index 05e4d0a..f69e35d 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -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 diff --git a/app/views/articles/_form.html.erb b/app/views/articles/_form.html.erb index 64a19bd..db970b2 100644 --- a/app/views/articles/_form.html.erb +++ b/app/views/articles/_form.html.erb @@ -12,6 +12,7 @@ <% end %> <%= f.hidden_field :user_id %> + <%= f.hidden_field :format %>
<%= f.label :title %>
diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index c9be65d..9480c86 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -17,7 +17,7 @@
<%= raw Kramdown::Document.new(@article.content, - input: "GFM", + input: @article.format, syntax_highlighter: :rouge, syntax_highlighter_opts: {css_class: 'highlight'} ).to_html %> diff --git a/db/migrate/20160930052633_add_format_to_articles.rb b/db/migrate/20160930052633_add_format_to_articles.rb new file mode 100644 index 0000000..38ec3c0 --- /dev/null +++ b/db/migrate/20160930052633_add_format_to_articles.rb @@ -0,0 +1,5 @@ +class AddFormatToArticles < ActiveRecord::Migration + def change + add_column :articles, :format, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index a1b9b8f..84d4080 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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