Visualizzazione post con etichetta rails. Mostra tutti i post
Visualizzazione post con etichetta rails. Mostra tutti i post

[Rails] find not used partial ruby script

find_partial_not_used.rb
 

def string_between_markers string, marker1, marker2
  string[/#{Regexp.escape(marker1)}(.*?)#{Regexp.escape(marker2)}/m, 1]
end

all_erb_files=`find . -name '_*.erb'`.split("\n")
short_names = all_erb_files.collect {|f|
  string_between_markers f, "/_", "."
}.uniq
short_names.each {|s|
  how_many_times_is_called=`find . -name *.erb | xargs grep render | grep #{s} | wc -l`
  puts "partial #{s} not rendered in any file" if how_many_times_is_called.to_i == 0
}

[Rails] Html to plain text


I need to extract some text from a model in order to publish it in a CSV file. My problem was that one field's content is in pure html, like:

& quot; name & quot;=& gt;
[& quot;TommasoTorti& quot;, & quot;Tommaso& quot;]
; but in my csv file i need the text in a plain format.
One way is to unescape the html in the model:


def to_csv
[... , CGI.unescapeHTML(self.field) ].join("\t")
end


and insert the 'raw' command inside the view:

<%= raw my_model.to_csv %>


(The controller's code:


@models = MyModel.all
response.headers["Content-Type"] = "text/csv; charset=UTF-8; header=present"
response.headers["Content-Disposition"] = "attachment; filename=my_models.csv"
render :layout => false

)

[JQuery] highlight event detail with jquery


My html page has multiple events inside a div identified by a prefix and the event's id.

Look at this example:




< id="event_1">...< /div >
< id="event_2">...< /div >...

I want to highlight the selected div when the user click on the link inside it and, in the meantime, deselect all the others divs.
My css:
.time_line_event_highlighted {
background-color:yellow;
}

Here is the jquery snippet:

$("#" + divId).click(function(){ // when a user click on a link inside a div...
$('div[id^=event]').removeClass("time_line_event_highlighted");
$("#event" + id).addClass("time_line_event_highlighted")
...
});

[Rails] how to tail log from multiple servers

A different approach from the one described here: http://serverfault.com/questions/112457/tail-multiple-remote-files
is to use a capistrano task like (in my deploy.rb ) ,

task :tail_logs, :roles => :your_server_tag do

cap production deploy:tail_logs
run "tail -f #{current_path}/log/production.log" do |channel, stream, data|
puts # for an extra line break before the host name
puts "#{channel[:host]}: #{data}"
break if stream == :err
end
end

[Rails] from html to pdf


I'm trying to use the nice library PDFKit, used for generate Pdf given an html in a rails application.

An interesting strategy for retrieving html generated by rails in order to put it directly in your pdf is shown in the following example:












def export_pdf

html = render :layout => false

kit = PDFKit.new(html)

kit.stylesheets << RAILS_ROOT + '/public/stylesheets/your_css.css'

send_data(kit.to_pdf, :filename => "your_pdf_name.pdf", :type => 'application/pdf')


end

[Rails] paginate with will_paginate plugin


I'm using the will_paginate plugin for rails.
Unluckily, my query aren't in the form of:

MyModel.find(:all..)

in this case the main change is simply replacing 'find' with 'paginate' as described in the site.
I have a collection of MyModel object retrieved in a lot of different ways.
My solution, based on what i can see here , is calling a method (outside the controller class):

def self.paginate(my_collection, params)
current_page =
if params[:page].nil?
1
else
params[:page].to_i
end
per_page = 10
page_results = WillPaginate::Collection.create(current_page, per_page, my_collection.size) do |pager|
start = (current_page-1)*per_page
pager.replace(my_collection[start, per_page])
end
page_results
end

and, in the view:

<%= will_paginate @page_results, :params => params %>