module Fiona7 module AttributeWriters module Helpers class LinkSerializer def initialize(obj, klass) self.obj = obj self.klass = klass end def call(link) link = link.symbolize_keys new_link = {} if link[:url] # handle www.example.com new_link[:url] = "external:#{link[:url]}" unless link[:url] =~ /\A[a-zA-Z][a-zA-Z0-9+.-]:/ elsif obj_id = link[:obj_id] # FIXME: use a repository here to speed up the lookup new_link[:url] = self.klass.find(obj_id).path end new_link[:url] ||= link[:url] new_link[:title] = link[:title] if link[:title] new_link[:target] = link[:target] if link[:target] new_link[:url] = "#{new_link[:url]}?#{link[:query]}" if link[:query] new_link[:url] = "#{new_link[:url]}##{link[:fragment]}" if link[:fragment] new_link end protected attr_accessor :obj, :klass end end end end