Skip to content

Commit

Permalink
Added support for nokogiri >= 1.5.1 while maintaining backwards compa…
Browse files Browse the repository at this point in the history
…tibility for >=1.4.2
  • Loading branch information
visoft committed Feb 12, 2013
1 parent abf1bbb commit 9e48dc4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
6 changes: 6 additions & 0 deletions lib/ruby_odata/helpers.rb
Expand Up @@ -17,5 +17,11 @@ def self.uri_escape(*args)
URI.escape(*args)
end
end

# Nokogiri changed how it handles namespaced attributes with v1.5.1, this is for backwards compatibility to >= 1.4.2
# Nokogiri now >=1.5.1 requires the namespace prefix is used
def self.get_namespaced_attribute(node, attr_name, prefix)
return node["#{prefix}:#{attr_name}"] || node[attr_name]
end
end
end
5 changes: 3 additions & 2 deletions lib/ruby_odata/property_metadata.rb
Expand Up @@ -24,8 +24,9 @@ def initialize(property_element)
@name = property_element['Name']
@type = property_element['Type']
@nullable = ((property_element['Nullable'] && property_element['Nullable'] == "true") || property_element.name == 'NavigationProperty') || false
@fc_target_path = property_element['FC_TargetPath']
@fc_keep_in_content = (property_element['FC_KeepInContent']) ? (property_element['FC_KeepInContent'] == "true") : nil
@fc_target_path = Helpers.get_namespaced_attribute(property_element, 'FC_TargetPath', 'm')
keep_in_content = Helpers.get_namespaced_attribute(property_element, 'FC_KeepInContent', 'm')
@fc_keep_in_content = (keep_in_content) ? (keep_in_content == "true") : nil
@nav_prop = property_element.name == 'NavigationProperty'
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/ruby_odata/service.rb
Expand Up @@ -630,7 +630,7 @@ def build_batch_operation(operation, changeset_num)

# Complex Types
def complex_type_to_class(complex_type_xml)
klass_name = qualify_class_name(complex_type_xml.attr('type').split('.')[-1])
klass_name = qualify_class_name(Helpers.get_namespaced_attribute(complex_type_xml, 'type', 'm').split('.')[-1])
klass = @classes[klass_name].new

# Fill in the properties
Expand Down Expand Up @@ -663,8 +663,8 @@ def parse_date(sdate)

# Parses a value into the proper type based on an xml property element
def parse_value(property_xml)
property_type = property_xml.attr('type')
property_null = property_xml.attr('null')
property_type = Helpers.get_namespaced_attribute(property_xml, 'type', 'm')
property_null = Helpers.get_namespaced_attribute(property_xml, 'null', 'm')

# Handle a nil property type, this is a string
return property_xml.content if property_type.nil?
Expand Down
4 changes: 2 additions & 2 deletions spec/property_metadata_spec.rb
Expand Up @@ -28,13 +28,13 @@ module OData
property_metadata.nullable.should eq false
end
it "parses an EDMX property with the fc_target_path and fc_keep_in_content attribute" do
property_element = RSpecSupport::ElementHelpers.string_to_element('<Property Name="Title" Type="Edm.String" Nullable="true" m:FC_TargetPath="SyndicationTitle" m:FC_ContentKind="text" m:FC_KeepInContent="false" />')
property_element = RSpecSupport::ElementHelpers.string_to_element('<Property xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" Name="Title" Type="Edm.String" Nullable="true" m:FC_TargetPath="SyndicationTitle" m:FC_ContentKind="text" m:FC_KeepInContent="false" />')
property_metadata = PropertyMetadata.new property_element
property_metadata.fc_target_path.should eq "SyndicationTitle"
property_metadata.fc_keep_in_content.should eq false
end
it "parses an EDMX property where fc_keep_in_content is true" do
property_element = RSpecSupport::ElementHelpers.string_to_element('<Property Name="Title" Type="Edm.String" Nullable="true" m:FC_TargetPath="SyndicationTitle" m:FC_ContentKind="text" m:FC_KeepInContent="true" />')
property_element = RSpecSupport::ElementHelpers.string_to_element('<Property xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" Name="Title" Type="Edm.String" Nullable="true" m:FC_TargetPath="SyndicationTitle" m:FC_ContentKind="text" m:FC_KeepInContent="true" />')
property_metadata = PropertyMetadata.new property_element
property_metadata.fc_keep_in_content.should eq true
end
Expand Down

2 comments on commit 9e48dc4

@sillylogger
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, I wish I had seen this yesterday... I ended up doing dirty things to nokogiri. This is much better. I don't see the impetus for that change on nokogiri's end.. it's not well documented that they were changing the [] method.

@visoft
Copy link
Owner Author

@visoft visoft commented on 9e48dc4 Feb 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That change drove me nuts. I'm surprised that 1.5.0 was ok (the version I was originally running) but 1.5.1+ had such a big change that wasn't really noted at all.

Please sign in to comment.