Wednesday, October 31, 2012

Nice brief git log format

I'm really happy with this. There's two new features compared to what I've been using up until recently.

Firstly, I just learned about the core.pager option which solves the problems I was solving previously with this ugliness:
ll = "!git --no-pager log --pretty=nice -n30; echo"

Secondly, the %+d. I generally don't use the --graph log formats, (such as these), that show you the branch paths, but seeing where the refs are is super useful. The + makes it appear on the next line without adding a line break when there are no refs.
[core]
# -E means don't page if less one screen
# -F means quit when EOF reached
# -r fixes the %C(yellow)%+d colouring somehow
pager = less -E -F -r
[alias]
ll = log --pretty=nice -n 30 # about one screen worth
lll = log --pretty=nice # goes back forever
[pretty]
# %h is short commit hash
# %cr is time ago in words
# %cn is committer name
# %s is the commit message title
# %+d is a line break and a list of refs
nice = format:%Cblue%h %Cred%cr %Cgreen%cn%Creset - %s%C(yellow)%+d
view raw .gitconfig hosted with ❤ by GitHub

Here's a screenshot of how it looks (with details obscured to protect the innocent).

Update: I dropped the line break before the refs list, which is fine if you have a nice wide terminal, and tweaked some colours. Now it's:

  nice = format:%Cblue%h %C(cyan)%cr %Cgreen%an%Creset %s%C(yellow)%d %Creset

Tuesday, October 2, 2012

Some "progress" on my Tau in Ruby patch

Its category was changed from "core" to "joke" :(... How does the quote go? "First they ignore your patch, then they classify it as a joke, then... " I forget the rest ;)

"Once τ is widely accepted in these communities, we might add it..."

But also:

"...if it were just up to me, I'd add it just to show support. It's a rather tiny and harmless addition."

See the ticket here.

Tuesday, August 14, 2012

Wednesday, August 8, 2012

Make it easier to use partials with blocks in rails

The shorter version of render :partial is widely known and used. These are equivalent, but the second version is so much more succinct and readable.
<%= render :partial => 'thing_one', :locals => { :foo => '123' } %>
<%= render 'thing_one', :foo => '123' %>
To use a partial with a yield in it, (what I call a "block partial"), you can use render :layout which a. doesn't read very well, (it's a partial, not a layout clearly), and b. can't be shortened nicely the way that render :partial can. So here is block_render:
#
# A trick so you can have partials that take blocks.
# Uses render :layout.
#
# Example:
# <%= block_render 'some_partial_with_a_yield', :foo => 123 do %>
# Any erb here...
# <% end %>
#
def block_render(partial_name, locals={})
render :layout => partial_name, :locals => locals do
yield
end
end
It lets you have nice neat render calls like this:
<%= block_render 'block_thing', :foo => '123' do %>
stuff inside here
<% end %>
This is a real-life example of a block partial I like to use. Those classes are bootstrap classes for making a nice drop down menu.
<%#
#
# Locals:
# right (optional)
#
# Example usage:
# <x= block_render 'shared/more_button_menu' x>
# <li><x= link_to 'Foo, '#' x></li>
# <li><x= link_to 'Bar, '#' x></li>
# <x end x>
#
-%>
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">More <span class="caret"></span></a>
<ul class="dropdown-menu<%= " pull-right" if defined?(right) && right %>">
<%= yield %>
</ul>

How to make pinned tabs in Firefox a bit wider

I'm using Stylish to apply this, but I guess you could do it with a userChrome.css.
@namespace url(http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul);
.tabbrowser-tab[pinned] {
width: 72px;
}
view raw styles.css hosted with ❤ by GitHub
The main reason I want this is so the hit target is larger. I want to be able to click my pinned app tabs (currently a TiddlyWiki and my Zimbra corporate calendar) without aiming too hard.

Wednesday, February 8, 2012

Make ActiveRecord::Base#create respect the :type attribute

I think I've needed and implemented this twice now, so let's make a post about it.

The problem occurs when you are using f.fields_for and nested forms, as per these railscasts, and the nested objects are using STI.

(Will leave a more detailed explanation for future posts).

Update 1: Even though this works as described, I don't think it solves my problem. Will post again if I figure it out...

Update 2: Found this which is I think what I was using last time I had this problem... :S


#
# This was causing problems when submitting the nested object form
# in a/v/product/channels_edit because all the nested channels would
# be created at plan Channel objects instead of the PrimaryChannel, EusChannel etc.
#
# In active record normally :type is ignored, eg:
# Channel.create(:type='PrimaryChannel', :etc=>1, ...)
# ignores the type attr and uses 'Channel'.
#
# This hack will make it actually use the :type value.
#
# Usage:
#
# class User < ActiveRecord::Base
# extend ActiveRecord::CreateWithSubtype
# ...
# end
#
# class AdminUser < User
# ...
# end
#
# >> User.create(:type=>'AdminUser', :username=>'simon', ...) #=> #<AdminUser id:123 ...>
#
# Actually needed this for nested objects in forms using accepts_nested_attributes_for.
# http://railscasts.com/episodes/197-nested-model-form-part-2
#
# When updating the type is preserved, but when creating a new subobject with the nested form
# the type is always the base class, which in my case was a problem.
#
# (I put this file in lib/active_record/create_with_subtype.rb)
#
module ActiveRecord
module CreateWithSubtype
def create(attributes=nil, &block)
# Check if :type attribute exists. Remove it from attributes
# (Note that attributes can be an array, hence Hash test)
specified_type = attributes.delete(:type) if attributes.is_a?(Hash)
if specified_type
# Find the specified class
use_sub_class = const_get(specified_type)
# Sanity check
raise "'#{use_sub_class}' not a subclass of '#{self}'" unless self.descendants.include?(use_sub_class)
# Call create on the specified subclass
use_sub_class.create(attributes, &block)
else
# Otherwise, just do the normal thing
super(attributes, &block)
end
end
end
end