Added markdown

This commit is contained in:
Dan Elbert 2017-04-13 16:18:20 -05:00
parent a11ab12cd8
commit 624ca9ee7a
11 changed files with 55 additions and 17 deletions

View File

@ -19,6 +19,7 @@ gem 'turbolinks', '~> 5.0.1'
gem 'jbuilder', '~> 2.6'
gem 'cocoon', '~> 1.2.9'
gem 'unitwise', '~> 2.0.0'
gem 'redcarpet', '~> 3.4.0'
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.11'

View File

@ -164,6 +164,7 @@ GEM
rb-fsevent (0.9.8)
rb-inotify (0.9.8)
ffi (>= 0.5.0)
redcarpet (3.4.0)
ref (2.0.0)
rspec (3.5.0)
rspec-core (~> 3.5.0)
@ -245,6 +246,7 @@ DEPENDENCIES
puma
rails (= 5.0.2)
rails-controller-testing
redcarpet (~> 3.4.0)
rspec-rails (~> 3.5.0)
sass-rails (~> 5.0)
sqlite3

View File

@ -106,6 +106,6 @@ class RecipesController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def recipe_params
params.require(:recipe).permit(:name, :description, :source, :yields, :total_time, :active_time, tag_names: [], recipe_ingredients_attributes: [:name, :ingredient_id, :quantity, :units, :preparation, :sort_order, :id, :_destroy], recipe_steps_attributes: [:step, :sort_order, :id, :_destroy])
params.require(:recipe).permit(:name, :description, :source, :yields, :total_time, :active_time, :step_text, tag_names: [], recipe_ingredients_attributes: [:name, :ingredient_id, :quantity, :units, :preparation, :sort_order, :id, :_destroy])
end
end

View File

@ -44,6 +44,10 @@ class BaseDecorator < SimpleDelegator
ERB::Util.html_escape(*args)
end
def markdown(text)
MarkdownProcessor.render(text).html_safe
end
def wrapped
__getobj__
end

View File

@ -31,4 +31,8 @@ class RecipeDecorator < BaseDecorator
end
end
def step_text
markdown(wrapped.step_text)
end
end

View File

@ -64,21 +64,15 @@
<h3>Steps</h3>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#bulk_steps_modal">Bulk Edit</button>
<div id="step-list">
<%= f.fields_for :recipe_steps do |rs_form| %>
<%= render partial: 'recipes/editor/step', locals: { f: rs_form } %>
<% end %>
<div class="steps">
<div class="form-group form-group-sm">
<%= f.text_area :step_text, class: 'form-control', rows: 15 %>
</div>
</div>
<div id="deleted_steps"></div>
<%= link_to_add_association 'Add Step', f, :recipe_steps, partial: 'recipes/editor/step', :'data-association-insertion-node' => '#step-list', :'data-association-insertion-method' => 'append', class: 'btn btn-primary', id: 'addStepButton' %>
<% content_for(:page_bottom) do %>
<%= render partial: 'recipes/editor/bulk_ingredient_dialog' %>
<%= render partial: 'recipes/editor/bulk_step_dialog' %>
<div class="modal fade" id="convert_modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">

View File

@ -73,11 +73,7 @@
<h3 class="panel-title">Directions</h3>
</div>
<div class="panel-body">
<ol class="steps">
<% @recipe.recipe_steps.each do |s| %>
<li><div><%= "#{s.step}" %></div></li>
<% end %>
</ol>
<%= @recipe.step_text %>
</div>
</div>
</div>

View File

@ -1,3 +1,4 @@
require 'markdown_processor'
require 'unit_conversion'
require 'yield_parser'

View File

@ -0,0 +1,5 @@
class AddStepTextToRecipes < ActiveRecord::Migration[5.0]
def change
add_column :recipes, :step_text, :text
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161014173138) do
ActiveRecord::Schema.define(version: 20170413173225) do
create_table "ingredient_units", force: :cascade do |t|
t.integer "ingredient_id", null: false
@ -108,6 +108,7 @@ ActiveRecord::Schema.define(version: 20161014173138) do
t.integer "user_id"
t.boolean "is_log"
t.float "rating"
t.text "step_text"
end
create_table "recipes_tags", id: false, force: :cascade do |t|

30
lib/markdown_processor.rb Normal file
View File

@ -0,0 +1,30 @@
module MarkdownProcessor
class << self
def render_options
{}
end
def markdown_extensions
{
no_intra_emphasis: true,
fenced_code_blocks: true,
disable_indented_code_blocks: true
}
end
def renderer
@renderer ||= Redcarpet::Render::SmartyHTML.new(render_options)
end
def markdown_obj
@markdown_obj ||= Redcarpet::Markdown.new(renderer, markdown_extensions)
end
def render(text)
return '' unless text.present?
markdown_obj.render(text)
end
end
end