Created
December 22, 2009 15:09
-
-
Save pjfitzgibbons/261791 to your computer and use it in GitHub Desktop.
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 | |
PROFILE_KEYS = %w/ first_name last_name office_phone home_phone cell_phone / | |
def self.moderator_fields | |
key_val_pairs = @params.select {|k,v| not PROFILE_KEYS.include? k}.flatten | |
Hash[*key_val_pairs] | |
end | |
def self.profile_fields | |
key_val_pairs = @params.select {|k,v| PROFILE_KEYS.include? k }.flatten | |
Hash[*key_val_pairs] | |
end | |
def self.new_with_profile(params) | |
@params = params | |
new_moderator = create moderator_fields | |
new_moderator.profile = Profile.create profile_fields | |
new_moderator | |
end | |
def update_with_profile(params) | |
@params = params | |
update_attributes self.class.moderator_fields and | |
profile.update_attributes self.class.profile_fields | |
save | |
end | |
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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | |
describe User do | |
@u = Factory(:user) | |
end | |
describe "with profile" do | |
before(:each) do | |
@example_params = { | |
"email" => "user.test@test.com", | |
"company_name" => "Test Company Name", | |
"form_toll_number" => "3125554444", | |
"form_toll_free_number" => "3125556666", | |
"first_name" => 'User', | |
"last_name" => "Test", | |
"office_phone" => "8005551111", | |
"home_phone" => "3125552222", | |
"cell_phone" => "3125553333", | |
} | |
end | |
it "should create modeator with profile" do | |
user = User.new_with_profile(@example_params) | |
%w/email company_name form_toll_number form_toll_free_number/.each do |fld| | |
user[fld].should == @example_params[fld] | |
end | |
%w/first_name last_name office_phone home_phone cell_phone/.each do |fld| | |
user.profile[fld].should == @example_params[fld] | |
end | |
end | |
it "should update user with profile" do | |
@u.update_with_profile @example_params | |
%w/email company_name form_toll_number form_toll_free_number/.each do |fld| | |
@u[fld].should == @example_params[fld] | |
end | |
%w/first_name last_name office_phone home_phone cell_phone/.each do |fld| | |
@u.profile[fld].should == @example_params[fld] | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment