lib/wcc/contentful/model/menu_button.rb in wcc-contentful-app-0.4.0.pre.rc vs lib/wcc/contentful/model/menu_button.rb in wcc-contentful-app-1.0.0.pre.rc1
- old
+ new
@@ -1,16 +1,29 @@
# frozen_string_literal: true
class WCC::Contentful::Model::MenuButton < WCC::Contentful::Model
- validate_field :text, :String, :required
- validate_field :icon, :Asset, :optional
- validate_field :external_link, :String, :optional
- validate_field :link, :Link, :optional, link_to: 'page'
+ def external_uri
+ @external_url ||= URI(external_link) if external_link.present?
+ end
+ # A menu link is external if `external_link` is present and not relative.
+ def external?
+ external_uri&.scheme.present?
+ end
+
# Gets either the external link or the slug from the referenced page.
# Example usage: `<%= link_to button.title, button.href %>`
def href
return external_link if external_link
- link&.try(:slug) || link&.try(:url)
+ url = (link&.try(:slug) || link&.try(:url))
+ return url unless fragment.present?
+
+ url = URI(url || '')
+ url.fragment = fragment
+ url.to_s
+ end
+
+ def fragment
+ WCC::Contentful::App::SectionHelper.section_id(section_link) if section_link
end
end