randym / activeadmin-axlsx

ActiveAdmin plugin using Axlsx for adding Excel (xlsx) download links for your resources
MIT License
82 stars 134 forks source link

Break rails custom helpers #14

Closed ryancheung closed 11 years ago

ryancheung commented 11 years ago

I got a register_page Dashboard:

ActiveAdmin.register_page "Dashboard" do
  content :title => proc{ I18n.t("active_admin.dashboard")  } do
    columns do
      column do
        panel "最近进来的订单" do
          table_for Order.order('id desc').limit(10) do
            column("状态")  { |order| status_tag t('models.order.state.' + order.state), order_state(order) }
            ...
          end
        end
      end
    end
  end
end

The order_state is a custom helper in app/helpers/orders_helper.rb. It would not work after I added this gem to Gemfile. I checked the activeadmin repo and found it's use method_missing to find a helper, but It just can't find out the helper from @helpers in pry debugging. More seriously, it cause infinit loop, which means the server stucked and cpu/memory is in high usage.

ryancheung commented 11 years ago

I manually added the help to the controller and then the issue is gone.

baxang commented 11 years ago

I have a looking similar issue here.

After defining gem 'activeadmin-axlsx' in Gemfile, my Rails application's controllers can't use custom helpers which previously worked well. I tried helper :all in ApplicationController that all other controllers inherit, it didn't work and I had to specify helpers I needed one by one.

I don't know much about how helpers are included, but it looks that activeadmin-axlsx gem affects the mechanism.

Maybe re-open this issue?

SteveLTN commented 11 years ago

I have also met this problem, and I tried to solve it, but unfortunately I failed.

So here's what I have done:

First, I cloned this gem into my local filesystem, and used my local version of this gem in my Gemfile. Since I don't really understand what's happening here, I started to comment out lines, in order to locate the problematic line of code.

So, I've found it is this line that causes the problem: Once I commented this line out, my helpers are automatically loaded (Though this gem did not work, I suppose).

Since it mixes a module into ActiveAdmin::ResourceController, I dug into this line, which lead me to resource_controller_extension.rb, and I commented these lines out, which made ActiveAdmin::Axlsx::ResourceControllerExtension an empty module. But my helpers are still not loaded!

In order to make sure that nothing in somewhere else magically adds stuff into the module, I changed the file into this:

# require 'active_admin'
# require 'active_admin/axlsx/version'
# require 'active_admin/axlsx/builder'
# require 'active_admin/axlsx/dsl'
# require 'active_admin/axlsx/resource_extension'
require 'active_admin/axlsx/resource_controller_extension'

class Railtie < ::Rails::Railtie
  config.before_initialize do
    # begin
    #   if Mime::Type.lookup_by_extension(:xlsx).nil?
    #     # The mime type to be used in respond_to |format| style web-services in rails
    #     Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx
    #   end
    # rescue NameError
    #   # noop
    # end

    # ActiveAdmin::ResourceDSL.send :include, ActiveAdmin::Axlsx::DSL
    # ActiveAdmin::Resource.send :include, ActiveAdmin::Axlsx::ResourceExtension

    # Problematic Line
    # ActiveAdmin::ResourceController.send :include, ActiveAdmin::Axlsx::ResourceControllerExtension
    module Foobar
    end

    ActiveAdmin::ResourceController.send :include, Foobar

    # ActiveAdmin::Views::PaginatedCollection.add_format :xlsx
  end
end

and it will still prevent my helpers from auto-loading! Once I comment ActiveAdmin::ResourceController.send :include, Foobar out, my helpers would just be back, magically.

I tried to mix an empty module in in an initializer in my Rails application, it won't affect anything.

The rest of this problem is beyond my knowledge, does anyone have any ideas?

SteveLTN commented 11 years ago

Fixed it in this pull-request.