Sha256: 920bc3da8a816c3007edce2c3696e4f89fec86e2cebba3a0657543cc253dadb7

Contents?: true

Size: 997 Bytes

Versions: 7

Compression:

Stored size: 997 Bytes

Contents

module SocialStream
  module Controllers
    # Common methods added to ApplicationController
    module Helpers
      extend ActiveSupport::Concern

      included do
        helper_method :current_subject
      end

      module InstanceMethods
        # Current subject represented by the user. Defaults to the own user
        def current_subject
          @current_subject ||=
            current_subject_from_session ||
              current_user
        end

        # Set represented subject
        def current_subject= instance
          session[:subject_type] = instance.class.to_s
          session[:subject_id]   = instance.id

          @current_subject = instance
        end

        private

        # Get represented subject from session
        def current_subject_from_session
          return unless session[:subject_type].present? && session[:subject_id].present?

          session[:subject_type].constantize.find session[:subject_id]
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
social_stream-0.4.1 lib/social_stream/controllers/helpers.rb
social_stream-0.4.0 lib/social_stream/controllers/helpers.rb
social_stream-0.3.6 lib/social_stream/controllers/helpers.rb
social_stream-0.3.5 lib/social_stream/controllers/helpers.rb
social_stream-0.3.4 lib/social_stream/controllers/helpers.rb
social_stream-0.3.3 lib/social_stream/controllers/helpers.rb
social_stream-0.3.2 lib/social_stream/controllers/helpers.rb