Skip to content

Instantly share code, notes, and snippets.

@baldwindavid
Created November 19, 2010 01:56
Show Gist options
  • Save baldwindavid/706019 to your computer and use it in GitHub Desktop.
Save baldwindavid/706019 to your computer and use it in GitHub Desktop.
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_counter
helper_method :counter
protected
def counter
@counter
end
def set_counter
@counter = Counter.new
end
end
class Counter
def initialize(i = 1)
@i = i
end
def current
@i
end
def increment!(by = 1)
@i += by
end
end
%h3= "#{counter.current}. Something"
%p Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
%h3= "#{counter.increment!}. Something Else"
%p Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
%h3= "#{counter.increment!}. Other Stuff"
%p Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
@baldwindavid
Copy link
Author

This is handy in cases when you need to manually set numbering in a view. The example (index.html.haml) is not a good one given you would obviously use an ordered list with that content...but you get the point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment