Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a scope just for some actions on ActiveAdmin

I have a classic posts-belongs-to-user association. I want to apply a default_scope to all the actions like #index so it lists my posts only. But I want the ability to see posts from anybody when I go to the #show action if follow a link to it.

How can I avoid the default_scope to be applied on that action?

class Post < ActiveRecord::Base
  belongs_to :user
end

ActiveAdmin.register CertificationModel do
  controller do
    def scoped_collection
      current_user.posts
    end
  end
end
like image 245
wacko Avatar asked Feb 10 '14 16:02

wacko


2 Answers

The solution was simple: to keep scoped_collection and to redefine #show action.

ActiveAdmin.register Post do
  controller do
    def show
      @post = Post.find params[:id]
    end

    def scoped_collection
      current_user.posts
    end
  end
end
like image 144
wacko Avatar answered Sep 19 '22 02:09

wacko


You can call super for other action on which you don't want your scoped_collection to be called.

ActiveAdmin.register Post do
  controller do
    def scoped_collection
      if ['index', 'action2', 'action3'].include?(params[:action])
        current_user.posts
      else
        super
      end
    end
  end
end
like image 36
Touqeer Avatar answered Sep 18 '22 02:09

Touqeer