Skip to content

Commit

Permalink
Merge pull request #59 from basvodde/master
Browse files Browse the repository at this point in the history
Changing a custom field when the field wasn't set before
  • Loading branch information
Lucas Neves Martins committed Apr 18, 2014
2 parents 8aba140 + 5ddadd7 commit 4da21a5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/highrise/person.rb
Expand Up @@ -36,11 +36,21 @@ def field(field_label)
field ? field.value : nil
end

def new_subject_data(field, value)
Highrise::SubjectData.new(:subject_field_id => field.id, :subject_field_label => field.label, :value => value)
end

def set_field_value(field_label, new_value)
custom_fields = attributes["subject_datas"] ||= []
custom_fields.each { |field|
return field.value = new_value if field.subject_field_label== field_label
}

SubjectField.find(:all).each { |custom_field|
if custom_field.label == field_label
return attributes["subject_datas"] << new_subject_data(custom_field, new_value)
end
}
end

end
Expand Down
9 changes: 9 additions & 0 deletions spec/highrise/person_spec.rb
Expand Up @@ -59,6 +59,8 @@
}]
}
})
@subject_field_blueberry = Highrise::SubjectField.new ({:id => 1, :label => "Fruit Blueberry"})
@subject_field_papaya = Highrise::SubjectField.new ({:id => 2, :label => "Fruit Papaya"})
end

it "Can get the value of a custom field via the field method" do
Expand All @@ -70,6 +72,13 @@
@fruit_person.field("Fruit Grape").should== "Red"
end

it "Can set the value of a custom field that wasn't there via the field method, but that was defined (happens on new Person)" do
Highrise::SubjectField.should_receive(:find).with(:all).and_return([@subject_field_papaya, @subject_field_blueberry])
@fruit_person.set_field_value("Fruit Blueberry", "Purple")
@fruit_person.field("Fruit Blueberry").should== "Purple"
@fruit_person.attributes["subject_datas"][2].subject_field_id.should == 1
end

end

it { subject.label.should == 'Party' }
Expand Down

0 comments on commit 4da21a5

Please sign in to comment.