Does Ruby/Slim have support for markdown? - ruby-on-rails

I cant get markdown to work with Slim (Rails 3.1 app), I get this error:
Unknown line indicator
:markdown
I have the following gems in my Gemfile:
gem 'bluecloth'
gem 'rdiscount'
gem 'slim'
This is how my template looks like:
:markdown
#hello

Yes. Slim supports Markdown. You need to put markdown: before you use markdown code in your templates.
Example:
markdown:
#Header
Hello from #{"Markdown!"}
Second Line!

You might be able to use Markdown syntax with a single gem: redcarpet. Nothing against the other gems, but after a couple of years dealing with Rails I really believe less is more, especially when it comes to adding gems to your projects, it can get out of hand very quickly.
This page lists the requirements for Markdown support in Slim:
slim-template

Related

Rails bootstrap gem monkeypatching method not working

I'm using the excellent twitter-bootstrap-rails gem. There is a helper within that gem (NavbarHelper) which is used to generate Bootstrap navbars with a Ruby helper. I want to monkey patch the gem such that the dropdown lists won't have carets.
So, I looked into the source and found the relevant method here. All I have to do is override it. I created a new file in config/initializers called navbar.rb with the following content:
NavbarHelper.module_eval do
def name_and_caret(name)
"HELLO WORLD"
end
end
Presumably, all of the dropdown titles then should be rendered as "HELLO WORLD" in my app (as referenced by the gem source). However, this is not occurring, and the gem does not appear to be monkeypatched at all.
I tried placing puts NavbarHelper.methods - Object.methods in the initializers file, and there were no results, which makes me think that Rails is not loading the gem correctly before the initializers. I have also checked and verified that the gem is not using autoload for its helpers.
Edit
What may be complicating this is the fact that my Gemfile includes the gem in the following manner:
gem 'twitter-bootstrap-rails', git: 'git://github.com/seyhunak/twitter-bootstrap-rails.git', branch: 'bootstrap3'
I'm not sure if this specific versioning means the monkeypatching doesn't work.
Edit #2
It seems there is only one version of the gem on my system, so I don't think that's the issue. Also, I have tried placing require 'twitter-bootstrap-rails at the top of the initializers file, with no results.
The problem is that you patch the method on this module but the module already got included at this point. Try to define this in your application_helper.rb
def name_and_caret(name)
super("blub #{name}")
end

ruby gem that prepares data for datatables

I'd like to use datatables in my rails application but I'd like to avoid prepraring JSON data by myself, so I'm looking for a gem that does it. Ideally I'd pass an ActiveRecored Relation and the gem generated JSON that could be consumed by datatables, for example:
class ItemsController < ApplicationController
def index
# I fetch data I need (taking into account authorization, search etc.)
#items = Item.find_relevant_items
respond_to do |format|
# gem prepares JSON for datatables
format.json { ItemDatatable.new(#items) }
# ...
end
end
end
I'm aware that there are several gems availabe. However, they don't suit me: jquery-datatables-rails is just a wrapper for the JS and the others seem to be outdated or not maintained (ajax-datatables-rails, rails_datatables, simple_datatables).
Do you know about any gem that would serve data for datatables?
Ruby's JSON module is built-in, just require 'json' at the top of a Ruby script to make it available. See "How do I parse JSON with Ruby on Rails?" for more information.
Rails also includes JSON capability too. See "Understanding Ruby and Rails: Serializing Ruby objects with JSON" for info using JSON and Rails. Rails is a fast-moving platform so that might be a bit out of date, but it should get you started. "Lightning JSON in Rails" is a good read for things to pay attention to as you generate JSON.
"JSON implementation for Ruby" is a great reference for Ruby and JSON also.
The best way I have found to using datatables in a rails app is this:
jquery-datatables-rails gem to get all the assets datatables needs
ajax-datatables-rails gem to get the the JSON datatables expects when using server side pagination and searching.
There are other gems that make it easier to generate the JSON datatables need If you don't want to go through the trouble of making a new class like in the RailsCast. You can find them here
RABL and JBuilder both spring to mind.
They are json template handlers and awesomely simple to use and both are equally powerfull enabling complete customisation of your json output.
There are railscasts on them both
http://railscasts.com/episodes/320-jbuilder
http://railscasts.com/episodes/322-rabl
and the sites
https://github.com/rails/jbuilder
https://github.com/nesquena/rabl
Both are well maintained.
Otherwise the datatables-rails gem is really the best option for you
http://railscasts.com/episodes/340-datatables?view=asciicast
UPDATE 2
It's the gem you have already looked at
As per railscast the gem is defined in the Gemfile
group :assets do
gem 'jquery-datatables-rails', github: 'rweng/jquery-datatables-rails'
gem 'jquery-ui-rails'
end
I strongly urge you to watch that railscast (http://railscasts.com/episodes/340-datatables?view=asciicast) as there are other configurations needed.

Rails Application templates remove a default gem

When using a rails application template is possible to make so that some default gems (sqlite3, coffescript and sass) are not included in the Gemfile?
I'm sure you've solved this problem in the last 7 years, but for everyone else, the best method I've seen for doing this is to gsub it out of the file:
# Removes sqlite3, coffee-rails, and sass-rails gems
gsub_file "Gemfile", /^gem\s+["']sqlite3["'].*$/,''
gsub_file "Gemfile", /^gem\s+["']coffee-rails["'].*$/,''
gsub_file "Gemfile", /^gem\s+["']sass-rails["'].*$/,''
Yes, just modify your application template file to not include them.
take a look https://github.com/RailsApps/rails3-application-templates
to get a better idea, specifically mongoid templates

Ruby gem naming conventions

I'm creating a ruby gem and I've noticed that there doesn't seem to be (to my knowledge) a naming convention for gems. For example, I've seen both:
gem 'foo-bar'
gem 'foo_bar'
Is there some kind of definitive guide/convention for the naming of ruby gems?
The dashed version is for extensions on other frameworks, like rspec-rails and the underscore is for part of the normal gem name and should be camelcased in your classes.
So if you have a gem named foo_bar, the class/module should be named FooBar. If that gem should have a rails extension which ships as a different gem, it should be called foo_bar-rails and the module should be called FooBar::Rails and it should be required as require "foo_bar/rails"
This convention is also what Bundler tries to require.
Admittedly, this convention is not always followed. jquery_rails should actually be jquery-rails and factory_girl_rails should be called factory_girl-rails. But hey, not everything is perfect.
RubyGems convention docs:
Naming gems
Naming patterns
Make your own gem
Turns out that this is answered pretty clearly and succinctly in the rubygems docs: http://guides.rubygems.org/name-your-gem/
(This may be a recent doc addition because I recall searching for this info in the past and not finding it.)
The one advantage is the convention of collapsing foo_bar into module or class FooBar as far as autoloaders go. foo-bar doesn't have a default equivalent.
Generally the underscore version is preferable from a require perspective, but the dashed version does come across as more readable so it tends to get used often.
In a recommendation of #svenfuchs:
underscore => camelized
hyphen => name::space
https://twitter.com/svenfuchs/status/135773593526206464
But it's true that I still see non-coherence behaviors like:
gem 'my_gem`, :require => 'my-gem'
https://twitter.com/#!/svenfuchs/status/135784542819713024

BlueCloth isn't working with Rails 3

Is BlueCloth compatible with Rails 3? I can't make it work, maybe someone uses it?
There is supposed to be a helper called 'markdown' available in the views after requiring 'bluecloth', but this doesn't seem to be available.
I'm upgrading an app to rails3 right now and it worked fine for me. I use a helper function called "format" in templates though the code below also provides a markdown function (in rails3 you'll have to use that with raw()). Here's the contents of my [project]/app/helpers/application_helper.rb
module ApplicationHelper
# Format text for display.
def format(text)
sanitize(markdown(text))
end
# Process text with Markdown.
def markdown(text)
BlueCloth::new(text).to_html
end
end
Like a previous poster said, you'll also need
gem 'bluecloth'
in your [project]/Gemfile. My template looks like:
<p><%= format #post.body %></p>
With the markdown function it would be:
<p><%= raw(markdown(#post.body)) %></p>
So I use the format function. Rename the functions however you want.
I've created a fresh Rails 3 app and in the Gemfile I added:
gem 'bluecloth', '>= 2.0.0'
Then opened the console:
ruby-1.8.7-p302 > BlueCloth.new('**hello**').to_html
=> "<p><strong>hello</strong></p>"
So it appears to be working, at least for me.
You could also try Rdiscount which I am not shure but I think is based on the same C library, or at least has similar benchmarks.
You should be more specific in how is it not working: Does it raises an error? Doesn't it renders html? etc...
What you could do, not saying it is pretty, is creating an initializer in your rails project and put the following in it:
require 'bluecloth'
class String
def markdown
BlueCloth.new(self).to_html
end
end
This should enable the markdown method on every string object.
I'd suggest switching to RDiscount over BlueCloth. It's a drop in replacement and is better on all counts.
http://github.com/rtomayko/rdiscount

Resources