Addling Line Break in Vim with Substitution - regex

There is a really helpful article on Vim's wiki here that is nearly exactly what I want to do, I think I'm just missing something small.
I would like to take this line:
<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
and make it into:
<%= simple_form_for(resource,
as: resource_name,
url: session_path(resource_name)
) do |f| %>
However when I run:
/[(,)]
:s//\r&/g
I get:
<%= simple_form_for
(resource
, as: resource_name
, url: session_path
(resource_name
)
) do |f| %>
I need the linebreaks to happen AFTER the commmas and I'm unsure the regex to provide to make that happen. Thoughts?

Here is how you would replace every comma by a comma followed by a newline.
:s/,/&\r/g
If you also want to separate every pair of two )) by a newline, you can do this.
%s/))/)\r)/g

Thanks #FDinoff ! I figured it out, it was simply rearranging my search & replace with:
:s//&\r/g

Related

What is the shortest way of getting this?

What is the shortest/right/nice way to get this? Where f is the form alias for Customer model with customer_contract_id and belong_to CustomerContact model.
<%= f.object.customer_contact.blank? ? '':f.object.customer_contact.name %>
this is not working
<%= f.object.customer_contact.name ||= '' %>
I apologize upfront for any confusion/frustration. I appreciate any help that I can get!
Can you try it like this?
<%= f.object.customer_contact.present? ? f.object.customer_contact.name : '' %>
Please note the space between : and the two outputs. And .present? is the reverse of .blank?
For future references, I think this table would be of some help to you.
Credits for this table should go to Sibevin Wang
Hope this helps?

url_for in an email sent by a rake task in rails 4

I have a rake task that sends out daily digest emails of player activity during a day. (See example code below.) If I run PlayerActivityMailer.activity_report.deliver in my console, everything works just fine. However, when I try to invoke the rake task, I get the following error:
rake aborted!
ActionView::Template::Error: arguments passed to url_for can't be handled.
Please require routes or provide your own implementation
After doing some research, I found that in Rails 4, they totally nerfed ActionView::Helpers::UrlHelper.url_for (http://apidock.com/rails/v4.1.8/ActionView/Helpers/UrlHelper/url_for - notice the giant red minus sign under the 4.0.2). If you look at the source, you can see the error I'm seeing - it no longer takes options. As far as I can tell, that functionality still exists in other url_fors, such as the one in ActionDispatch::Routing::UrlFor. Also, the error message suggests including Rails.application.routes.url_helpers.
What I've tried
include ActionDispatch::Routing::UrlFor in both the rake task (inside the task) and the mailer (both at the same time, and each separately)
include Rails.application.routes.url_helpers in the same places and configurations, both with and without the UrlFor include.
The error still persists. My guess is that the page view is still insisting on using the ActionView::Helpers::UrlHelper version of url_for. I don't think I can include things actually in the views (which is sloppy looking and hacky even if I could).
Example Code
(heavily sanitized)
config/environtments/development.rb:
config.action_mailer.default_url_options = { host: 'localhost:3000' }
lib/tasks/player.rake:
namespace :player do
task :activity => :environment do
PlayerActivityMailer.activity_report.deliver
end
end
app/mailers/player_activity_mailer.rb:
class PlayerActivityMailer < ActionMailer::Base
def activity_report
#activities = PlayerActivity.all
mail(to: 'foo#bar.com', subject: 'activity report')
end
end
app/views/player_activity_mailer/activity_report.html.erb:
<% #activites.each do |activity| %>
Player: <%= link_to activity.player.name, player_url(id: activity.player.id) %>
...
<% end %>
I also have a model Player, resources :players in my routes.rb file, and a PlayerActivity class with an association to Player.
I'm currently using the (really horrifying) workaround of #base_url = Rails.configuration.action_mailer.default_url_options[:host] in my mailer action and "http://#{#base_url}/players/#{activity.player.id}" in my view instead of the player_url part.
Help!
Have you tried passing just your player in the URL? Like this:
<% #activites.each do |activity| %>
Player: <%= link_to activity.player.name, player_url(activity.player) %>
<% end %>

Rails, filterrific with multiple parameters

I'm using the most recent ruby + rails, with the filterrific gem. It works great - but how can multiple parameters per scope be used? For single column filtering it is simple, but for the following scenario, how would it be handled?
Filter by X miles of zipcode Y
Scopes currently only have knowledge of the value being modified (EITHER miles, OR zipcode - two different scopes), but neither have knowledge of the other. This filter scenario requires knowledge of miles and zipcode. I have searched the documentation thoroughly and see no way. Has anyone done this before or am I missing something hidden in the documentation?
You can use Rails' fields_for form helper to submit a hash with multiple keys and values to a single filterrific enabled scope:
<%= f.fields_for :with_distance do |with_distance_fields| %>
<%= with_distance_fields.text_field :max_distance %>
<%= with_distance_fields.text_field :city %>
<% end %>
Then you can access the various values in the scope like so:
scope :with_distance, lambda { |distance_attrs|
# `distance_attrs` is a hash with two keys:
# {
# :max_distance => '10',
# :city => 'Vancouver',
# }
where(...)
}
This is tested with Filterrific 1.4.2 and Rails 4.1

Need help passing dynamic list of files to partial. Can't get list to pass successfully in Rails

In my application/index, the user selects a location and directory path from a drop down list. onchange event makes a call to the controller called file_dir where it takes the path, executes a command line call passing back a list of files in that directory to a parameter #files. I then render a partial passing this #files to a local variable. Then in the partial the select tag will display with the list of file passed to it.
I am new to ROR and have not been able to successfully pass a readable local variable. to the partial. Everything works fine with a hardcoded line but not with the local variable.
Can someone please help advise me on the correct way to set this up?
Here is the method called from the first onchange dropdown pass that receives the directory:
def file_dir
unless params[:dir_list].nil?
#dir_path_choice = params[:dir_list]
else
#dir_path_choice = '/watchfolder/indemandvod'
end
# #files = "#{#dir}"
#files = Dir.glob("#{#dir_path_choice}/**/*.{mpg,mov}").map
if #files.nil?
#files = Dir.glob("/watchfolder/hbovod/**/*.{mpg,mov}").map
end
render :partial => 'list_files', :locals => { #list => #files }
end
In irb I tested the #files = Dir.glob("#{#dir_path_choice}/**/*.{mpg,mov}").map line to make sure this was processing correctly. Here is what #files looks like:
1.9.3-p547 :008 > #files
=> #<Enumerator: ["/watchfolder/indemandvod/MJR-TEST.mov", "/watchfolder/indemandvod/MJR-TEST-AWS1.mov", "/watchfolder/indemandvod/MJR-TEST-AWS.mov", "/watchfolder/indemandvod/MJR-TEST2.mov", "/watchfolder/indemandvod/PIX_Gor_SVO40185/PIX_Gor_SVO40185_mezz.mov"]:map>
This data breaks out in the select list, separated by the commas.
NOTE: I did try to pass the #list without the # symbol but I got unidentified local variable or method and couldn't get the code to run with out adding the #. This is like a collection of data so I expect it is interpreting it as an array. Not sure though.
Here is the partial file code:
<p>
<label>Select Partial Test File List:</label><%= #list %><br />
<% unless #list.nil? %>
<%= #list %>
<% else %>
<label> list is empty. </label>
<% #list = Dir.glob("/watchfolder/showtimevod/**/*.{mpg,mov}").map %>
<% end %>
<%= select_tag 'filepath', options_for_select(#list, #selected_filepath) %>
</p>
It does display back on the page and my 'list is empty' always shows and the dropdown box populates with my default 'hardcoded' command line for '/watchfolder/showtimevod/' files list.
The #selected_filepath maintains the selected line in the list.
I don't know what I am doing wrong in passing values to partials.
In my application/index,
What is an "application/index"?
Can someone please help advise me on the correct way to set this up?
The correct way is to get something simple to work first:
app/controllers/some_controller.rb
def file_dir
puts "****#{params[:dir_list]}"
#files = %w[ a b c]
render :partial => 'list_files', :locals => { file_names => #files }
end
views/some_controller/_list_files.htm.erb:
<div>
<%= file_names.each do |fname| %>
<div><%= fname %></div>
<% end %>
</div>
And as with all your posts, in this line:
<%= select_tag(
'filepath',
options_for_select(#list, #selected_filepath) %>
#selected_filepath is undefined.
This is like a collection of data so I expect it is interpreting it as
an array. Not sure though.
You should probably strive to create data that you understand. What are you trying to accomplish with this line:
#files = Dir.glob("#{#dir_path_choice}/**/*.{mpg,mov}").map
that the following line doesn't do:
#files = Dir.glob("#{#dir_path_choice}/**/*.{mpg,mov}")
This:
I did try to pass the #list without the # symbol but I got
unidentified local variable or method and couldn't get the code to run
with out adding the #.
is not sufficient. No one cares what your interpretation of the error message is--what people care about is the EXACT error message, with file names and line numbers, copy and pasted into your post, along with the exact code the error refers to.
If you just want to get your code to work, you can do this:
app/some_controller.rb:
def file_dir
if params[:dir_list].nil?
#dir_path_choice = '/watchfolder/indemandvod'
else
#dir_path_choice = params[:dir_list]
end
#files = Dir.glob("#{#dir_path_choice}/**/*.{mpg,mov}").map
if #files.nil?
#files = Dir.glob("/watchfolder/hbovod/**/*.{mpg,mov}").map
end
render :partial => 'list_files', :locals => {file_names => #files }
end
However, adding map() to the end of your Dir.glob() does nothing useful.
app/views/some_controller/_list_files.html.erb:
<p>
<label>Select Partial Test File List:</label>
<%= select_tag 'filepath', options_for_select(file_names) %>
</p>

f.input collect: is showing duplicates in the select

I have the following simple_form input:
<%= f.input :user_id, collection: [options_for_select(User.all.map{ |u| [u.firstname, u.id]})] %>
There are 3 users in my local database. When I use the select in the form it shows the users twice like:
Tony
Johnny
Bill
Tony
Johnny
Bill
I'm not a pro with the map syntax above, so it may have to do with that.
I found this as a much better way to code the select. Also, the one I was trying above doesn't work on Heroku. This is what I used to make it work:
true) %>