OSDN Git Service

Regular updates
[twpd/master.git] / activeadmin.md
1 ---
2 title: ActiveAdmin
3 category: Ruby
4 layout: 2017/sheet
5 ---
6
7 ### Listing scopes
8
9 Allows you to filter listings by a certain scope.
10 {: .-setup}
11
12 ```ruby
13 scope :draft
14 scope :for_approval
15 ```
16
17 ```ruby
18 scope :public, if: ->{ current_admin_user.can?(...) }
19 scope "Unapproved", :pending
20 scope("Published") { |books| books.where(:published: true) }
21 ```
22
23 ### Sidebar filters
24
25 ```ruby
26 filter :email
27 filter :username
28 ```
29
30 ### Custom actions
31
32 You can define custom actions for models.
33 {: .-setup}
34
35 ```ruby
36 before_filter only: [:show, :edit, :publish] do
37   @post = Post.find(params[:id])
38 end
39 ```
40
41 #### Make the route
42
43 ```ruby
44 member_action :publish, method: :put do
45   @post.publish!
46   redirect_to admin_posts_path, notice: "The post '#{@post}' has been published!"
47 end
48 ```
49
50 #### Link it in the index
51
52 ```ruby
53 index do
54   column do |post|
55     link_to 'Publish', publish_admin_post_path(post), method: :put
56   end
57 end
58 ```
59
60 #### And link it in show/edit
61
62 ```ruby
63 action_item only: [:edit, :show] do
64   @post = Post.find(params[:id])
65   link_to 'Publish', publish_admin_post_path(post), method: :put
66 end
67 ```
68
69 ### Columns
70
71 ```ruby
72 column :foo
73 ```
74
75 ```ruby
76 column :title, sortable: :name do |post|
77   strong post.title
78 end
79 ```
80
81 ### Other helpers
82
83 ```ruby
84 status_tag "Done"           # Gray
85 status_tag "Finished", :ok  # Green
86 status_tag "You", :warn     # Orange
87 status_tag "Failed", :error # Red
88 ```
89
90 ### Disabling 'new post'
91
92 ```ruby
93 ActiveAdmin.register Post do
94   actions :index, :edit
95   # or: config.clear_action_items!
96 end
97 ```