Created
October 17, 2011 09:48
-
-
Save Holek/1292297 to your computer and use it in GitHub Desktop.
Birthday gem examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> User.find_birthdays_for(Date.parse('04-04-2000')) | |
=> [#<User id: 23, birthday: "1961-04-04">, #<User id: 34, birthday: "1985-04-04">] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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">] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> u = User.find(1) | |
=> #<User id: 1, birthday: "1961-12-14"> | |
> u.birthday_age | |
=> 50 | |
> u.birthday_today? | |
=> false | |
# obviously true on 14 December |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Marriage < ActiveRecord::Base | |
acts_as_birthday :anniversary | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (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">] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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