Sha256: d5f2d41cc085d04b6c90c3535f2377f70bef61f98304e6cb99dfeeb3c66ed59d

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

module AuthHelpers
  module Controller
    module Helpers

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

      module ClassMethods
        protected

          # Writes the inherited hook for the included class based on its name.
          #
          def inherited(base) #:nodoc:
            super

            base.send :cattr_accessor, :resource_class, :instance_name, :route_name, :instance_writter => false

            # Converts:
            #
            #   Mockable::ConfirmationsController #=> mockable
            #   MockablePasswordsController       #=> mockable
            #
            resource = base.controller_path.gsub('/', '_').split('_')[0..-2].join('_')

            begin
              base.resource_class = resource.classify.constantize
              base.route_name     = resource.singularize
              base.instance_name  = resource.singularize
            rescue NameError
              nil
            end
          end

          def set_class_accessors_with_class(klass)
            self.resource_class = klass
            self.instance_name  = klass.name.downcase
            self.route_name     = klass.name.downcase
          end
      end

      protected

        # If a block is given, redirect to the url in the block, otherwise
        # try to call the url given by scope, for example:
        #
        #   new_account_session_url
        #   new_account_password_url
        #
        def redirect_to_block_or_scope_to(redirect_block, scope) #:nodoc:
          redirect_to redirect_block ? redirect_block.call : send("new_#{self.route_name}_#{scope}_url")
        end

        # Try to get the instance variable, otherwise send the args given to
        # the resource class and store the result in the same instance variable.
        #
        def get_or_set_with_send(*args) #:nodoc:
          instance_variable_get("@#{self.instance_name}") || instance_variable_set("@#{self.instance_name}", resource_class.send(*args))
        end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
josevalim-auth_helpers-0.3.0 lib/auth_helpers/controller/helpers.rb
josevalim-auth_helpers-0.3.1 lib/auth_helpers/controller/helpers.rb
josevalim-auth_helpers-0.3.2 lib/auth_helpers/controller/helpers.rb
josevalim-auth_helpers-0.4.1 lib/auth_helpers/controller/helpers.rb