-
Notifications
You must be signed in to change notification settings - Fork 24
Home
Here is a brief instruction to set up zodiac in your projects. It consists of 3 steps, but the number of steps required for your situation depends on what you want to get.
For casual use:
gem install zodiacFor a bundler-powered project:
# in your Gemfile
gem 'zodiac'# and in your shell (from your project directory)
bundle installNow you can require the gem and get all the goodies for date/time classes:
require 'zodiac'
Time.now.zodiac_sign # => "Libra"
Time.gm(1987, 3, 28).aries? # => true
Date.new(1964, 9, 12).gemini? # => falseThis step is required if you have a rails app and you want to work with zodiac sign of your model objects.
# in your app/models/person.rb
class Person < ActiveRecord::Base
zodiac_reader :dob
endNow you got some new methods for introspecting the object's zodiac sign based on a :dob attribute:
@person = Person.first
@person.zodiac_sign # => 'Scorpio'
@person.scorpio? # => true
@person.libra? # => falseThis step is only required if you need to search, for example, all the scorpios in your people table. It's slightly more complicated than the previous two steps, but not as difficult as it may seem.
First you should generate the migration which adds an integer field to your table - just give the generator the name of your model class:
rails generate zodiac:migration PersonYou can read the migration - it's no more than 20 lines of easy-readable code (or you can trust me saying that it just creates the integer field, puts an index on it and fills it for all the existing records). By the way the generator is smart enough to distinguish between 3.0 and 3.1 versions of active_record.
Next you need to apply the migration:
rake db:migrateAnd now you get all the scopes:
Person.by_zodiac(3) # AR::Relation with all the geminis
Person.by_zodiac(:gemini) # the same
Person.gemini # and the same again
Person.pisces # and now AR::Relation with the piscesDon't worry, the integer field is auto-updated each time you change a date-of-birth for some of your objects (basically, zodiac_reader adds a before_save hook doing that, if it sees it's own integer field in your table).