Sha256: cf1ef0c69e38c295eee3936c605e93f912f2a84a57e213be9166ed862996358b
Contents?: true
Size: 1.71 KB
Versions: 9
Compression:
Stored size: 1.71 KB
Contents
module ActsAsLinkable module ActiveRecordExtensions def acts_as_linkable(opts={}, &block) class_eval "def link_attr; #{opts.except(:parent).inspect}; end; cattr_accessor :parent; self.parent = #{opts[:parent].inspect}" end end module ActionViewExtensions def link(obj, options={}, html_options={}) raise "I can't link to nil." unless obj if (obj.class == Array) || (obj.class == ActiveRecord::Relation) obj.map{|v| link(v)}.to_sentence else link_attr = obj.link_attr link_attr[:partial] = "#{obj.class.name.tableize}/link" if link_attr.empty? if link_attr[:partial] obj_name = (link_attr[:object_name] ||= obj.class.name.underscore).to_s.to_sym (render :partial => link_attr[:partial], :locals => {obj_name => obj}).strip else name = obj.send(obj.link_attr[:name]) obj.link_attr.except(:name).each do |key, val| html_options[key] ||= ((val.class==Symbol) ? obj.send(val) : val) end link_obj = options.empty? ? obj : self.send("#{obj.class.name.tableize}_path", obj, options.except(:remote)) link_obj = [link_obj.send(link_obj.parent), link_obj] if link_obj.parent link_to name, link_obj, html_options end end end def tag_list(item, options={}) tags = item.send(:tag_list_on, options[:on] || :tags) url = href(item) tags.map! do |t| link_to t, "#{url}?tag=#{t}" end tags.join(", ") end def href(item) url_for (item.parent ? [item.parent, item] : item) end end end ActiveRecord::Base.extend(ActsAsLinkable::ActiveRecordExtensions) ActionView::Base.send :include, ActsAsLinkable::ActionViewExtensions
Version data entries
9 entries across 9 versions & 1 rubygems