How to call helper method of parent application from rails 3.1 engine

I am creating a rails engine that uses the "acts as" format to establish relationships with the parent application. User model.

module Cornerstone

  module ActsAsCornerstoneUser

    extend ActiveSupport::Concern

    module ClassMethods

      def acts_as_cornerstone_user(options = {})

        #= Associations
        has_many :cornerstone_discussions


        #= Options
        Cornerstone::Config.auth_with << options[:auth_with] if options[:auth_with]
        Cornerstone::Config.auth_with.flatten!

      end
    end

    module InstanceMethods

    end

  end

  ActiveRecord::Base.send :include, ActsAsCornerstoneUser

end

I would like the developer to be able to specify the name of the helper method using the option :auth_with. The idea is that the developer will specify a helper method in the parent application that will return the signed-in user for this session.

My question is, as soon as the developer specified the parameter auth_with, how can I call this method of the parent application?

, ? , , , current_user.

+1
1

- , - :

module Cornerstone
  module ActsAsCornerstoneUser
    extend ActiveSupport::Concern

    module ClassMethods
      def acts_as_cornerstone_user(options = {})

        #= Associations
        has_many :cornerstone_discussions

        #= Options
        Cornerstone::Config.auth_with = options[:auth_with] if options[:auth_with]
      end
    end

    module InstanceMethods

    end

    def self.included(base)
      base.extend(ClassMethods)
      base.include(InstanceMethods)
    end
  end

  ActiveRecord::Base.send :include, ActsAsCornerstoneUser
end

(.. app/helpers/cornerstone_helper.rb):

module Cornerstone
  module CornerStoneHelper
    def current_cornerstone_user
      Config.auth_with.call(controller)
    end
  end
end

acts_as_cornerstone :

class MyUser < ActiveRecord::Base
  acts_as_cornerstone_user :auth_with => Proc.new { |controller| controller.current_user }
end

current_cornerstone_user , .

, acts_as_cornerstone_user . , ( ).

, :auth_with => :warden, :

module Cornerstone
  module CornerStoneHelper
    def current_cornerstone_user
      if Config.auth_with.respond_to?(:call)
        Config.auth_with.call(controller)
      elsif Config::AUTH_MODES.keys.include?(Config.auth_with)
        Config::AUTH_MODES[Config.auth_with].call(controller)
      end
    end
  end
end

Cornerstone::Config::AUTH_MODES :

module Cornerstone
  class Config
    AUTH_MODES = {
      :warden => Proc.new { |controller| controller.env['warden'].user },
      :devise => Proc.new { |controller| controller.current_user }
    }
  end
end
+2

All Articles