How to force Kaminari to always include page param? - ruby

Kaminari's URL generation omits the page param if it is generating the link back to the first page. However, the application is designed to select a random page if the page parameter is omitted. Kaminari's default behaviour, then, precludes paginating back to the first page in a reliable way.
I've resolved this issue, and will post my solution below a bit later, but I wanted to post this question for posterity, and I'm also pretty new to Rails, thus I'm not sure my solution is the best or most elegant, and I'm interested in improvements and refinements, if just for my own selfish edification!

The line of code in Kaminari that implements the behaviour we want to change is in lib/kaminari/helpers/tags.rb, in the method Kaminari::Helpers::Tag::page_url_for.
def page_url_for(page)
#template.url_for #template.params.merge(#param_name => (page <= 1 ? nil : page))
end
To override this behaviour, I created a file lib/kaminari/helpers/tag.rb, containing the following:
module Kaminari
module Helpers
class Tag
def page_url_for(page)
#template.url_for #template.params.merge(#param_name => (page < 1 ? nil : page))
end
end
end
end
I then patched in the file by adding the following line to config/initializers/extensions.rb:
require "lib/kaminari/helpers/tag.rb"
My apologies for any awkwardness with the Ruby/Rails terminology, I'm still fairly new to Ruby. Comments and criticisms are welcome.

UPDATE
The new version of the kaminari source will require this as the updated line:
#template.url_for #params.merge(#param_name => (page))
Otherwise you will lose other params passed into your pagination call.
For clairity sake here is the full output of the new code:
module Kaminari
module Helpers
class Tag
def page_url_for(page)
#template.url_for #params.merge(#param_name => (page))
end
end
end
end
You will still place this inside an initializers file as Daniel suggested.

As of today (July 2016), the Kaminari master branch includes a config option params_on_first_page, which is false by default.
Setting this config option to true will include page params for all pages, including page 1.
Note that the master branch isn't a stable release, so use with caution!

This is the answer for 2018 as am writing this :
Like it's stated in the kaminari github home page
Run this to create a config file for kaminari :
rails g kaminari:config
This will create a file kaminari_config.rb in your config/initializers folder
Uncomment the line : config.params_on_first_page = false and replace false by true :
config.params_on_first_page = true
Restart your server if necessary. That's it :)

Related

Unknown response for all methods and commands in ruby-asterisk

Testing ruby-asterisk manager interface with ruby version 1.9.3p0 and gem 1.8.11, for all command and methods its printing the the same output.
Anyone faced similar problem.
Code:
#!/usr/bin/env ruby
require 'ruby-asterisk'
#ami = RubyAsterisk::AMI.new("192.168.1.5",5038)
#ami.login("admin","passs")
puts #ami.command("sip show peers")
Output:
#<RubyAsterisk::Response:0x000000016af710>
Project URL
Problem solved. Didn’t check the readme RESPONSE OBJECT section.
It's working.
var = #ami.command(""sip show peers)
puts var.data
You are putting the Instance of the RubyAsterix. I think after haveing a brief look at the project that most/all of the instance methods returns the instance it self. The reason for doing it that way is that it makes it very easy to chain multiplie actions which makes for a nice syntax/usage.
I think you should remove the puts and allow the gem to display what it wants to display.

Get Post Full Path in Jekyll/Octopress

In Octopress, I'm trying to get a post's full file path (something like ~/projects/site/source/_posts/2012-01-01-something.markdown) by extending the Jekyll:Post class.
module Jekyll
class Post
alias_method :original_to_liquid, :to_liquid
def to_liquid
# test if this function is actually called
puts "hello"
original_to_liquid.deep_merge({
'full_path' => File.join(#base,#name)
})
end
end
end
I name this file as full_path.rb and put it in the plugins folder. Oddly, my to_liquid function never get called, since the hello message didn't show up.
Even more strange, I find the date.rb shipped with Octopress also defines to_liquid method of class Post, so I add the full_path => File.join(#base,#name) line there and it works! I'm soooo confused.
So my question is, why my to_liquid method didn't get called?
UPDATE
After upgrading jekyll from 0.12.0 to 1.2.1, it magically works......
You might take a look at the Post#permalink documentation. It should do what you want without having to create new plugins.
(if I misunderstood you, maybe containing_dir is the method you're looking for)

Most appropriate place to require library in Padrino/Sinatra

I'm using "therubyracer" in a model, and I'm requiring at the top of the model as so:
require 'v8'
class Thing
def self.ctx; ##ctx ||= V8::Context.new; end;
def self.eval(script); ctx.eval(script); end;
end
However, I intermittently get:
NameError - uninitialized constant Thing::V8:
/app/thing.rb:3:in `ctx'
When testing requests through a local Padrino server, apparently after I modify code in Thing. This is corrected by restarting the padrino server. I'm assuming requiring v8 somewhere else would fix this problem, wheres the correct place?
This looks like it might be caused by the Padrino reloader getting confused when it reloads your thing.rb file, causing Ruby to look for V8 in the Thing namespace.
Try explicitly specifying V8 is in the top level using the :: prefix:
def self.ctx; ##ctx ||= ::V8::Context.new; end;
You can put it wherever you want if you add it on the Gemfile. Did you added it?
Thanks!

How to extend redcarpet to support auto linking user mentions?

On my rails3 application I want to use redcarpet to handle user's posts and the user comment section. As such I'd like to extend redcarpet to support turning #username into a link to a user on my site. I know redcarpet is written in C but is there anyway easy way to extend it in ruby? How hard would it be to write it in C? Should I just do this outside of redcarpet?
Also I'm intrested in some other extensions of redcarpet that would be shorthand for linking to other models in my app. I'm not sure the syntax yet but I'm guessing it would be similar to how github handles linking to issues.
I found it pretty easy to extend redcarpet's parser in Ruby for my rails 3 app. It wasn't scary at all.
First, start by deriving a class from Redcarpet's HTML renderer and override the preprocess method as recommended in the docs. In Rails 3.2 and Rails 4, this file can go anywhere and you don't need to require it. I use a 'services' folder to hold code like this.
# app/services/my_flavored_markdown.rb
class MyFlavoredMarkdown < Redcarpet::Render::HTML
def preprocess(text)
text
end
end
Next step is to add methods that do text substitutions you want. Here I use regex to wrap text that looks like "#mention" in an HTML span tag with a css class 'mention'.
# app/services/my_flavored_markdown.rb
class MyFlavoredMarkdown < Redcarpet::Render::HTML
def preprocess(text)
wrap_mentions(text)
end
def wrap_mentions(text)
text.gsub! /(^|\s)(#\w+)/ do
"#{$1}<span class='mention'>#{$2}</span>"
end
text
end
end
You could just as easily look up a user's profile page and wrap the #mention in an anchor tag instead. In my case, I also made methods for emoticons and hashtags that worked the same way and chained the methods together.
The last step is to add a helper that accepts some text, creates an instance of your Redcarpet-derived class, passes the text into that for processing, and returns the html result.
# app/helpers/application_helper.rb
def flavored_markdown_to_html(text)
renderer = MyFlavoredMarkdown.new()
# These options might be helpful but are not required
options = {
safe_links_only: true,
no_intra_emphasis: true,
autolink: true
}
Redcarpet::Markdown.new(renderer, options).render(text)
}
In your views you can call it like this:
<%= flavored_markdown_to_html("This is something worth #mentioning") %>
The output would then be:
This is something worth <span class='mention'>#mentioning</span>.
I once tried to extend redcarpet, but found it very difficult. If there are no other dependencies on redcarpet I'd recommend you try rpeg-markdown which is a (somewhat outdated) Ruby gem providing bindings to the excellent peg-markdown.
peg-markdown is a markdown interpreter written as a formal grammar. This means that it is very easy to extend it with own syntax. I've successfully extended peg-markdown for my own projects (see my fork here) and I found it to be much simpler than fiddling with redcarpet's custom parser code.
I also found peg-markdown to have fewer bugs.
The Ruby bindings may have to be made current by updating the git submodule. (I'm planning to submit a pull request to update rpeg-markdown to the latest version of peg-markdown.)

How do I turn off automatic stylesheet/javascript generation on Rails 3.1?

I've got a Rails 3.1 project that I'm working on, but I don't want controller_name.css.sass and controller_name.js.coffee to be generated each time I run rails generate controller controller_name. I could swear I've seen the setting somewhere on the internet, but I can't find it now for the life of me. What is it?
Keep in mind that I'm still wanting to use the Asset Pipeline and the CoffeeScript/Sass integration, but I'm organizing those files in my own way.
I'm pretty sure the answer is a command line argument, but bonus points for turning it off with a generator setting or a hidden file or something.
EDIT: I've found the command line flag for it.
rails generate controller controller_name --assets=false
Or something of the like (that line actually errors out, but it also doesn't generate the assets). The API here shows :assets => true as a default option. How do I change that to false and have it always be false every time I generate a controller?
Add these lines to application.rb:
config.generators.stylesheets = false
config.generators.javascripts = false
New syntax is rails generate controller Resources --no-assets.
Don't forget you can also use g in place of generate. And you can skip the creation of a controller helper using the --no-helper flag.
For just one time, use:
rails generate controller controller_name --no-assets
An update on #Dmitry Maksimov's answer for Rails 4.2. You can disable generation of controller-specific asset files by default with the following in your config/application.rb file (source: the guide):
config.generators do |g|
g.assets false
end
My whole options in the application.rb file:
config.generators do |g|
g.stylesheets = false
g.javascripts = false
g.test_framework :rspec, fixture: false
g.template_engine :haml
g.fixture_replacement :factory_girl, dir: 'spec/factories'
end

Resources