Permalink
Browse files

Added "gender" attribute to contacts and leads

  • Loading branch information...
Philipp Ullmann
Philipp Ullmann committed Apr 11, 2012
2 parents 2ed6591 + 9357fc5 commit 3b8c0b057f9fb1dc4f17eea49879146417a37110
@@ -49,6 +49,8 @@
#
class Contact < ActiveRecord::Base
+ include HasAGender
+
belongs_to :user
belongs_to :lead
belongs_to :assignee, :class_name => "User", :foreign_key => :assigned_to
@@ -0,0 +1,37 @@
+# Include this module in models, which have a "gender" database column.
+module HasAGender
+ extend ActiveSupport::Concern
+ GENDER = %w{m f}
+
+ included do
+ validates :gender, :inclusion => { :in => GENDER }
+ end
+
+ module ClassMethods
+ # Returns an array, which acts as a mapping between gender key and human readable name.
+ #----------------------------------------------------------------------------
+ def dropdown_list
+ GENDER.map { |gender| [humanize_gender_for(gender), gender] }
+ end
+
+ # Returns the human readable name for a given gender key.
+ # Returns null if the given gender is unknown.
+ #----------------------------------------------------------------------------
+ def humanize_gender_for(key)
+ case key
+ when 'm'
+ I18n.t 'male'
+ when 'f'
+ I18n.t 'female'
+ else
+ nil
+ end
+ end
+ end
+
+ # Returns a human readable representation of the keys "m" or "f".
+ #----------------------------------------------------------------------------
+ def humanize_gender
+ self.class.humanize_gender_for gender
+ end
+end
@@ -49,6 +49,8 @@
#
class Lead < ActiveRecord::Base
+ include HasAGender
+
belongs_to :user
belongs_to :campaign
belongs_to :assignee, :class_name => "User", :foreign_key => :assigned_to
@@ -17,6 +17,10 @@
%td
.label #{t :phone}:
= f.text_field :phone
+ %tr
+ %td
+ .label #{t :gender}:
+ = f.select :gender, Contact.dropdown_list
%table
= fields_for(@account) do |a|
@@ -17,6 +17,10 @@
%td
.label #{t :phone}:
= f.text_field :phone
+ %tr
+ %td
+ .label #{t :gender}:
+ = f.select :gender, Lead.dropdown_list
- if Setting.background_info && Setting.background_info.include?(:lead)
%tr
@@ -610,7 +610,9 @@ de:
yahoo: "Yahoo IM"
yes_button: "Ja"
zipcode: "Postleitzahl"
-
+ gender: Geschlecht
+ male: Männlich
+ female: Weiblich
# Views -> Admin
# TODO
@@ -12,6 +12,9 @@ en-US:
select_none: '-- None --'
select_blank: '-- Select --'
yes_button: 'Yes'
+ gender: Gender
+ male: Male
+ female: Female
# Settings.
@@ -0,0 +1,8 @@
+class AddGenderToContactsAndLeads < ActiveRecord::Migration
+ def change
+ with_options :limit => 1, :default => 'm' do |o|
+ o.add_column :contacts, :gender, :string
+ o.add_column :leads, :gender, :string
+ end
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120405080742) do
+ActiveRecord::Schema.define(:version => 20120411082730) do
create_table "account_contacts", :force => true do |t|
t.integer "account_id"
@@ -169,6 +169,7 @@
t.string "background_info"
t.string "skype", :limit => 128
t.text "subscribed_users"
+ t.string "gender", :limit => 1, :default => "m"
end
add_index "contacts", ["assigned_to"], :name => "index_contacts_on_assigned_to"
@@ -255,6 +256,7 @@
t.string "background_info"
t.string "skype", :limit => 128
t.text "subscribed_users"
+ t.string "gender", :limit => 1, :default => "m"
end
add_index "leads", ["assigned_to"], :name => "index_leads_on_assigned_to"
@@ -36,7 +36,7 @@ def uses_user_permissions
scope :my, lambda {
current_ability = Ability.new(User.current_user)
- accessible_by(current_ability)
+ accessible_by(current_ability).readonly(false)
}
include FatFreeCRM::Permissions::InstanceMethods
@@ -0,0 +1,51 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe Contact do
+ before :each do
+ @contact = Contact.new
+ end
+
+ context 'validate gender' do
+ it 'should be invalid if gender is not "m" or "f"' do
+ @contact.gender = "a"
+ @contact.valid?
+ @contact.should have_errors_on(:gender).with_message('is not included in the list')
+ end
+
+ it 'should be valid if gender is "m"' do
+ @contact.gender = "m"
+ @contact.valid?
+ @contact.should_not have_errors_on(:gender)
+ end
+
+ it 'should be valid if gender is "f"' do
+ @contact.gender = "f"
+ @contact.valid?
+ @contact.should_not have_errors_on(:gender)
+ end
+ end
+
+ describe '#humanize_gender' do
+ it 'should return "Male" for "m"' do
+ @contact.gender = 'm'
+ @contact.humanize_gender.should == 'Male'
+ end
+
+ it 'should return "Female" for "f"' do
+ @contact.gender = 'f'
+ @contact.humanize_gender.should == 'Female'
+ end
+
+ it 'should return nil for an unkown gender' do
+ @contact.gender = 'a'
+ @contact.humanize_gender.should be_nil
+ end
+ end
+
+ describe '#dropdown_list' do
+ it 'should return a mapping between gender key and readable name' do
+ Contact.dropdown_list.should == [['Male', 'm'],
+ ['Female', 'f']]
+ end
+ end
+end
View
@@ -0,0 +1,29 @@
+RSpec::Matchers.define(:have_errors_on) do |attribute|
+ chain(:with_message) do |message|
+ @message = message
+ end
+
+ match do |model|
+ model.valid?
+
+ @has_errors = model.errors.include?(attribute)
+
+ if @message
+ @has_errors && model.errors[attribute].include?(@message)
+ else
+ @has_errors
+ end
+ end
+
+ failure_message_for_should do |model|
+ if @message
+ "Validation errors #{model.errors[attribute].inspect} should include #{@message.inspect}"
+ else
+ "#{model.class} should have errors on attribute #{attribute.inspect}"
+ end
+ end
+
+ failure_message_for_should_not do |model|
+ "#{model.class} should not have an error on attribute #{attribute.inspect}"
+ end
+end

0 comments on commit 3b8c0b0

Please sign in to comment.