Skip to content

Commit

Permalink
Removed unneeded whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
basvodde committed May 11, 2014
1 parent 8723894 commit 8a42512
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
26 changes: 13 additions & 13 deletions lib/highrise/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ def web_address
def label
'Party'
end

def field(field_label)
custom_fields = attributes["subject_datas"] ||= []
field = custom_fields.detect { |field|
field.subject_field_label == 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|

SubjectField.find(:all).each { |custom_field|
if custom_field.label == field_label
return attributes["subject_datas"] << new_subject_data(custom_field, new_value)
end
Expand All @@ -52,7 +52,7 @@ def set_field_value(field_label, new_value)
def transform_subject_field_label field_label
field_label.downcase.tr(' ', '_')
end

def convert_method_to_field_label method
custom_fields = attributes["subject_datas"] ||= []
custom_fields.each { |field|
Expand All @@ -61,19 +61,19 @@ def convert_method_to_field_label method
}
nil
end

def method_missing(method_symbol, *args)
method_name = method_symbol.to_s

if method_name[-1,1] == "="
attribute_name = method_name[0...-1]
field = convert_method_to_field_label(attribute_name)
return set_field_value(field.subject_field_label, args[0]) if field
return super if attributes[attribute_name]

return super if attributes[attribute_name]

subject_fields = SubjectField.find(:all)
subject_fields.each { |custom_field|
subject_fields.each { |custom_field|
if transform_subject_field_label(custom_field.label) == attribute_name
return attributes["subject_datas"] << new_subject_data(custom_field, args[0])
end
Expand All @@ -83,6 +83,6 @@ def method_missing(method_symbol, *args)
return field(field.subject_field_label) if field
end
super
end
end
end
end
36 changes: 18 additions & 18 deletions spec/highrise/person_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
subject.tags.should == @tags
}
end

describe "Custom fields" do

before (:each) do
@fruit_person = Highrise::Person.new({ :person => {
:id => 1,
:first_name => "John",
@fruit_person = Highrise::Person.new({ :person => {
:id => 1,
:first_name => "John",
:last_name => "Doe",
:subject_datas => [{
:subject_field_label => "Fruit Banana",
Expand All @@ -61,53 +61,53 @@
})
@subject_field_blueberry = Highrise::SubjectField.new ({:id => 1, :label => "Fruit Blueberry"})
@subject_field_papaya = Highrise::SubjectField.new ({:id => 2, :label => "Fruit Papaya"})
end
end

it "Can get the value of a custom field via the field method" do
@fruit_person.field("Fruit Banana").should== "Yellow"
end

it "Can get the value of a custom field using a custom method call" do
@fruit_person.fruit_grape.should== "Green"
end

it "Will raise an exception on an unknown field" do
expect {@fruit_person.unknown_fruit}.to raise_exception(NoMethodError)
end

it "Can set the value of a custom field via the field method" do
@fruit_person.set_field_value("Fruit Grape", "Red")
@fruit_person.field("Fruit Grape").should== "Red"
end

it "Can set the value of a custom field" do
@fruit_person.fruit_grape= "Red"
@fruit_person.fruit_grape.should== "Red"
end

it "Assignment just returns the arguments (like ActiveResource base does)" do
Highrise::SubjectField.should_receive(:find).with(:all).and_return []
(@fruit_person.unknown_fruit = 10).should== 10
end

it "Can deal with the find returning nil (which is a bit ugly in the ActiveResource API)" do
Highrise::SubjectField.should_receive(:find).with(:all).and_return nil
(@fruit_person.unknown_fruit = 10).should== 10
(@fruit_person.unknown_fruit = 10).should== 10
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

it "Can still set and read the usual way of reading attrivutes" do
@fruit_person.first_name = "Jacob"
@fruit_person.first_name.should== "Jacob"

end

end

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

0 comments on commit 8a42512

Please sign in to comment.