Skip to content

Instantly share code, notes, and snippets.

@Holek
Created October 17, 2011 09:48
Show Gist options
  • Save Holek/1292297 to your computer and use it in GitHub Desktop.
Save Holek/1292297 to your computer and use it in GitHub Desktop.
Birthday gem examples
> User.find_birthdays_for(Date.parse('04-04-2000'))
=> [#<User id: 23, birthday: "1961-04-04">, #<User id: 34, birthday: "1985-04-04">]
> User.find_birthdays_for(Date.parse('04-04-2000'), Date.parse('05-04-2000'))
=> [#<User id: 23, birthday: "1961-04-04">, #<User id: 34, birthday: "1985-04-04">, #<User id: 56, birthday: "1976-04-24">, #<User id: 57, birthday: "1958-04-30">, #<User id: 60, birthday: "1986-05-04">]
# This will search for birthdays between 12.12 and 3.01
> User.find_birthdays_for(Date.parse('12-12-2000'), Date.parse('03-01-2001'))
=> [#<User id: 1, birthday: "1961-12-14">, #<User id: 12, birthday: "1985-12-15">, #<User id: 25, birthday: "1961-12-24">, #<User id: 27, birthday: "1985-01-01">, #<User id: 40, birthday: "1961-01-02">]
> u = User.find(1)
=> #<User id: 1, birthday: "1961-12-14">
> u.birthday_age
=> 50
> u.birthday_today?
=> false
# obviously true on 14 December
class User < ActiveRecord::Base
acts_as_birthday :birthday
scope :admins, {:is_admin => true}
scope :named_like, lambda { |name| { :conditions => [ "first_name LIKE :q OR email LIKE :q OR last_name LIKE :q", { :q => "%#{name}%" } ] } }
end
> User.admins.named_like("Mike").find_birthdays_for(Date.parse('12-12-2000'), Date.parse('03-01-2001'))
class Marriage < ActiveRecord::Base
acts_as_birthday :anniversary
end
# (Date.today = 2011-10-14)
> marriage = Marriage.create(:anniversary => Date.parse('1999-10-14'))
=> #<Marriage id: 1, :anniversary => "1999-10-14">
> marriage.anniversary_today?
=> true
> marriage.anniversary_age
=> 12
> marriage2 = Marriage.create(:anniversary => Date.parse('1999-10-24'))
=> #<Marriage id: 2, :anniversary => "1999-10-24">
> marriage2.anniversary_today?
=> false
> marriage2.anniversary_age
=> 11
> Marriage.find_anniversaries_for(Date.parse('2001-10-24'))
=> [#<Marriage id: 2, :anniversary => "1999-10-24">]
class User < ActiveRecord::Base
acts_as_birthday :birthday
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment