I use Rails 4.2 and i want to refactor my helper method to get rid of duplicate code:
In app/helpers/admin/tasks_helper.rb
def chosen_select(name, method, chzes, selected = nil, options = {}, html_options = {})
options[:value_method] ||= :id
options[:text_method] ||= :name
if options.key?(:placeholder)
html_options['data-placeholder'.intern] = options[:placeholder]
options.delete(:placeholder)
end
if html_options.key?(:class)
html_options[:class] = 'chosen-select ' + html_options[:class]
else
html_options[:class] = 'chosen-select'
end
chzes = options_from_collection_for_select(chzes, options[:value_method], options[:text_method], selected)
options.delete(:value_method)
options.delete(:text_method)
select(name, method, chzes, options.merge!(include_hidden: false), html_options)
end
def chosen_select_array(name, method, chzes, selected = nil, options = {}, html_options = {})
options[:value_method] ||= :id
options[:text_method] ||= :name
if options.key?(:placeholder)
html_options['data-placeholder'.intern] = options[:placeholder]
options.delete(:placeholder)
end
if html_options.key?(:class)
html_options[:class] = 'chosen-select ' + html_options[:class]
else
html_options[:class] = 'chosen-select'
end
chzes = options_for_select(chzes, selected)
options.delete(:value_method)
options.delete(:text_method)
select(name, method, chzes, options.merge!(include_hidden: false), html_options)
end
I my view i have a lot of method calls like app/views/admin/tasks/index.html.erb
<%= chosen_select(:select, :project_id, [TaskFilterOptgroups.active_projects, TaskFilterOptgroups.inactive_projects] , #task_filter_configuration.project_id, {:include_blank => true, :placeholder => 'Project'}, {'data-last-project_id' => #task_filter_configuration.project_id, :style => 'width: 150px;'}) %>
so that i don't want to change my method calls in the view.
My attempt is to make a generic method "chosen_select_generic" that will be called from the specific method like "chosen_select":
def chosen_select_generic(name, method, chzes, selected = nil, options = {}, html_options = {})
options[:value_method] ||= :id
options[:text_method] ||= :name
if options.key?(:placeholder)
html_options['data-placeholder'.intern] = options[:placeholder]
options.delete(:placeholder)
end
if html_options.key?(:class)
html_options[:class] = 'chosen-select ' + html_options[:class]
else
html_options[:class] = 'chosen-select'
end
# 2 different chzes in 2 methods:
# 1) chosen_select(...)
# chzes = options_from_collection_for_select(chzes, options[:value_method], options[:text_method], selected)
# 2) chosen_select_array(...)
# chzes = options_for_select(chzes, selected)
yield chzes
options.delete(:value_method)
options.delete(:text_method)
select(name, method, chzes, options.merge!(include_hidden: false), html_options)
end
and then chosen_select could look like:
def chosen_select(name, method, chzes, selected = nil, options = {}, html_options = {})
chosen_select_generic(name, method, chzes, selected = nil, options = {}, html_options = {}) do |contents|
chzes = option_groups_from_collection_for_select(chzes, :entries, :status, options[:value_method], options[:text_method], selected)
end
end
But this doesn't work. How can i extract the duplicate code in a block without changing the method calls in the view?
This assignment in your block won't do what you think:
chzes = option_groups_from_collection_for_select(...)
It creates a new local variable instead of changing external one. If it's the only changeable piece here, then you can just return it from the block:
chosen_select_generic(name, method, chzes, selected = nil, options = {}, html_options = {}) do |chzes|
option_groups_from_collection_for_select(chzes, :entries, :status, options[:value_method], options[:text_method], selected)
end
And receive the value in your generic method like this:
chzes = yield(chzes)
Related
In a rails 4 app, I'm trying to pass a default option to the text_field helper, but seem to be stuck on how to implement this.
So far, I have in my view:
<%= new_text_field :name, class: "", placeholder: "" %>
and in my application_helper.rb
def new_text_field(object_name, method, options = {})
text_field(object_name, method, options = {}) # Trying to pass in a default class here, for example ".bigger"
end
Try this:
def new_text_field(object_name, method = nil, options = {})
options[:class] ||= 'bigger' # this will set bigger as default value if "class" option isn't passed
text_field(object_name, method, options = {})
end
Something like this should work:
def new_text_field_tag(name, value=nil, options)
your_class = "bigger"
if options.has_key?(:class)
options[:class] += " #{your_class}"
else
options[:class] = your_class
end
text_field_tag(name, value, options)
end
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])
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
I'm developing an application that displays tabular data in many different areas and I find myself constantly using the same HTML table structure over and over. For example a particular table looks like this:
%table.zebra-striped#user-table{ :cellspacing => "0" }
%colgroup
%col{:id => "email"}
%col{:id => "username"}
%col{:id => "sign-in-count"}
%col{:id => "last-sign-in-at"}
%thead
%tr
%th{:id => "email-head", :scope => "col"} E-mail
%th{:id => "username-head", :scope => "col"} Username
%th{:id => "sign-in-count-head", :scope => "col"} Sign Ins
%th{:id => "last-sign-in-at-head", :scope => "col"} Last Sign In
%tbody
- #users.each do |user|
%tr{ :class => zebra }
%td
=h user.email
%td
=h user.username
%td
=h user.sign_in_count
%td
=h user.last_sign_in_at
Ideally, I would like to create some kind of helper method where I could do something like:
= custom_table_for #users do
= column :email
= column :username do |user|
= link_to user.username, user_path(user)
= column "Sign Ins", :sign_in_count
= column :last_sign_in_at
This way I can change the formatting of the data in the columns and the column header names if I'm not happy with default values, but have the table generated for me.
I suppose I could create a normal helper, but I'd have to use arrays and I have no idea how I could include custom data formatting per column.
active_admin has something similar to this which you can see here: http://activeadmin.info/docs/3-index-pages/index-as-table.html
Any leads or ideas would be greatly appreciated.
I just came up with this:
A few points:
The line #columns = [] is a reset so you can call it more than once.
The yield in the custom_table_for calls the block that you pass it.
The block in the column method is stored and called in custom_table_for if it is set.
I included a sample class to show the usage too.
please note I did this outside of a rails app and you almost certainly want to use http://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag instead of the p "<table>" this is merely for sample purposes when you run it in the console.
module TableHelper
def custom_table_for(items)
#columns = []
yield
p "<table>"
#columns.each do |c|
p "<th>#{c[:value]}</th>"
end
items.each do |e|
p "<tr>"
#columns.each do |c|
e[c[:name]] = c[:block].call(e[c[:name]]) if c[:block]
p "<td>#{e[c[:name]]}</td>"
end
p "</tr>"
end
p "</table>"
end
def column(name, value = nil, &block)
value = name unless value
#columns << {:name => name, :value => value, :block => block}
end
end
class ExampleTable
include TableHelper
def test
#users = [{:email => "Email 1", :username => "Test User"}, {:email => "Email 2", :username => "Test User 2"}]
custom_table_for #users do
column :email, "Email"
column :username do |user|
user.upcase
end
end
end
end
et = ExampleTable.new
et.test
UPDATE
I migrated this to rails to use content_tags
module TableHelper
def custom_table_for(items)
#columns = []
yield
content_tag :table do
thead + tbody(items)
end
end
def thead
content_tag :thead do
content_tag :tr do
#columns.each do |c|
concat(content_tag(:th, c[:value]))
end
end
end
end
def tbody(items)
content_tag :tbody do
items.each { |e|
concat(content_tag(:tr){
#columns.each { |c|
e[c[:name]] = c[:block].call(e[c[:name]]) if c[:block]
concat(content_tag(:td, e[c[:name]]))
}
})
}
end
end
def column(name, value = nil, &block)
value = name unless value
#columns << {:name => name, :value => value, :block => block}
end
end
To compliment #gazler's response, here's a way to make a table of a single resource-- column one for attribute names, column two for their values:
module TableHelper
#resource = nil
def simple_table_for(resource)
#resource = resource
content_tag :table do
content_tag :tbody do
yield
end
end
end
def row(key, label = nil, &block)
if key.is_a? String
label = key
end
content_tag(:tr) {
concat content_tag :td, label || key.capitalize
concat content_tag(:td ){
if block_given?
yield
else
#resource.send(key)
end
}
}
end
end
I am rewriting the controller render method, however, I want to use the old method when in the render_to_string method. These are my current codes:
def render_with_xhr(options = {}, extra_options = {}, xhr_check = true, &block)
if xhr_check && request.xhr?
template = render_to_string(options)
render_without_xhr(:update) {|page| page.replace_html("#popup .dialog", template)}
else
render_without_xhr(options, extra_options, &block)
end
end
alias_method_chain :render, :xhr
What happens is that since render_to_string makes use of render (presumably), I end up in an infinite loop. How can I make it fall back to the old method just for that line my new render method?
I tweaked the codes from the accepted answer, final code is below:
def render_to_string(options = {}, &block)
render(options, {}, false, &block)
ensure
response.content_type = nil
erase_render_results
reset_variables_added_to_assigns
end
def render_with_xhr(options = nil, extra_options = {}, xhr_check = true, &block)
if xhr_check && request.xhr?
template = render_to_string(options)
render_without_xhr :update do |page|
page.replace_html("#popup .dialog", template)
end
else
render_without_xhr(options, extra_options, &block)
end
end
alias_method_chain :render, :xhr
you could at line 2 pass some unique value to options hash, and then detect it in your code and remove
def render_with_xhr(options = {}, extra_options = {}, xhr_check = true, &block)
if xhr_check && request.xhr? && !options.delete(:bacon)
template = render_to_string(options.merge(:bacon => true))
render_without_xhr(:update) {|page| page.replace_html("#popup .dialog", template)}
else
render_without_xhr(options, extra_options, &block)
end
end
alias_method_chain :render, :xhr
like that :)