Skip to content

Commit

Permalink
add nil contacts handling to helper + regression specs
Browse files Browse the repository at this point in the history
  • Loading branch information
andersoncardoso committed Mar 15, 2014
1 parent f285ea5 commit 7947173
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def i18n_language_names

def current_translations
@translations ||= I18n.backend.send(:translations)
@translations[I18n.locale].with_indifferent_access
(@translations[I18n.locale] || {}).with_indifferent_access
end

def javascript_exists?(script)
Expand Down
4 changes: 3 additions & 1 deletion app/helpers/concerns/contacts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ module ContactsHelper
end

def contacts_list_for(object)
return [] if object.contacts.nil?
CONTACTS_ICONS.select{|key, val| object.contacts.keys.include? key }.map{ |key, icon| {
:key => key, :icon => icon, :value => object.contacts[key]
}}
end

def contacts_fields_for(object)
contacts = object.contacts || {}
CONTACTS_ICONS.map{ |key, icon| {
:key => key,
:icon => icon,
:value => object.contacts[key],
:value => contacts[key],
:name => "#{object.class.name.downcase}[contacts][#{key}]",
}}
end
Expand Down
10 changes: 10 additions & 0 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
let(:user) { FactoryGirl.create :user, :contacts => {'address' => 'rua Bla', 'phone' => '12345'} }

describe "#contacts_list_for" do
it 'returns an empty list for nil contacts' do
user.contacts = nil
expect(helper.contacts_list_for user).to eq []
end

it 'selects only the icons forms existing contact keys' do
expect(helper.contacts_list_for user).to eq([
{:key => 'phone', :icon => :phone, :value => "12345"},
Expand All @@ -117,6 +122,11 @@
expect(helper.contacts_fields_for(user).size).to eq 10
end

it "build fields even if contacts is nil" do
user.contacts = nil
expect(helper.contacts_fields_for(user).size).to eq 10
end

it "each contact field has key, icon, value and name" do
expect(helper.contacts_fields_for(user).first.keys).to eq [:key, :icon, :value, :name]
end
Expand Down

0 comments on commit 7947173

Please sign in to comment.