ruby-on-railsactiverecordblogsruntime-errordeclarative-authorization

Error when running rails server


I am trying to configure declarative authorization to my rails blog app. After doing all as required, I did rails s to start the server. I have got stuck with the following error.

rails s
/var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/dynamic_matchers.rb:50:in `method_missing': undefined local variable or method `scopes' for ActiveRecord::Base:Class (NameError)
    from /var/lib/gems/1.8/gems/declarative_authorization-0.5.2/lib/declarative_authorization/in_model.rb:37:in `included'
    from /var/lib/gems/1.8/gems/declarative_authorization-0.5.2/lib/declarative_authorization/in_model.rb:36:in `module_eval'
    from /var/lib/gems/1.8/gems/declarative_authorization-0.5.2/lib/declarative_authorization/in_model.rb:36:in `included'
    from /var/lib/gems/1.8/gems/declarative_authorization-0.5.2/lib/declarative_authorization.rb:17:in `include'
    from /var/lib/gems/1.8/gems/declarative_authorization-0.5.2/lib/declarative_authorization.rb:17:in `send'
    from /var/lib/gems/1.8/gems/declarative_authorization-0.5.2/lib/declarative_authorization.rb:17
    from /var/lib/gems/1.8/gems/bundler-1.2.3/lib/bundler/runtime.rb:68:in `require'
    from /var/lib/gems/1.8/gems/bundler-1.2.3/lib/bundler/runtime.rb:68:in `require'
    from /var/lib/gems/1.8/gems/bundler-1.2.3/lib/bundler/runtime.rb:66:in `each'
    from /var/lib/gems/1.8/gems/bundler-1.2.3/lib/bundler/runtime.rb:66:in `require'
    from /var/lib/gems/1.8/gems/bundler-1.2.3/lib/bundler/runtime.rb:55:in `each'
    from /var/lib/gems/1.8/gems/bundler-1.2.3/lib/bundler/runtime.rb:55:in `require'
    from /var/lib/gems/1.8/gems/bundler-1.2.3/lib/bundler.rb:128:in `require'
    from /blogging/config/application.rb:7
    from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands.rb:53:in `require'
    from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands.rb:53
    from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands.rb:50:in `tap'
    from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands.rb:50
    from script/rails:6:in `require'
    from script/rails:6

My blog app is somewhat similar to this blog app.

Kindly help to resolve this issue. Thanks. :)-


Solution

  • EDIT: after your comment about using authlogic. here's another github page with the same error:

    https://github.com/binarylogic/authlogic/issues/316

    replace your authlogic gem code to this:

    gem "authlogic", :git => "git://github.com/binarylogic/authlogic.git"
    

    This is a compatibility error with rails. look at this github page with the error:

    https://github.com/stffn/declarative_authorization/issues/102

    they've fixed it by editing the lib/declarative_authorization/in_model.rb

        def self.included(base) # :nodoc:
           #base.extend(ClassMethods)
           base.module_eval do
    -        scopes[:with_permissions_to] = lambda do |parent_scope, *args|
    -          options = args.last.is_a?(Hash) ? args.pop : {}
    -          privilege = (args[0] || :read).to_sym
    -          privileges = [privilege]
    -          context =
    -              if options[:context]
    -                options[:context]
    -              elsif parent_scope.respond_to?(:proxy_reflection)
    -                parent_scope.proxy_reflection.klass.name.tableize.to_sym
    -              elsif parent_scope.respond_to?(:decl_auth_context)
    -                parent_scope.decl_auth_context
    -              else
    -                parent_scope.name.tableize.to_sym
    -              end
    -          
    -          user = options[:user] || Authorization.current_user
    +        if Rails.version < "3.1"
    +          scopes[:with_permissions_to] = lambda do |parent_scope, *args|
    +            options = args.last.is_a?(Hash) ? args.pop : {}
    +            privilege = (args[0] || :read).to_sym
    +            privileges = [privilege]
    +            context =
    +                if options[:context]
    +                  options[:context]
    +                elsif parent_scope.respond_to?(:proxy_reflection)
    +                  parent_scope.proxy_reflection.klass.name.tableize.to_sym
    +                elsif parent_scope.respond_to?(:decl_auth_context)
    +                  parent_scope.decl_auth_context
    +                else
    +                  parent_scope.name.tableize.to_sym
    +                end
    
    -          engine = options[:engine] || Authorization::Engine.instance
    -          engine.permit!(privileges, :user => user, :skip_attribute_test => true,
    -                         :context => context)
    +            user = options[:user] || Authorization.current_user
    +
    +            engine = options[:engine] || Authorization::Engine.instance
    +            engine.permit!(privileges, :user => user, :skip_attribute_test => true,
    +                           :context => context)
    
    -          obligation_scope_for( privileges, :user => user,
    -              :context => context, :engine => engine, :model => parent_scope)
    +            obligation_scope_for( privileges, :user => user,
    +                :context => context, :engine => engine, :model => parent_scope)
    +          end
             end
    
             # Builds and returns a scope with joins and conditions satisfying all obligations.
    @@ -96,7 +98,32 @@ def self.obligation_scope_for( privileges, options = {} )
             #   current user.
             #
             def self.with_permissions_to (*args)
    -          scopes[:with_permissions_to].call(self, *args)
    +          if Rails.version < "3.1"
    +            scopes[:with_permissions_to].call(self, *args)
    +          else
    +            options = args.last.is_a?(Hash) ? args.pop : {}
    +            privilege = (args[0] || :read).to_sym
    +            privileges = [privilege]
    +
    +            parent_scope = scoped
    +            context =
    +                if options[:context]
    +                  options[:context]
    +                elsif parent_scope.klass.respond_to?(:decl_auth_context)
    +                  parent_scope.klass.decl_auth_context
    +                else
    +                  parent_scope.klass.name.tableize.to_sym
    +                end
    +
    +            user = options[:user] || Authorization.current_user
    +
    +            engine = options[:engine] || Authorization::Engine.instance
    +            engine.permit!(privileges, :user => user, :skip_attribute_test => true,
    +                           :context => context)
    +
    +            obligation_scope_for( privileges, :user => user,
    +                :context => context, :engine => engine, :model => parent_scope.klass)
    +          end
             end
    
             # Activates model security for the current model.  Then, CRUD operations
    

    and the test/test_helper.rb file:

         map.connect ':controller/:action/:id'
       end
     else
    -  Rails::Application.routes.draw do
    +  #Rails::Application.routes.draw do
    +  Rails.application.routes.draw do
         match '/name/spaced_things(/:action)' => 'name/spaced_things'
         match '/deep/name_spaced/things(/:action)' => 'deep/name_spaced/things'
         match '/:controller(/:action(/:id))'
    @@ -146,7 +147,8 @@ def request! (user, action, reader, params = {})
    
       unless Rails.version < "3"
         def setup
    -      @routes = Rails::Application.routes
    +      #@routes = Rails::Application.routes
    +      @routes = Rails.application.routes
         end
       end
     end