Front End Development javascript tips & snippets: caching cdn Google javascript jquery page load speed
by bseanvt
leave a comment
Link to jQuery Source from Google’s CDN
https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js
That is the link to the jQuery source hosted by Google on their CDN. It’s probably already cached on client machines so it should be as fast as is possible! You can read more/use other Javascript libs from Google’s CDN here: http://code.google.com/apis/libraries/devguide.html#jquery
Ruby on Rails: bookmarks buffer collection content_tag excerpt helper methods loop Rails visual markup
by bseanvt
leave a comment
Simple String Concatenation of a Collection Written as a Helper for Rails
At Railsconf last week I took Greg Pollack’s online course Rails Best Practices. The interface is gorgeous and the instructions are excellent. One of the lessons involved taking a partial and moving it into a helper. I was reminded how difficult such a simple task can be. I have written about this before Yield a Block Within Rails Helper Method with Multiple Content Tags. However, that post aims to solve a slightly different problem, where the helper method takes the captured text from a block passed as an argument which essentially acts as a wrapper.
The difficulty isn’t with the logic itself and or the complexity/verbosity that the code is likely to produce. Rather, it is difficult because you have to endlessly concatenate strings and this something somewhat uncommon when programming with ruby. We have to remember that we’re working with a buffer of text about to be flushed. Here is a simple code snippet that shows how to write a helper method that loops over a collection of objects.
def bookmarks_for(user=nil)
content_tag(:div, :id => "bookmarks") do
user.bookmarks.each do |bookmark|
concat(
content_tag(:strong) { bookmark.member_name } +
excerpt(bookmark.body, '', 100)
)
end.join
end
end
Notice the use of the concat() method the “+” sugb and .join() method. All three of which bring these statements together into one final piece of html called in the view.
<%= bookmarks_for(@user) %>
bash environment Linux: bash configuration development environment named pipe pipes tmux
by bseanvt
2 comments
How to Copy and Paste to/from the Global Register with Tmux on Mac OS X
Using the system clipboard with tmux on OS X is broken. I really like tmux but having copy and paste is kind of important for me. Here is my attempt to get it back with a simple bash script until I find something better. The approach is to take a named pipe and feed it the contents of “tmux showb”. A bash script will manage the pipe and because this script is initialized from a normal session it will write to the system clipboard just fine.
In my .bash_profile…
pipe4tmux=/tmp/pipe4tmux alias tcp="tmux showb > $pipe4tmux" if [[ ! -p $pipe4tmux ]]; then ~/pipe4tmux.sh & fi
And in pipe4tmux.sh…
#!/bin/bash pipe4tmux=/tmp/pipe4tmux echo "Starting named pipe $pipe4tmux" trap "rm -f $pipe4tmux" EXIT if [[ ! -p $pipe4tmux ]]; then mkfifo $pipe4tmux fi while true do pbcopy < $pipe4tmux done echo "Quitting pipe4tmux $pip4tmux"
Ruby on Rails: active record models named scopes scopes
by bseanvt
leave a comment
Reusing Scopes (Formerly Named_scope) In Rails 3
You can easily chain scopes together in your models.
class Article < ActiveRecord::Base
scope :ordered, order('position ASC')
scope :published, ordered.where('published = ?', true)
scope :for_homepage, published.limit(3)
end
Article.for_homepage.to_sql
# => SELECT \"articles\".* FROM \"articles\" WHERE (published = 't') ORDER BY position LIMIT 3


