link_helper.rb revision 1ce62f5bb9fe0d729f767af704035b9536e7d7fc
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenmodule LinkHelper
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen def sort_link_list(collection)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen hash = {}
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen collection.includes(:entity_mappings).each do |link|
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen link.entity_mappings.each do |mapping|
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen sym = mapping.source.to_s.to_sym
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen if hash[sym]
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen hash[sym] << {link: link, target: mapping.target}
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen else
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen hash[sym] = [{link: link, target: mapping.target}]
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen return hash
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
6ec925f52d04ec8700e47cb005bd7ddc65ac5614Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen def fancy_link(resource)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen clazz = resource.class
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen clazz = 'Ontology' if clazz.to_s.include?('Ontology')
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unless resource.is_a? Array then
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen if block_given?
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen name = yield resource
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen else
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen name = resource
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
617e13833c798435e2be425b99c27ecaad1b8393Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen link_to name, resource,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen 'data-type' => clazz.to_s,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen :title => resource.respond_to?(:title) ? resource.title : nil
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen else
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen if block_given?
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen name = yield resource
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen else
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen name = resource
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
7487ff578435377bbeefffdbfb78ca09ed1292dfTimo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen link_to name, resource,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen 'data-type' => clazz.to_s,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen :title => resource.last.respond_to?(:title) ? resource.last.title : nil
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen end
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen def counter_link(url, counter, subject)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen text = content_tag(:strong, counter || '?')
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen text << content_tag(:span, counter==1 ? subject : subject.pluralize)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen link_to text, url
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen end
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen def format_links(*args, &block)
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen options = args.extract_options!
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen args = %w(xml json) if args.empty?
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen args.flatten!
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen options[:url] ||= {}
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen links = ''
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen links << capture(&block) << ' ' if block_given?
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen links << args.collect{ |f|
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen content_tag :li, link_to(f.to_s.upcase, params.merge(options[:url]).merge(:format => f), :title => "Get this page as #{f.upcase}")
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen }.join("")
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen content_tag('ul', links.html_safe, :class => 'formats')
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen end
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenend
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen