How Do I Pass A Block Through To A Method Call? - ruby-on-rails

I need a helper that generates a link wrapped in a <li> including an active class.
Without supporting blocks this is easy:
def nav_item(*args, &block)
url = args[1]
clazz = 'active' if current_page?(url)
content_tag(:li, :class => clazz) do
link_to(*args)
end
end
But like link_to I want my helper to support blocks for defining content. With link_to I can do:
So how do I support the same in my helper?
All I need to do is pass the block through to link_to. My current attempt
def nav_item(*args, &block)
url = if block_given?
args.first
else
args[1]
end
clazz = 'active' if current_page?(url)
content_tag(:li, :class => clazz) do
if block_given?
# What goes here?
else
link_to(*args)
end
end
end

You can just pass the block to link_to as the last arg. Like this:
def nav_item(*args, &block)
url = if block_given?
args.first
else
args[1]
end
clazz = 'active' if current_page?(url)
content_tag(:li, :class => clazz) do
if block
link_to(*args, &block)
else
link_to(*args)
end
end
end

Related

How to present list of names in Presenter?

I have my custom presenter
class ShiftPresenter
def initialize(shift, template)
#shift = shift
#template = template
end
def h
#template
end
def users_list
logs = ShiftLog.by_shift(#shift)
names = logs.map do |log|
log.cardiologist.name
end
h.content_tag :div, names unless names.empty?
end
end
and #index view
- present shift do |shift_presenter|
= shift_presenter.user_list
How to present users names using li instead of ['tom', 'jerry']
You can add this to your presenter method:
def users_list
logs = ShiftLog.by_shift(#shift)
names = logs.map(&:cardiologist).map(&:name)#.compact.uniq # you can add this if you want
h.content_tag :div do
h.content_tag :ul do
ul_content = ''.html_safe
names.each do |name|
ul_content << h.content_tag :li, name
end
ul_content
end
end
The thing is it works as block with the return statement: the last used/returned object will be put inside the content_tag.
Try to wrap each element of names in users_list method into <li> tag and join them in a string. To do this you need to change this line:
h.content_tag :div, names unless names.empty?
into this:
h.content_tag :div, names.map{|str| '<li>' + str + '</li>'}.join unless names.empty?

Rails routing - how to add scope param to url_for helper?

I have resource bio and in views and link for add new bio is:
= link_to "Add new bio", [:new, :admin, :bio]
If I put resource :bio to scope like this:
namespace :admin do
scope "/:bio_type", :defaults => {:bio_type => "company"} do
resources :bios
end
end
This doesn't work
= link_to "Add new bio", [:new, :admin, :bio, { bio_type: params[:bio_type] }]
My question is how can I add scoped param to url_for helper? And can rails do this by default?
p.s. new_admin_bio_path({bio_type: params[:bio_type]}) works fine, but it's just curious
I believe you cannot make this with array params to link_to. You have to use polymorphic_path or new_admin_bio_path({bio_type: params[:bio_type]})
The reason is that link_to calls url_for with [:new, :admin, :bio, { bio_type: params[:bio_type] }], which calls polymorphic_path with these params.
Check the source code for url_for and for polymorphic_url.
Notice, that polymorphic_url takes 2 params - record_or_hash_or_array and options, but url_for calls it with one parameter only.
def url_for(options = {})
options ||= {}
case options
when String
options
when Hash
options = options.symbolize_keys.reverse_merge!(:only_path => options[:host].nil?)
super
when :back
controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
else
polymorphic_path(options)
end
end
def polymorphic_path(record_or_hash_or_array, options = {})
polymorphic_url(record_or_hash_or_array, options.merge(:routing_type => :path))
end
def polymorphic_url(record_or_hash_or_array, options = {})
if record_or_hash_or_array.kind_of?(Array)
record_or_hash_or_array = record_or_hash_or_array.compact
if record_or_hash_or_array.first.is_a?(ActionDispatch::Routing::RoutesProxy)
proxy = record_or_hash_or_array.shift
end
record_or_hash_or_array = record_or_hash_or_array[0] if record_or_hash_or_array.size == 1
end
record = extract_record(record_or_hash_or_array)
record = convert_to_model(record)
args = Array === record_or_hash_or_array ?
record_or_hash_or_array.dup :
[ record_or_hash_or_array ]
inflection = if options[:action] && options[:action].to_s == "new"
args.pop
:singular
elsif (record.respond_to?(:persisted?) && !record.persisted?)
args.pop
:plural
elsif record.is_a?(Class)
args.pop
:plural
else
:singular
end
args.delete_if {|arg| arg.is_a?(Symbol) || arg.is_a?(String)}
named_route = build_named_route_call(record_or_hash_or_array, inflection, options)
url_options = options.except(:action, :routing_type)
unless url_options.empty?
args.last.kind_of?(Hash) ? args.last.merge!(url_options) : args << url_options
end
args.collect! { |a| convert_to_model(a) }
(proxy || self).send(named_route, *args)
end
So, correct call with the scope option should sound like
polymorphic_path([:new, :admin, :bio], bio_type: params[:bio_type])

Rails 3 - Custom link_to helper (with default class and ability to add classes)

I'm trying to hook up a custom helper that has a default class 'pjax' but also retains an ability to add classes where need be.
Example:
link_to_pjax('pagename', page_path, :class => 'current')
So the helper would add the 'pjax' by default, and also the class 'current', or whatever is passed in.
def link_to_pjax(name, path, options = {:class => 'pjax'})
link_to(name, path, options)
end
The syntax is freaking me out. Any advice would be much appreciated.
def link_to_pjax(name, path, options)
options[:class] += ' pjax'
link_to(name, path, options)
end
edit
After test, it's much less elegant:
def link_to_pjax(name, path, options = {})
options[:class] ? options[:class] += ' pjax' : options[:class] = 'pjax'
link_to(name, path, options)
end
My first solution works but only if you have still specified a class.
The latest works in all cases:
link_to_pjax 'click me', my_super_path, class: 'ahah', id: 'hello'
link_to_pjax 'click me', my_super_path
etc
My bad...
def link_to_pjax(name, path, options={})
default_options = { :class => "pjax" }
link_to(name, path, options.merge(default_options))
end
I improved Delba answer to handle block version of link_to:
def link_to_pjax(*args, &block)
if block_given?
options = args.first || {}
html_options = args.second
link_to_pjax(capture(&block), options, html_options)
else
name = args[0]
options = args[1] || {}
html_options = args[2] || {}
html_options[:class] ? html_options[:class] += ' pjax' : html_options[:class] = 'pjax'
link_to(name, options, html_options)
end
end

block methods in ruby

I am using a block method to print a list, but it is generating error.
class MyDataListBuilder
attr_accessor :object
def initialize(object)
#object = object
end
def column (&block)
content_tag :li, block.call
end
end
and using it as
<%= my_data_list_for #leads, [" :10", "Age:30", "Contact:140", "Phone:140", "Email:180", "Company:100", ""] do |l| %>
<%= l.column do %>
<%= object.age %>
<% end %>
<% end %>
other methods are
def list_headers(args=[])
args = Array.new(args)
columns = []
args.map { |o| columns << content_tag(:li, o.split(":").first, :style=>"width:#{o.split(":").second}px;") }
content_tag(:ul, columns.join(" ").html_safe, :class=>"list-headers")
end
def my_data_list_for(object, headers=[], &block)
arr = []
object.each do |o|
arr = capture(DataListHelper::MyDataListBuilder.new(o), &block)
end
content_tag(:ol, list_headers(headers) + arr, :class=>"data-list")
end
it is generating an error and i can not figure out why:
ActionView::Template::Error (undefined local variable or method `object' for #<#<Class:0xcaa1ca0>:0xca9ebf4>):
Please help me in it.
This solves the issue.
class MyDataListBuilder
include ActionView::Helpers::TagHelper
include ActionView::Helpers::CaptureHelper
attr_accessor :object, :output_buffer
def initialize(object)
#object = object
#output_buffer = nil
end
def column (&block)
if block_given?
content_tag(:li, capture(self, &block))
else
content_tag(:li, "")
end
end
end

How do I make link_to open external URLs in a new window?

I need to convert a rails 2.3 site so that all external URLs open in a new window. I could go though every call to link_to and add :target => '_blank', but I'd like to do it in one step for all links, present and future. Is there a way I can monkey patch link_to to get the desired behaviour?
You should not have to change your server-side code for this view problem.
You should use Unobscursive javascript.
This example will only make external links showing up in a new window :
// jQuery
//
$(document).ready(function() {
$("a").click(function() {
link_host = this.href.split("/")[2];
document_host = document.location.href.split("/")[2];
if (link_host != document_host) {
window.open(this.href);
return false;
}
});
});
In the end I went with this, in an initialiser:
module ExternalLinksInNewTabs
def new_tab_link_to *args, &block
if block_given?
options = args.first || {}
html_options = args[1] || {}
if options.is_a? String
if ExternalLinksInNewTabs.is_external_link? #controller.request.host, options
html_options[:target] = '_BLANK'
end
end
same_tab_link_to options, html_options, &block
else
name = args.first
options = args[1] || {}
html_options = args[2] || {}
if options.is_a? String
if ExternalLinksInNewTabs.is_external_link? #controller.request.host, options
html_options[:target] = '_BLANK'
end
end
same_tab_link_to name, options, html_options
end
end
def self.is_external_link? host, url
host.sub! /^www\./, ''
url =~ /^http/i && url !~ /^http:\/\/(www\.)?#{host}/i
end
end
module ActionView
module Helpers
module UrlHelper
include ExternalLinksInNewTabs
alias_method :same_tab_link_to, :link_to
alias_method :link_to, :new_tab_link_to
end
end
end
You just add an helper to add this options in your link_to
If you want add it on each link_to to can add on ApplicationHelper
def link_to(*args, &block)
if block_given?
args = [(args.first || {}), (args.second || {}).merge(:target => '_blank')]
else
args = [(args.first || {}), (args.second || {}), (args.third || {}).merge(:target => '_blank')]
end
super(args, block)
end
Or you can create your own link_to helper
def link_to_blank(*args, &block)
if block_given?
args = [(args.first || {}), (args.second || {}).merge(:target => '_blank')]
else
args = [(args.first || {}), (args.second || {}), (args.third || {}).merge(:target => '_blank')]
end
link_to(args, block)
end
In rails 3.2+, it has been added as an option, just add
= link_to 'facebook', 'http://www.facebook.com/fb-page', target: '_blank'
and it'll open the link in a new tab.

Resources