Permalink
Browse files

models/person_spec passes

  • Loading branch information...
1 parent 534cbdf commit c1612f7d40fb0065b1ef5b1f4f12356856b52cf6 @linojon committed Mar 18, 2012
View
@@ -12,6 +12,7 @@ end
gem 'jquery-rails'
gem 'haml-rails'
gem 'simple_form'
+gem 'hebruby'
# official unofficial ruby-debug19 fix
# with the same gems as mentioned in
@@ -29,6 +30,7 @@ group :test, :development do
gem "rspec-rails"
gem 'rspec-instafail'
gem 'factory_girl_rails'
+ gem 'faker'
gem 'database_cleaner'
gem "capybara"
end
View
@@ -63,13 +63,16 @@ GEM
factory_girl_rails (1.7.0)
factory_girl (~> 2.6.0)
railties (>= 3.0.0)
+ faker (1.0.1)
+ i18n (~> 0.4)
ffi (1.0.11)
haml (3.1.4)
haml-rails (0.3.4)
actionpack (~> 3.0)
activesupport (~> 3.0)
haml (~> 3.0)
railties (~> 3.0)
+ hebruby (2.0.4)
heroku (2.21.3)
launchy (>= 0.3.2)
netrc (~> 0.7.1)
@@ -191,7 +194,9 @@ DEPENDENCIES
coffee-rails (~> 3.2.1)
database_cleaner
factory_girl_rails
+ faker
haml-rails
+ hebruby
heroku
jquery-rails
linecache19 (>= 0.5.13)
View
@@ -1,2 +1,96 @@
+require 'capitalize_name'
+require 'hebrew_date'
+require 'proper_name_validator'
+
class Person < ActiveRecord::Base
+ #extend ActiveSupport::Memoizable
+
+ def full_name
+ [ prefix, first_name, middle_name, last_name, suffix ].join(' ')
+ end
+
+ composed_of :death_hebrew_date,
+ :class_name => 'Hebruby::HebrewDate',
+ :mapping =>
+ [ # database # HebrewDate
+ [:death_hebrew_date_day, :day],
+ [:death_hebrew_date_month, :month],
+ [:death_hebrew_date_year, :year]
+ ],
+ :allow_nil => true
+
+ GENDERS = %w{ male female }
+ validates :gender, :inclusion => { :in => GENDERS }, :allow_blank => true
+
+ PREFIXES = %w{ Ms Miss Mrs Mr Dr Atty Prof Hon Gov Ofc Rabbi Cantor }
+ validates :prefix, :inclusion => { :in => PREFIXES }, :allow_blank => true
+
+ validates :last_name, :presence => true,
+ :length => { :minimum => 2 },
+ :proper_name => true
+
+ validates :first_name, :presence => true,
+ :proper_name => true
+
+ validates :maiden, :proper_name => true,
+ :allow_blank => true
+
+ validates :middle_name, :proper_name => true,
+ :allow_blank => true
+
+ validate :death_date_cannot_be_before_birth_date
+
+
+ def death_date_cannot_be_before_birth_date
+ if !death_date.blank? and !birth_date.blank? and death_date < birth_date
+ errors.add(:death_date, "can't be before birth date")
+ end
+ end
+
+ before_save :strip_and_capitalize_names
+ before_save :set_death_dates
+
+ # get next yahrzeit date on or after "from" date
+ def next_yahrzeit_date(from=Date.today)
+ return unless death_date
+ # TODO: use Marlena rules
+ h_from = Hebruby::HebrewDate.new(from)
+ h_death = Hebruby::HebrewDate.new(death_date)
+ # yahrzeit date from year
+ h_yahrzeit = Hebruby::HebrewDate.new(h_death.day, h_death.month, h_from.year)
+ date = Date.jd(h_yahrzeit.jd)
+ if date < from
+ h_yahrzeit = Hebruby::HebrewDate.new(h_death.day, h_death.month, h_from.year+1)
+ date = Date.jd(h_yahrzeit.jd)
+ end
+ date
+ end
+
+ ### callbacks
+
+ def strip_and_capitalize_names
+ self.last_name.strip! unless last_name.nil?
+ self.first_name.strip! unless first_name.nil?
+ self.middle_name.strip! unless middle_name.nil?
+ self.maiden.strip! unless maiden.nil?
+
+ self.last_name.capitalize_name! if last_name.present?
+ self.first_name.capitalize_name! if first_name.present?
+ self.middle_name.capitalize_name! if middle_name.present?
+ self.maiden.capitalize_name! if maiden.present?
+ end
+
+ def set_death_dates
+ if death_hebrew_date.nil? && death_date.present?
+ date = death_date
+ date += 1.day if death_after_sunset
+ self.death_hebrew_date = Hebruby::HebrewDate.new(date)
+ end
+ if death_date.nil? && death_hebrew_date.present?
+ jd = death_hebrew_date.jd
+ jd -= 1 if death_after_sunset
+ self.death_date = Date.jd(jd)
+ end
+ end
+
end
@@ -5,7 +5,7 @@
= f.input :last_name
= f.input :first_name
= f.input :middle_name
- = f.input :maden
+ = f.input :maiden
= f.input :prefix
= f.input :suffix
= f.input :gender
@@ -5,7 +5,7 @@
%th Last name
%th First name
%th Middle name
- %th Maden
+ %th Maiden
%th Prefix
%th Suffix
%th Gender
@@ -24,7 +24,7 @@
%td= person.last_name
%td= person.first_name
%td= person.middle_name
- %td= person.maden
+ %td= person.maiden
%td= person.prefix
%td= person.suffix
%td= person.gender
@@ -10,8 +10,8 @@
%b Middle name:
= @person.middle_name
%p
- %b Maden:
- = @person.maden
+ %b Maiden:
+ = @person.maiden
%p
%b Prefix:
= @person.prefix
@@ -4,7 +4,7 @@ def change
t.string :last_name
t.string :first_name
t.string :middle_name
- t.string :maden
+ t.string :maiden
t.string :prefix
t.string :suffix
t.string :gender
View
@@ -17,7 +17,7 @@
t.string "last_name"
t.string "first_name"
t.string "middle_name"
- t.string "maden"
+ t.string "maiden"
t.string "prefix"
t.string "suffix"
t.string "gender"
View
@@ -237,11 +237,20 @@ re-deploy
## ---------------
## people
-$ rails g scaffold person last_name first_name middle_name maden prefix suffix gender birth_date:date death_date:date death_hebrew_date_day:integer death_hebrew_date_month:integer death_hebrew_date_year:integer death_after_sunset:boolean --skip-stylesheets
+$ rails g scaffold person last_name first_name middle_name maiden prefix suffix gender birth_date:date death_date:date death_hebrew_date_day:integer death_hebrew_date_month:integer death_hebrew_date_year:integer death_after_sunset:boolean --skip-stylesheets
$ rake db:migrate
$ rails s
+$ rake spec
+specs for people
+ Gemfile
+ gem 'faker'
+ spec/factories/people.rb
+ app/models/person.rb
+ spec/support/random_date.rb
+ spec/support/shared_examples.rb
+
## ---------------
##
@@ -0,0 +1,13 @@
+class String
+ def capitalize_name
+ if self.downcase==self || self.upcase==self
+ self.capitalize
+ else
+ self
+ end
+ end
+ def capitalize_name!
+ self.replace self.capitalize_name
+ end
+
+end
View
@@ -0,0 +1,40 @@
+class Hebruby::HebrewDate
+ include Comparable
+
+ def to_s(format = :day_month_year)
+ case format
+ when :day_month
+ "#{day} #{month_name}"
+ else # :day_month_year
+ "#{day} #{month_name} #{year}"
+ end
+ end
+
+ def ==(other_date)
+ jd == other_date.jd if other_date.is_a? Hebruby::HebrewDate
+ end
+
+ def <=>(other_date)
+ jd <=> other_date.jd if other_date.is_a? Hebruby::HebrewDate
+ end
+
+ # new object from string "dd month yyyy"
+ def self.parse(hebstr)
+ if hebstr.include? 'Sheni'
+ day, month, sheni, year = hebstr.split(' ')
+ hm = 13
+ else
+ day, month, year = hebstr.split(' ')
+ hm = MONTH_NAMES.find_index {|m| m==month}
+ end
+ self.new( day.to_i, hm, year.to_i)
+ end
+
+ def self.today
+ @today ||= Hebruby::HebrewDate.new(Date.today.jd)
+ end
+
+ def self.months_for_select
+ 1.upto(13).map {|i| [Hebruby::HebrewDate::MONTH_NAMES[i], i] }
+ end
+end
@@ -0,0 +1,7 @@
+class ProperNameValidator < ActiveModel::EachValidator
+ def validate_each(record, attribute, value)
+ unless value =~ /\A[a-zA-Z\'\-\x20]+\z/
+ record.errors[attribute] << (options[:message] || "is not a proper name format")
+ end
+ end
+end
View
@@ -2,18 +2,21 @@
FactoryGirl.define do
factory :person do
- last_name "MyString"
- first_name "MyString"
- middle_name "MyString"
- maden "MyString"
- prefix "MyString"
- suffix "MyString"
- gender "MyString"
- birth_date "2012-03-17"
- death_date "2012-03-17"
- death_hebrew_date_day 1
- death_hebrew_date_month 1
- death_hebrew_date_year 1
- death_after_sunset false
+ last_name { Faker::Name.last_name }
+ first_name { Faker::Name.first_name }
+ middle_name { Faker::Name.first_name }
+ # prefix "MyString"
+ # suffix "MyString"
+ gender { Person::GENDERS[rand 2] }
+ ignore do
+ bdate { randomDate(:year_range => 60, :year_latest => 22).to_date }
+ end
+ maiden { Faker::Name.last_name if gender=='female' }
+ birth_date { bdate }
+ death_date { bdate + rand(Date.today - bdate) }
+ # death_hebrew_date_day 1
+ # death_hebrew_date_month 1
+ # death_hebrew_date_year 1
+ # death_after_sunset false
end
end
Oops, something went wrong.

0 comments on commit c1612f7

Please sign in to comment.