Sha256: 72a23a43b196558ad3c86384de668ad6885561f30cc492ded04e4a593dd359af

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

module Authlogic
  module Session
    # Authlogic looks like ActiveRecord, sounds like ActiveRecord, but its not ActiveRecord. That's the goal here.
    # This is useful for the various rails helper methods such as form_for, error_messages_for, or any method that
    # expects an ActiveRecord object. The point is to disguise the object as an ActiveRecord object so we can take
    # advantage of the many ActiveRecord tools.
    module ActiveRecordTrickery
      def self.included(klass)
        klass.extend ClassMethods
        klass.send(:include, InstanceMethods)
      end
      
      module ClassMethods
        # How to name the attributes of Authlogic, works JUST LIKE ActiveRecord, but instead it uses the following
        # namespace:
        #
        #   authlogic.attributes.user_session.login
        def human_attribute_name(attribute_key_name, options = {})
          options[:count] ||= 1
          options[:default] ||= attribute_key_name.to_s.humanize
          I18n.t("attributes.#{name.underscore}.#{attribute_key_name}", options)
        end
        
        # How to name the class, works JUST LIKE ActiveRecord, except it uses the following namespace:
        #
        #   authlogic.models.user_session
        def human_name(*args)
          I18n.t("models.#{name.underscore}", {:count => 1, :default => name.humanize})
        end
        
        # For rails < 2.3, mispelled
        def self_and_descendents_from_active_record
          [self]
        end
        
        # For rails >2.3, fix mispelling
        def self_and_descendants_from_active_record
          [self]
        end
        
        # For rails >3.0
        def self.model_name
          ActiveModel::Name.new(self.to_s)
        end
      end
      
      module InstanceMethods
        # Don't use this yourself, this is to just trick some of the helpers since this is the method it calls.
        def new_record?
          new_session?
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kschrader-authlogic-2.1.2 lib/authlogic/session/active_record_trickery.rb
kschrader-authlogic-2.1.3 lib/authlogic/session/active_record_trickery.rb