Sha256: cf3c8f015229ab5677eeaa8de0a5c861f6f8fc894fc396b414aaa8de55ccdd3f

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Trestle
  module UrlHelper
    def admin_link_to(content, instance_or_url=nil, options={}, &block)
      if block_given?
        instance_or_url, options = content, instance_or_url || {}
        content = capture(&block)
      end

      if instance_or_url.is_a?(String)
        link_to(content, instance_or_url, options)
      else
        if instance_or_url.is_a?(Hash)
          instance_or_url, options = nil, instance_or_url
        end

        if options.key?(:admin)
          admin = Trestle.lookup(options.delete(:admin))
        elsif instance_or_url.respond_to?(:id)
          admin = admin_for(instance_or_url)
        end

        admin ||= self.admin if respond_to?(:admin)

        if admin
          action = options.delete(:action) || :show

          params = options.delete(:params) || {}
          params[:id] ||= admin.to_param(instance_or_url) if instance_or_url

          if [:show, :edit].include?(action) && admin.form.dialog?
            options[:data] ||= {}
            options[:data][:behavior] ||= "dialog"
          end

          link_to(content, admin.path(action, params), options)
        else
          raise ActionController::UrlGenerationError, "An admin could not be inferred. Please specify an admin using the :admin option."
        end
      end
    end

    def admin_url_for(instance, options={})
      admin = Trestle.lookup(options[:admin]) if options.key?(:admin)
      admin ||= admin_for(instance)

      admin.path(options[:action] || :show, id: admin.to_param(instance)) if admin
    end

    def admin_for(instance)
      Trestle.admins[instance.class.name.underscore.pluralize]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trestle-0.8.6 app/helpers/trestle/url_helper.rb