activeadmin / inherited_resources

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IH with anonymous controller

shlima opened this issue · comments

Hello. Is there a way to use IH with anonymous controller?

Here is controller example:

module Api
  class BaseController < ApplicationController
     inherit_resources
  end
end

Here is my spec:

describe Api::BaseController do

  controller(described_class) do
  end

  it {
     #:stub:
  }
end

Here is backtrace

/Users/alex/.rvm/gems/ruby-2.1.2/gems/inherited_resources-1.4.1/lib/inherited_resources/class_methods.rb:322:in `include?': no implicit conversion of nil into String (TypeError)
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/inherited_resources-1.4.1/lib/inherited_resources/class_methods.rb:322:in `rescue in initialize_resources_class_accessors!'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/inherited_resources-1.4.1/lib/inherited_resources/class_methods.rb:318:in `initialize_resources_class_accessors!'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/inherited_resources-1.4.1/lib/inherited_resources/class_methods.rb:372:in `inherited'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-rails-3.0.0.rc1/lib/rspec/rails/example/controller_example_group.rb:59:in `initialize'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-rails-3.0.0.rc1/lib/rspec/rails/example/controller_example_group.rb:59:in `new'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-rails-3.0.0.rc1/lib/rspec/rails/example/controller_example_group.rb:59:in `controller'
    from /Users/alex/Documents/Development/chat/spec/controllers/api/base_controller_spec.rb:5:in `block in <top (required)>'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.0.rc1/lib/rspec/core/example_group.rb:331:in `module_exec'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.0.rc1/lib/rspec/core/example_group.rb:331:in `subclass'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.0.rc1/lib/rspec/core/example_group.rb:227:in `block in define_example_group_method'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.0.rc1/lib/rspec/core/dsl.rb:41:in `block in expose_example_group_alias'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.0.rc1/lib/rspec/core/dsl.rb:79:in `block (2 levels) in expose_example_group_alias_globally'
    from /Users/alex/Documents/Development/chat/spec/controllers/api/base_controller_spec.rb:3:in `<top (required)>'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.0.rc1/lib/rspec/core/configuration.rb:1051:in `load'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.0.rc1/lib/rspec/core/configuration.rb:1051:in `block in load_spec_files'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.0.rc1/lib/rspec/core/configuration.rb:1051:in `each'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.0.rc1/lib/rspec/core/configuration.rb:1051:in `load_spec_files'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.0.rc1/lib/rspec/core/runner.rb:97:in `setup'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.0.rc1/lib/rspec/core/runner.rb:85:in `run'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.0.rc1/lib/rspec/core/runner.rb:70:in `run'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.0.rc1/lib/rspec/core/runner.rb:38:in `invoke'
    from /Users/alex/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.0.rc1/exe/rspec:4:in `<top (required)>'
    from /Users/alex/.rvm/gems/ruby-2.1.2/bin/rspec:23:in `load'
    from /Users/alex/.rvm/gems/ruby-2.1.2/bin/rspec:23:in `<main>'
    from /Users/alex/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
    from /Users/alex/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'

The only way I've found is to prevent eager load IH class methods for the test environment, and load them in our anonymous controller:

module Api
  class BaseController < ApplicationController
    def self.load_inherited_resource
      inherit_resources
      actions :all, except: %i(new edit)
    end

    # Prevent IH class methods eager load and load them
    # in anonymous controller from specs by hand
    load_inherited_resource unless Rails.env.test?
  end
end

And spec:

require 'spec_helper'

describe Api::BaseController do
  controller do
    load_inherited_resource
  end
end

But this is smell 💩

Please use the mailing list or StackOverflow for questions/help, where a wider community will be able to help you. We reserve the issues tracker for issues only.