Thursday, April 3, 2014

Adding will_paginate to a rails application

There are times when you have a lot of records to display and want to provide an easy way to navigate through the entire unfiltered list. Various paginate gems offer a solution. The one used most in my shop is will_paginate.

To add it to an application, the first obvious step is to install it if not already installed.
gem install will_paginate

Once available, add it to the config/environment.rb:
config.gem 'will_paginate', :version => '~> 2.3.16' (adjust for your version)

If you are using Bundler, add it to the Gemfile.

Next, add the paginate method to your controller result set for all records to be paginated:
@records = Record.find(:all)
@records = @records.paginate(:page => params[:page], :per_page => 30)

Finally, add code to your view to display the navigation. I sometimes add it to both top and bottom, depending on how many records I am displaying.
<%= will_paginate @records, :style => 'color:blue' %>

Friday, December 6, 2013

Mysql commands

Login to MySQL

mysql --user=xxx --password=xxx --database=xxx

Dump a database

mysqldump --user=xxx --password=xxx dbname

Dump a single table with data

mysqldump --user=xxx --password=xxx dbname tablename

Dump a single table with only some of the data using --where

mysqldump --user=xxx --password=xxx dbname tablename --where="db='blog3'"

Dump a single table with data in tab separated format (for import to XL)

mysqldump --user=xxx --password=xxx dbname tablename -T path
The -T path (example: -T /tmp) tells mysqldump where to create a tablename.sql file with the table definition, and a tablename.txt file with the tab separated data from the table.

Dump a single table structure without the data

mysqldump --user=xxx --password=xxx --no-data dbname tablename

Grant all privileges on a database to a user, be sure to flush privileges after to make the security change effective immediately

GRANT ALL ON db1.* TO 'username'@'localhost';
FLUSH PRIVILEGES;

Tuesday, August 20, 2013

Getting Rails variables into external JavaScript files

It is often convenient to keep JavaScript code in external .js files and pull them into a web page using this HTML command:

<script src="external.js"></script>

In a normal rails .erb or .haml view, you can't use a rails variable value inside the external JavaScript file. The trick is to set the value in an HTML meta tag, then use JavaScript to read the meta tag after the DOM is ready. For example, in the .erb view file, use:

<meta name='railsvariable' content='<%= @railsvar %>' /%>

Then, in the JavaScript file, using the Dojo query function:

var railsvar = query('meta[name="railsvariable"]')[0].content;

In raw JavaScript, it would be this:

var railsvar = document.getElementsByTagName('meta').item(property='railsvariable').getAttribute('content');

Tuesday, May 7, 2013

Haml comments

I've had to look this up about three times which means I should add it to the blog.

This technique was found at Makandra Cards.

To comment out code such that haml does not process it (it generates no HTML):

-# = link_to 'foo'

Wednesday, December 5, 2012

Ruby one liners

One liner to edit a file in place

This command edits a file in place, performing a global text search/replace. The -p switch tells ruby to place your code in the loop while gets; ...; print; end. The -i tells ruby to edit files in place, and -e indicates that a one line program follows.

ruby -p -i -e '$_.gsub!(/248/,"949")' file.txt

One liner to remove DOS line endings from a file

A Unix or Mac text file uses a single line feed character to mark the end of a line (hex 0A). Files created on DOS or Windows use two characters, carriage return and line feed (hex 0D 0A). This command edits a file in place, removing the carriage returns.

ruby -p -i -e '$_.gsub!(/\x0D/,"")' file.txt

One liner to edit files in place and backup each one to *.old

ruby -p -i.old -e '$_.gsub!(/248/,"949")' *.txt

Saturday, November 3, 2012

Finding files owned by a user or group

To find all files from the current directory owned by user "john":

find . -user john -print

To find all files from the current directory owned by group "john":

find . -group john -print

You can also use numeric IDs (-uid or -gid) instead. The find command searches hidden files and directories automatically (those with names that start with a period).

Saturday, August 11, 2012

Finding the size of directories and subdirectories

I can't take credit for this sequence of commands. I found several variations through search engines. I apologize for not linking to the site(s) where I found it. I can't find them. However, it is useful enough to document for posterity.

ls -AF | grep \/ | sed 's/\ /\\\ /g' | xargs du -sh