Sha256: c330f1bf749a9790871c3975e090d8a199ec4e7df55d1d1b19d8444c6b1c38e3

Contents?: true

Size: 1.79 KB

Versions: 3

Compression:

Stored size: 1.79 KB

Contents

module ActiveRecord
  module Has
    module Permission
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        
        def has_permission(options = {})
          class_eval <<-_DEF
            def self.permission_namespace
              "#{options[:namespace] || 'Permission'}"
            end
          _DEF
          extend ActiveRecord::Has::Permission::SingletonMethods
          include ActiveRecord::Has::Permission::InstanceMethods
        end
        
      end
      
      module SingletonMethods
        
        def with_permission(user)
          permission_class.new :user => user, :object => self
        end
        
        def permission_class
          if respond_to?(:base_class)
            begin
              [permission_namespace, "#{self.to_s}Permission"].join('::').constantize
            rescue
              [permission_namespace ,"#{self.base_class.to_s}Permission"].join('::').constantize
            end
          elsif respond_to?(:proxy_reflection)
            begin
              [permission_namespace, "#{self.proxy_reflection.class_name}Permsission"].join('::').constantize
            rescue
              [permission_namespace, "#{self.proxy_reflection.class_name.constantize.base_class.to_s}Permission"].join('::').constantize
            end
          else
            begin
              [permission_namespace, "#{self.to_s}Permission"].join('::').constantize
            rescue
              [permission_namespace, "#{self.superclass.to_s}Permission"].join('::').constantize
            end
          end
        end
      end
      
      module InstanceMethods
        
        def with_permission(user)
          self.class.permission_class.new :user => user, :object => self
        end
        
      end
      
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
johnsbrn-has_permission-0.1.1 lib/active_record/has/permission.rb
johnsbrn-has_permission-0.1.3 lib/active_record/has/permission.rb
johnsbrn-has_permission-0.1.4 lib/active_record/has/permission.rb