- , - :
module Cornerstone
module ActsAsCornerstoneUser
extend ActiveSupport::Concern
module ClassMethods
def acts_as_cornerstone_user(options = {})
has_many :cornerstone_discussions
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