How to override blog template generator (article.tt) in Middleman Blog Gem? - middleman

The middleman blog gem creates new articles based on lib/middleman-blog/commands/article.tt Current format is is;
---
title: <%= #title %>
date: <%= #date.strftime('%F %R %Z') %>
tags:
---
What is the best way to override that if I wanted default front matter to be something else like;
---
layout: blog
author: Dave
---
(Other than opening the gem and changing it there.)

Solved it by adding blog.new_article_template to blog settings in config.rb like so
activate :blog do |blog|
blog.prefix = 'blog'
blog.new_article_template = 'source/blog/template.erb'
end

There is no built-in way to override that.
But probably you could find where in Middleman Blog code that file is invoked and override that method from config.rb, having it pointing at a different template file.

Related

Rails Nested Singular Resource Routing Appending .ID

I have the following nested singular route:
resources :listings do
resource :logo, only: [ :edit, :update ]
end
It generates 2 correct routes as expected:
edit_listing_logo GET /listings/:listing_id/logo/edit(.:format) logos#edit
listing_logo PUT /listings/:listing_id/logo(.:format) logos#update
Now when I redirect to edit_listing_logo_path
redirect_to edit_listing_logo_path( #listing, #logo )
or when I create an update form
<%= form_for [ #listing, #logo ] do |f| %>
the resulting link has always singular resource ID attached at the end like this
/listings/2/logo.1
I'm not using respond formats in this app yet so it's working fine. But this link generation seems strange and I expect it to cause problems if used with various respond formats.
Note, that this was also discussed here Rails Nested Singular Resource Routing but I'm not using 'show' path at all.
Thanks for any inputs.
I believe you can get the answer you need either by using the verbose path method edit_listing_logo_path(#listing) (as mentioned by #Martin Sojka) or by using a symbol instead of the object itself.
So instead of using the normal form:
form_for [ #listing, #logo ]
you can dispose of the object specificity (and the trailing ID) by writing:
form_for [#listing :logo]
and if you want to call another action on the controller you can prefix it in the array:
form_for [:edit, #listing :logo]
Hope it works for you - it seemed to solve my issues.

Showing Link in Text Field in Rails

In my Rails 3.1 app, I have a text field for comments and I want to be able to allow people to include clickable links (instead of just the url showing as plain text), as well as having the text field recognize when a user had line breaks in their text field (without the user adding html). How can I do this?
This works for showing a link if a user puts the html for a href:
<%= simple_format(#user.description) %>
And this works for recognizing and displaying the line breaks from carriage returns in the text_field:
<%= h(#user.description).gsub(/\n/, '<br/>').html_safe %>
However, I haven't figured out how to do both, together.
How about this?
#Doesnt work in this case
#<%= simple_format( h(#user.description).gsub(/\n/, '<br/>') ).html_safe %>
EDIT:
Seems like you need auto_link function to achieve this. Though it is removed from rails 3.1 it is available as a gem. So if you are using rails 3.1 or later you need to get this from a separate gem
#in Gemfile
gem "rails_autolink", "~> 1.0.9"
#in application.rb
require 'rails_autolink'
#Run
bundle install
#now in you view use it like
<%= h auto_link(simple_format(text)) %>
auto_link not only converts urls but also email addresses in clickable link. Get the document here.
Reference Links:
http://rubygems.org/gems/rails_autolink
http://apidock.com/rails/ActionView/Helpers/TextHelper/auto_link
Use the Rinku Gem
Link is here.
It brilliantly solves the problem. Enjoy!

HAML, Add class to image_tag

I have
= image_tag "chart.jpg"
I am new to HAML so how do I add a class to this?
Assuming this is rails (Haml doesn't have its own image_tag helper, but Rails does), then the second argument is an options hash where you can specify the class:
=image_tag "chart.jpg", :class => "my_class"
Stumbled across this answer when I was looking for something else and thought I'd throw in a little update to use the more modern formatting :)
=image_tag 'chart.jpg', class: 'my_class'

How do I make a settings configuration page for the rails-settings gem?

I just discovered the rails-settings gem and now I need to make an admin page that lets me edit the setting values. How would I make a settings controller with an edit view that can change these dynamic app wide settings?
I haven't used this gem but it seems like it should be fairly straight forward. Since it uses a database backed model, you would simply create a controller as normal:
rails g controller Settings
From here you would define your index action to gather all your individual settings for display in the view:
def index
#settings = Settings.all
end
Then in the view you can setup a loop to display them:
<% #settings.each do |setting| %>
<%= setting.var %> = <%= setting.value %>
<% end %>
As far as editing ... this might be a bit tricky since by default rails would expect you to submit only one setting at a time to edit. You could do it this way but unless you implement the edit with ajax it might be tedious and non-intuitive.
Another way would be to set up your update method to accept all the individual settings at once, loop through and update each one with new values. It might look something like this:
// The /settings route would need to be setup manually since it is without an id (the default)
<%= form_tag("/settings", :method => "put") do %>
<% #settings.each do |setting| %>
<%= label_tag(setting.var, setting.var) %>
<%= text_field_tag(setting.var, :value => setting.value) %>
<% end %>
<%= submit_tag("Save Changes") %>
<% end %>
This should output all of the settings (given they have been assigned to the #settings variable) with the var name as the label and the current value as the text field value. Assuming that the routing is setup, when you submit this form the action that receives it should all the new settings in the params variable. Then you can do something like this in the action:
def update
params.each_pair do |setting, value|
eval("Settings.#{setting} = #{value}")
end
redirect_to settings_path, :notice => 'Settings updated' # Redirect to the settings index
end
This may not be the best way depending on how often you edit the settings and how many settings you have...but this is a possible solution.
I was looking for some suggestions for this and found another answer to this that is very simple and elegant, for anyone looking for this later. It just sets up dynamic accessors in your model, allowing your form to have settings fields just like your normal attributes. An example can be found in the original answer:
How to create a form for the rails-settings plugin

How can I automatically render partials using markdown in Rails 3?

I want to have some of my partials as markdown snippets. What is the easiest way to render them using the standard rails erb templating?
Ideally, I'd like to do something like this:
If I have a partial in app/views/_my_partial.md.erb:
My awesome view
===============
Look, I can **use** <%= language %>!
which I reference from a view like so:
<%= render "my_partial", :language => "Markdown!" %>
I want to get output that looks like this:
<h1>My awesome view</h1>
<p>Look, I can <strong>use</strong> Markdown!</p>
Turns out, the Right Way (tm) to do this is using ActionView::Template.register_template_handler:
lib/markdown_handler.rb:
require 'rdiscount'
module MarkdownHandler
def self.erb
#erb ||= ActionView::Template.registered_template_handler(:erb)
end
def self.call(template)
compiled_source = erb.call(template)
"RDiscount.new(begin;#{compiled_source};end).to_html"
end
end
ActionView::Template.register_template_handler :md, MarkdownHandler
If you require 'markdown_handler' in your config/application.rb (or an initializer), then any view or partial can be rendered as Markdown with ERb interpolation using the extension .html.md:
app/views/home/index.html.md:
My awesome view
===============
Look, I can **use** <%= #language %>!
app/controllers/home_controller.rb:
class HomeController < ApplicationController
def index
#language = "Markdown"
end
end
Not a pure markdown solution but you can use HAML filters to render markdown, as well as other markup languages.
For example, in app/views/_my_partial.html.haml:
:markdown
My awesome view
===============
Look, I can **use** #{language}!
Have found way not to use haml in such situation.
in views/layouts/_markdown.html.erb
<%= m yield %>
in app/helpers/application_helper.rb
def m(string)
RDiscount.new(string).to_html.html_safe
end
in Gemfile
gem 'rdiscount'
So, in view you can call it like:
<%= render :partial => "contract.markdown", :layout => 'layouts/markdown.html.erb' %>
And contract.markdown will be formatted as markdown
I just released a markdown-rails gem, which handles .html.md views.
You cannot chain it with Erb though -- it's only for static views and partials. To embed Ruby code, you'd have to use tjwallace's solution with :markdown.
Piling on the solutions already presented, this is an interpolation-ary way in Rails 3 to render a pure Markdown file in a view from a partial without unnecessary indentation using Haml's :markdown filter and the RDiscount gem. The only catch is that your Markdown file is a Haml file, but that shouldn't matter for someone like a copy person.
In Gemfile:
gem 'rdiscount'
In app/views/my_page.html.haml
:markdown
#{render 'my_partial', language: 'Markdown!'}
In app/views/_my_partial.html.haml
My awesome view
===============
Look, I can **use** #{language}!
If you didn't need the :language variable passed in to the markdown file, you could do away altogether with your Markdown being a Haml file:
In app/views/my_page.html.haml
:markdown
#{render 'my_partial.md'}
In app/views/_my_partial.md
My awesome view
===============
Sorry, cannot **use** #{language} here!
Don't like those pesky underscores on your Markdown files?
In app/views/my_page.html.haml
:markdown
#{render file: 'my_markdown.md'}
In app/views/my_markdown.md
My awesome view
===============
Sorry, cannot **use** #{language} here!
Leveraged your answer to make a gem to render for GitHub Flavored Markdown in Rails (via HTML::Pipeline): https://github.com/afeld/html_pipeline_rails
Here is a version similar to #Jacob's but using Redcarpet.
module MarkdownHandler
def self.erb
#erb ||= ActionView::Template.registered_template_handler(:erb)
end
def self.call(template)
options = {
fenced_code_blocks: true,
smartypants: true,
disable_indented_code_blocks: true,
prettify: true,
tables: true,
with_toc_data: true,
no_intra_emphasis: true
}
#markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, options)
"#{#markdown.render(template.source).inspect}.html_safe"
end
end
ActionView::Template.register_template_handler :md, MarkdownHandler
Full credit to lencioni who posted this in this gist.
And if you'd like to evaluate erb:
erb = ERB.new(template.source).result
#markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, options)
"#{#markdown.render(erb).inspect}.html_safe"
You can use embedded Markdown in Rails 5. Embedded Markdown is based on the solution provided by Jacob above
Add these to your application's Gemfile:
gem 'coderay' #optional for Syntax Highlighting
gem 'redcarpet'
gem 'emd'
bundle install.
Then create a view app/view/home/changelog.html.md and paste your markdown in that .md file.
Generate a home controller using the following command
rails generate controller home
Add the following line to your route.rb:
get '/changelog', :to 'home#changelog'
That's all. Visit http://localhost:3000/changelog to see your rendered markdown
Source: http://github.com/ytbryan/emd