Deep integration with CanCan | James McKinney | 7/5/12 7:52 AM | I added the following content to this wiki page: https://github.com/gregbell/active_admin/wiki/How-to-work-with-cancan
If you want deep integration between CanCan and ActiveAdmin, put this gist (https://gist.github.com/3040303) in a Rails initializer. What it will do for you: • It will properly load and authorize all your resources. • Menus will only appear if the current user has the ability to access those resources. • On the index page, only the actions (links in rightmost column) that the current user has the ability to perform will appear. • On all resource pages, only the action items (buttons in top-right) that the current user has the ability to perform will appear. • If you use ActiveAdmin's auto_link helper, it will only create a link if the current user has the ability to view that resource. |
Re: Deep integration with CanCan | Awea | 7/5/12 7:54 AM | Many thanks :) |
Re: Deep integration with CanCan | Joseh-Henrique Caetano de Brito e Silva | 7/6/12 2:04 PM | Gave error when using. Check and see that is the version I'm using 0.4.4 Activeadmin, summarizing it did not work :-( Sorry for my English |
Re: [Active Admin] Deep integration with CanCan | James McKinney | 7/6/12 2:54 PM | What error did it give you? You still need to set up CanCan first to use this initializer. 1. Add CanCan to your Gemfile gem 'cancan' 2. Generate and edit an Ability class rails generate cancan:ability 3. Add this code to your Application Controller rescue_from CanCan::AccessDenied do |exception| respond_to do |format| format.html do redirect_to admin_root_path, :alert => exception.message end end end def current_ability @current_ability ||= Ability.new(current_admin_user) end |
Re: [Active Admin] Deep integration with CanCan | James McKinney | 7/6/12 3:37 PM | What error did it give you?
|
Re: [Active Admin] Deep integration with CanCan | Joseh-Henrique Caetano de Brito e Silva | 7/7/12 6:47 AM |
James the error that appears is this: C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:234:in `load': C:/Users/joseh/dev/sites/rails/arquivo/config/initializers/activeadmin-cancan.rb:68: syntax error, unexpected kDO, expecting kEND (SyntaxError) C:/Users/joseh/dev/sites/rails/arquivo/config/initializers/activeadmin-cancan.rb:75: syntax error, unexpected kDO, expecting kEND C:/Users/joseh/dev/sites/rails/arquivo/config/initializers/activeadmin-cancan.rb:82: syntax error, unexpected kDO, expecting kEND C:/Users/joseh/dev/sites/rails/arquivo/config/initializers/activeadmin-cancan.rb:89: syntax error, unexpected kEND, expecting $end The legacy code is different from the version 0.4.4 here. Comparing the code: activeadmin-0.4.4\lib\active_admin\resource\action_items.rb def add_default_action_items # New Link on all actions except :new and :show add_action_item :except => [:new, :show] do if controller.action_methods.include?('new') link_to(I18n.t('active_admin.new_model', :model => active_admin_config.resource_name), new_resource_path) ... You code arquivo\config\initializers\activeadmin-cancan.rb def add_default_action_items # New Link on all actions except :new and :show add_action_item :except => [:new, :show], :if => proc{ can? :create, active_admin_config.resource_class } do if controller.action_methods.include?('new') link_to(I18n.t('active_admin.new_model', :model => active_admin_config.resource_label), new_resource_path) ...
From the beginning I've used the cancan, just wanted more control (automatic) to define the actions. I had the gem activeadmin-cancan https://github.com/11factory/activeadmin-cancan cancan. I disabled it to use what you created.
Ok. already exist
my code redirect_to dashboard_path, :alert => exception.message end
ok. Already exist |
Re: [Active Admin] Deep integration with CanCan | James McKinney | 7/7/12 8:02 AM | Those errors are from the Ruby parser. I had only tested in Ruby 1.9. I added parentheses and it should work now in Ruby 1.8: https://gist.github.com/3040303 And yes, the code is different, because I'm making changes to the code to integrate with CanCan! It's important to remove https://github.com/11factory/activeadmin-cancan like you did. |
Re: [Active Admin] Deep integration with CanCan | Joseh-Henrique Caetano de Brito e Silva | 7/7/12 9:35 AM | How nice that our conversation is progressing :-) After updating the code done for you, given some access to the application after login. But there's always a but, NameError (uninitialized constant Dashboard): Rendered C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms) Rendered C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms) Rendered C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (21.0ms) My versions rails 3.1.6 activeadmin 0.4.4 ruby 1.8.7 |
Re: [Active Admin] Deep integration with CanCan | James McKinney | 7/7/12 9:44 AM | Hmm, my code doesn't refer to Dashboard. Not sure what's happening there. Is that the full stack trace? What's the URL path where that happens? |
Re: [Active Admin] Deep integration with CanCan | Joseh-Henrique Caetano de Brito e Silva | 7/7/12 9:54 AM | Bellow - Full Trace
|
Re: [Active Admin] Deep integration with CanCan | James McKinney | 7/7/12 10:07 AM | And what's the URL path/what page are you trying to load? |
Re: [Active Admin] Deep integration with CanCan | Joseh-Henrique Caetano de Brito e Silva | 7/7/12 10:19 AM | The activeadmin is the main application, after accessing the login, the system falls into the Dashboard, normal pattern of activeadmin. It is as it should be, the home screen after login to access -> Dashboard http://localhost:3000/users/login (Login main access) after login http://localhost:3000 (Dashboard) Remember C:\Users\joseh\dev\sites\rails\arquivo\config\initializers\active_admin.rb .. config.default_namespace = false .. |
Re: [Active Admin] Deep integration with CanCan | James McKinney | 7/7/12 10:24 AM | Not sure why it's not working. If you remove the initializer, does it work? |
Re: [Active Admin] Deep integration with CanCan | Joseh-Henrique Caetano de Brito e Silva | 7/7/12 10:31 AM | Yes, normally |
Re: [Active Admin] Deep integration with CanCan | James McKinney | 7/7/12 10:44 AM | Try adding this to your dashboard.rb file inside the main block controller.skip_authorize_resource :only => :index |
Re: [Active Admin] Deep integration with CanCan | Joseh-Henrique Caetano de Brito e Silva | 7/7/12 10:53 AM | Err, ActiveAdmin::Dashboards.build do controller.skip_authorize_resource :only => :index ... Error controller.skip_authorize_resource :only => :index C:/Users/joseh/dev/sites/rails/arquivo/app/admin/dashboards.rb:3: undefined local variable or method `controller' for ActiveAdmin::Dashboards:Module (NameError) from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/dashboards.rb:24:in `module_eval' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/dashboards.rb:24:in `build' from C:/Users/joseh/dev/sites/rails/arquivo/app/admin/dashboards.rb:1 Remember: gem 'activeadmin', '0.4.4' |
Re: [Active Admin] Deep integration with CanCan | James McKinney | 7/7/12 11:06 AM | Ah, you're using the old, deprecated ActiveAdmin dashboards class. You need to switch to new style of using pages instead, e.g. ActiveAdmin.register_page 'Dashboard' do ... end And for pages you don't need to add that skip_authorize_resource line. |
Re: [Active Admin] Deep integration with CanCan | Joseh-Henrique Caetano de Brito e Silva | 7/7/12 11:20 AM |
C:\Users\joseh>gem list | grep activeadmin File STDIN: activeadmin (0.4.4) My GemFile source 'http://rubygems.org' gem 'rails', '3.1.6' gem 'activeadmin', '0.4.4' gem "meta_search", '>= 1.1.0.pre' gem 'json' # workaround gem 'execjs' gem 'therubyracer', :platform => :ruby gem 'sass-rails', '~> 3.1.5' group :assets do gem 'coffee-rails', '~> 3.1.1' gem 'uglifier', '>= 1.0.3' end gem 'jquery-rails' gem 'delocalize' gem 'cancan' #gem 'activeadmin-cancan', '0.1.2' gem 'tlsmail' group :development, :test do gem 'mysql2' gem 'annotate' gem 'quiet_assets' end group :production do gem 'rubyfb' end Excuse me, I may be wrong, but the version I use is ActiveAdmin gem 0.4.4 and not the GitHub, see the error below. => Ctrl-C to shutdown server C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/namespace.rb:140:in `register_page_controller': (eval):1:in `register_page_controller': superclass mismatch for class DashboardController (TypeError) from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/namespace.rb:71:in `eval' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/namespace.rb:140:in `register_page_controller' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/namespace.rb:71:in `register_page' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/application.rb:128:in `register_page' from C:0:in `__send__' from C:0:in `register_page' from C:/Users/joseh/dev/sites/rails/arquivo/app/admin/dashboards.rb:2 from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:234:in `load' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:234:in `load' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:223:in `load_dependency' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:640:in `new_constants_in' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:223:in `load_dependency' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:234:in `load' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/application.rb:161:in `load!' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/application.rb:161:in `each' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/application.rb:161:in `load!' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/application.rb:187:in `routes' from C:0:in `__send__' from C:0:in `routes' from C:/Users/joseh/dev/sites/rails/arquivo/config/routes.rb:11 from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.6/lib/action_dispatch/routing/route_set.rb:264:in `instance_exec' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.6/lib/action_dispatch/routing/route_set.rb:264:in `eval_block' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.6/lib/action_dispatch/routing/route_set.rb:241:in `draw' from C:/Users/joseh/dev/sites/rails/arquivo/config/routes.rb:1 from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:234:in `load' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:234:in `load' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:223:in `load_dependency' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:640:in `new_constants_in' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:223:in `load_dependency' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:234:in `load' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.1.6/lib/rails/application/routes_reloader.rb:29:in `load_paths' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.1.6/lib/rails/application/routes_reloader.rb:29:in `each' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.1.6/lib/rails/application/routes_reloader.rb:29:in `load_paths' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.1.6/lib/rails/application/routes_reloader.rb:13:in `reload!' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.1.6/lib/rails/application.rb:87:in `reload_routes!' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/reloader.rb:34:in `reload!' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/reloader.rb:97:in `reload!' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/reloader.rb:92:in `initialize' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/file_update_checker.rb:32:in `call' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/file_update_checker.rb:32:in `execute_if_updated' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/reloader.rb:83:in `execute_if_updated' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activeadmin-0.4.4/lib/active_admin/reloader.rb:109:in `_callback_before_17' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/callbacks.rb:404:in `_run_prepare_callbacks' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/callbacks.rb:81:in `send' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/callbacks.rb:81:in `run_callbacks' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/actionpack-3.1.6/lib/action_dispatch/middleware/reloader.rb:46:in `prepare!' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.1.6/lib/rails/application/finisher.rb:41 from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.1.6/lib/rails/initializable.rb:30:in `instance_exec' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.1.6/lib/rails/initializable.rb:30:in `run' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.1.6/lib/rails/initializable.rb:55:in `run_initializers' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.1.6/lib/rails/initializable.rb:54:in `each' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.1.6/lib/rails/initializable.rb:54:in `run_initializers' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.1.6/lib/rails/application.rb:96:in `initialize!' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.1.6/lib/rails/railtie/configurable.rb:30:in `send' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/railties-3.1.6/lib/rails/railtie/configurable.rb:30:in `method_missing' from C:/Users/joseh/dev/sites/rails/arquivo/config/environment.rb:5 from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:240:in `require' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:240:in `require' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:223:in `load_dependency' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:640:in `new_constants_in' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:223:in `load_dependency' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/activesupport-3.1.6/lib/active_support/dependencies.rb:240:in `require' from C:/Users/joseh/dev/sites/rails/arquivo/config.ru:4 from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rack-1.3.6/lib/rack/builder.rb:51:in `instance_eval' from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/rack-1.3.6/lib/rack/builder.rb:51:in `initialize' from C:/Users/joseh/dev/sites/rails/arquivo/config.ru:1:in `new' from C:/Users/joseh/dev/sites/rails/arquivo/config.ru:1 Exiting Process finished with exit code 1 |
Re: [Active Admin] Deep integration with CanCan | James McKinney | 7/7/12 11:31 AM | You may have to upgrade to: gem 'activeadmin', :git => 'https://github.com/gregbell/active_admin.git' |
Re: [Active Admin] Deep integration with CanCan | Joseh-Henrique Caetano de Brito e Silva | 7/7/12 11:42 AM | I agree with you in upgrading to the development version on GitHub, but what about the other users? Who want to use your tip and are using the gem 0.4.4, will have a problem. In this case you'd better fix the tip on the Wiki. Actually, I would use your tip, with the gem installed and stable. For even expect Grebell put ActiveAdmin updated with more features and clear, stable, looks like it will take. :-( Meanwhile, it will update and I'll be without your great tip. :-( |
Re: [Active Admin] Deep integration with CanCan | James McKinney | 7/7/12 12:03 PM | I'm not sure I understand. 0.4.4 is two months old. ActiveAdmin has currently been publishing point-releases every few months, so I'm sure there will be a 0.4.5 soon. The gist will work with 0.4.5. Anyway, the error may not even have anything to do with my gist. I'd have to see ALL your code to debug it properly, or you can try to debug it yourself. |
Re: [Active Admin] Deep integration with CanCan | Joseh-Henrique Caetano de Brito e Silva | 7/7/12 12:07 PM | Thanks for your reply. Do you understand my bad English. :-) Thanks again |