cheatsheets/activeadmin.md

97 lines
1.5 KiB
Markdown
Raw Permalink Normal View History

2017-10-27 03:30:58 +00:00
---
2012-11-25 04:13:51 +00:00
title: ActiveAdmin
2015-11-24 04:32:36 +00:00
category: Ruby
2012-11-25 04:13:51 +00:00
---
### Listing scopes
Allows you to filter listings by a certain scope.
2017-10-27 03:30:58 +00:00
{: .-setup}
2012-11-25 04:13:51 +00:00
2017-10-27 03:30:58 +00:00
```ruby
scope :draft
scope :for_approval
```
2012-11-25 04:13:51 +00:00
2017-10-27 03:30:58 +00:00
```ruby
scope :public, if: ->{ current_admin_user.can?(...) }
scope "Unapproved", :pending
scope("Published") { |books| books.where(:published: true) }
```
2014-02-25 10:32:14 +00:00
2012-11-25 04:13:51 +00:00
### Sidebar filters
2017-10-27 03:30:58 +00:00
```ruby
filter :email
filter :username
```
2012-11-25 04:13:51 +00:00
### Custom actions
You can define custom actions for models.
2017-10-27 03:30:58 +00:00
{: .-setup}
```ruby
before_filter only: [:show, :edit, :publish] do
@post = Post.find(params[:id])
end
```
#### Make the route
```ruby
member_action :publish, method: :put do
@post.publish!
redirect_to admin_posts_path, notice: "The post '#{@post}' has been published!"
end
```
#### Link it in the index
```ruby
index do
column do |post|
link_to 'Publish', publish_admin_post_path(post), method: :put
end
end
```
#### And link it in show/edit
```ruby
action_item only: [:edit, :show] do
@post = Post.find(params[:id])
link_to 'Publish', publish_admin_post_path(post), method: :put
end
```
2012-11-25 04:13:51 +00:00
### Columns
2017-10-27 03:30:58 +00:00
```ruby
column :foo
```
2012-11-25 04:13:51 +00:00
2017-10-27 03:30:58 +00:00
```ruby
column :title, sortable: :name do |post|
strong post.title
end
```
2012-11-25 04:13:51 +00:00
### Other helpers
2017-10-27 03:30:58 +00:00
```ruby
status_tag "Done" # Gray
status_tag "Finished", :ok # Green
status_tag "You", :warn # Orange
status_tag "Failed", :error # Red
```
2012-11-25 04:13:51 +00:00
### Disabling 'new post'
2017-10-27 03:30:58 +00:00
```ruby
ActiveAdmin.register Post do
actions :index, :edit
# or: config.clear_action_items!
end
```