How to hide the resource in activeadmin gem - ruby-on-rails-3

I create an admin page by using activeadmin gem http://activeadmin.info/
I use cancan to authorize the privilege for 2 kind of user, user manage and book manage user.
My question is: How I hide the resource User when a book manage user log in to admin page?
I tried something like that but it didn't work
menu false if can? :manage, BookHeader
or
menu false if authorize! :manage, BookHeader
Thanks for your help!

You have to use proc for build menu dynamicly
Examples
dynamic label
menu :label => proc{current_admin_user.admin? ? "Accounts" : "My Account"}
Display/Hide
menu :if => proc{ can?(:manage, BookHeader ) }
For more info about integrating AA and CanCan read this article
https://github.com/gregbell/active_admin/wiki/How-to-work-with-cancan

i use this:
for me was users for adminuser,
password for none admin user
menu label: proc{I18n.t "#{current_user.is_admin? ? 'users':'password'}"}
Following your example
menu label: proc{I18n.t "#{current_admin_user.admin? ? 'Accounts' : 'My Account'}"}
will help

Related

Conditionally change resource name Activeadmin

I'd like to conditionally change the label of a resource in ActiveAdmin based on the user role. I'd like to keep the default pluralization of the name for admins, but for regular users, I need to change the name.
I.e. I have a resource Users, which I would like to change to My Account for the regular user (since they won't see #index).
I'm trying something like
ActiveAdmin.register User do
menu :if => proc {if !current_user.admin?
menu :label => "My Account"
else
menu :label => "I hate Users"
end}
Anyone know how to conditionally name the resource?
Thanks!
This works:
menu :label => proc { true ? "I Hate Users" : "My Account" }
But then to access the
current_admin_user
or the
current_user
object from within the proc won't work. I haven't found a way to get the logged in user object from within the ActiveAdmin::MenuBuilder scope. There are suggestions, see e.g. https://stackoverflow.com/a/2513456/790737 where you set a variable in
Thread.current
after succesfull login. I guess you will have to hook in to the post-authentication work of devise. Good luck.

Cancan is not working well with active_admin

I have implemented the cancan with active_admin using below link.
https://github.com/gregbell/active_admin/wiki/How-to-work-with-cancan
Just in my case the only change is below written code.
app/models/ability.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= AdminUser.new # guest user (not logged in)
if user.id == 1
can :manage, :all
puts ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> manage all"
else
can :read, :all
puts ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> read all"
end
end
end
For now just put condition on user.id.
so when i run my application i can see my puts are on right login.
Question:
so if i login with user whos user.id != 1 then also i can manage all my modules in active_admin.
cancan ability not working for me.
(In short my can :code isn't working in any condition)
Using rails 3.1.1, cancan 1.6.7, activeadmin 0.4.4, ruby 1.9.3
Followed commands in link correctly, double checked.
Used authorize_resource in AdminUser.
Using socery not devise, does this thing affecting the cancan?
I write the following in code in every model in /admin
then my conditions in ability model start working.
menu :if => proc{ can?(:manage, #ModelName) }
controller.authorize_resource
Before posting this question i just written the above code only in admin users

Activeadmin disabling the "new resource" method

I'm using Activeadmin for the admin interface on an app I'm working on (loving it) and I am curious if there is a way to disable the "New Resource" link in the upper-right corner of the resource show page?
The particular resource I'm using is nested inside another resource and I have a partial that allows it to be created from the show page on that parent resource.
I have disabled the resource in the menu, but I'd rather leave the resource in the menu so I can see/edit/delete those resources without having to find it by looking through its parent resource.
Previous solution didn`t work for me, so here is general solutions, that works always:
ActiveAdmin.register Book do
actions :index
#or like that
#actions :all, :except => [:destroy]
index do
column :title
column :author
end
end
Try config.clear_action_items! to remove the link to New and other links on top of the table
This removed the "New Resource" button from the top-right:
config.clear_action_items!
This removed both the "New Resource" button as well as the box "There are no resources yet - create one".
actions :all, :except => [:new]
Thank you, Irio
config.clear_action_items!
Will remove all the actions.
If you only want to remove the new action link you can also use:
config.remove_action_item(:new)
I know this is an old question, but I just came up to it (had the same problem), and realized that config.clear_action_items! and actions :all, :except => [:new] are fundamentally different.
config.clear_action_items! will remove the New button from the index page, while actions :all, :except => [:new] will remove both the button, AND the route, meaning you can't call it from another place (which, in my case, is needed).
I did this:
controller do
def action_methods
if some_condition
super
else
super - ['new', 'create', 'destroy']
end
end
end
To disable some of the possible actions. action_methods returns an array of the 7 standard CRUD actions, so you can subtract those you don’t want
Or even:
ActiveAdmin.register Purchase do
config.clear_action_items!
actions :index
end
Worked for me too ! :-)
ActiveAdmin.register AssetSumView do
menu :label => "Asset Summary View", :parent => "Things"
# no button for NEW (since this is a db view)
#---------------------------------------------------------------------------------------------
config.clear_action_items!
enter code here
action_item do
link_to "Assets" , "/admin/assets"
end
action_item do
link_to "AssetCatgCodes", "/admin/asset_catg_codes"
end
#---------------------------------------------------------------------------------------------
config.clear_action_items! does only half of the job. There is one issue though.
In case of empty index table, active admin show this message
There are no [Resources] yet. Create one
which doesn't get hidden by the above command and I don't want to entirely disable the action. So, I kept the link and edited the new action to redirect to the parent resource index with a message.
controller do
def new
if params[:parent_id].present?
super
else
redirect_to parent_resources_path, notice: "Create Resource through ParentResource"
end
end
end

Few questions about capybara

I've got a few questions about Capybara. And I might as well ask here since the RDOC in the github page for Capybara is great to get it set up and running. But where is the API or list of available methods??
First. Per *_spec.rb file, should scenario only exist once? Or is it fine to have multiple scenario's in one file?
For example, in spec/request/user_spec.rb:
require 'spec_helper'
feature 'User actions' do
background do
data = {
:first_name => 'foo',
:last_name => 'bar',
...
}
user = User.new(data, :as => :user)
user.save
end
scenario 'User can browse home page' do
visit root_path
page.should have_content('Homepage')
end
scenario 'User should not be able to visit the dashboard' do
visit dashboard_root_path
page.should have_content('You are not authorized to access this page.')
end
end
If there is anything wrong with the code structure above, or if there is room for improvement. I am open feedback.
Second. I notice with the code above. If I have config.use_transactional_fixtures = false in spec/spec_helper.rb, it saves the user twice. This means, in my test database / user table, I would have 2 users named 'foo bar'. Is this normal?
Third. I have a form that has an HTML button. When user clicks on this button, jQuery submits the form. How would I test this with Capybara? I don't think click_button "Add" will do the trick.
Fourth. How would I sign in users in Capybara? I am using Devise. Would sign_in User.first do the trick? And would I be able to access current_user in Capybara?
Lastly, if anyone knows any "Getting Started" guides / tutorials on Rspec + Capybara. Please do mention.
I've also switched over to writing request specs ever since i decided that I was no longer liking Cucumber.
ONE) Having multiple scenarios is indeed fine. You get to use all the other great features of rspec, so I would suggest also using contexts as in the code at the bottom.
TWO) This can probably be solved by using the Rspec Set Gem And the Database Cleaner Gem. Also: The Original Rationale for Set
Warning: make sure you set up DatabaseCleaner correctly when you use set. My own setup (which may be a little overkill but is working for me):
config.before(:suite) do
DatabaseCleaner.clean_with :truncation
end
config.before(:all) do
DatabaseCleaner.clean_with :truncation
end
config.after(:all) do
DatabaseCleaner.clean_with :truncation
end
config.after(:suite) do
DatabaseCleaner.clean_with :truncation
end
THREE) yep! click_button "Add" should work! The complete capybara API is useful but took me a while to grok. Of most relevant importance are the actions and rspec matchers.
example:
click_button "Add"
page.should have_content("Successfully Added")
you can narrow the scope with element finders.
FOURTH) Devise provides helpers. there is a sign_in helper. read the dox :). Here's a demo:
feature 'User actions' do
background do
data = {
:first_name => 'foo',
:last_name => 'bar',
...
}
#user = User.new(data, :as => :user)
#user.save
end
context "no user is signed in" do
scenario 'User can browse home page' do
visit root_path
page.should have_content('Homepage')
end
scenario 'User should not be able to visit the dashboard' do
visit dashboard_root_path
page.should have_content('You are not authorized to access this page.')
end
end
context "user is signed in" do
before :each do
sign_in #user
end
[more scenarios]
end
end
ultimately of course, you'd prolly want to split this up into more specific features. Probably have a "Public Navigation" Feature for all the tests that are about guests seeing content, and then a separate feature for a user signing in, etc.
I am not aware of capybara, but a full list of available methods can be found here:
http://rubydoc.info/github/jnicklas/capybara/master#
hope, that helps

How to create the first (Admin) user (CanCan and Devise)?

I made authentication in my Rails 3 app fallowed by Tony's tutorial
I don't want public registrations on my app, just to create new users with Admin account, but I can't create Admin account manually, because in table Users there is encrypted password and salt that must to be generated, and I don't know how :|
You can do it from the rails console. From the command line goto the directory of your rails application and type rails console. Then enter the following code to create a user:
user=User.create!(:email=>'test#test.com',:username=>'test',:password=>'password')
This will create a user object (assuming your devise resource is called User). Now you can use the user object that you just created to set admin privileges.
I am current something like this (your details may be different) in my seeds.rb file to create my admin user for Devise.
User.new({ :email => 'admin#example.com', :password => 'password', :password_confirmation => 'password'}).save
You can execute it using rake db:seed in the terminal window.
In addition, if you are using confirmable and want to skip the requirement for a confirmation email when creating new accounts you can do something like this:
newuser = User.new({ :email => 'admin#example.com',
:password => 'password',
:password_confirmation => 'password'})
newuser.skip_confirmation!
newuser.save
This is useful if the accounts you are creating are for trusted users or if you are creating test accounts.