/
resource_dsl.rb
214 lines (191 loc) · 6.1 KB
/
resource_dsl.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
module ActiveAdmin
# This is the class where all the register blocks are evaluated.
class ResourceDSL < DSL
private
# Redefine sort behaviour for column
#
# For example:
#
# # nulls last
# order_by(:age) do |order_clause|
# [order_clause.to_sql, 'NULLS LAST'].join(' ') if order_clause.order == 'desc'
# end
#
# # by last_name but in the case that there is no last name, by first_name.
# order_by(:full_name) do |order_clause|
# ['COALESCE(NULLIF(last_name, ''), first_name), first_name', order_clause.order].join(' ')
# end
#
#
def order_by(column, &block)
config.ordering[column] = block
end
def belongs_to(target, options = {})
config.belongs_to(target, options)
end
# Scope collection to a relation
def scope_to(*args, &block)
config.scope_to(*args, &block)
end
# Create a scope
def scope(*args, &block)
config.scope(*args, &block)
end
# Store relations that should be included
def includes(*args)
config.includes.push *args
end
#
# Keys included in the `permitted_params` setting are automatically whitelisted.
#
# Either
#
# permit_params :title, :author, :body, tags: []
#
# Or
#
# permit_params do
# defaults = [:title, :body]
# if current_user.admin?
# defaults + [:author]
# else
# defaults
# end
# end
#
def permit_params(*args, &block)
param_key = config.param_key.to_sym
belongs_to_param = config.belongs_to_param
create_another_param = :create_another if config.create_another
controller do
define_method :permitted_params do
permitted_params =
active_admin_namespace.permitted_params +
Array.wrap(belongs_to_param) +
Array.wrap(create_another_param)
params.permit(*permitted_params, param_key => block ? instance_exec(&block) : args)
end
end
end
# Configure the index page for the resource
def index(options = {}, &block)
options[:as] ||= :table
config.set_page_presenter :index, ActiveAdmin::PagePresenter.new(options, &block)
end
# Configure the show page for the resource
def show(options = {}, &block)
config.set_page_presenter :show, ActiveAdmin::PagePresenter.new(options, &block)
end
def form(options = {}, &block)
config.set_page_presenter :form, ActiveAdmin::PagePresenter.new(options, &block)
end
# Configure the CSV format
#
# For example:
#
# csv do
# column :name
# column("Author") { |post| post.author.full_name }
# end
#
# csv col_sep: ";", force_quotes: true do
# column :name
# end
#
def csv(options={}, &block)
options[:resource] = config
config.csv_builder = CSVBuilder.new(options, &block)
end
# Member Actions give you the functionality of defining both the
# action and the route directly from your ActiveAdmin registration
# block.
#
# For example:
#
# ActiveAdmin.register Post do
# member_action :comments do
# @post = Post.find(params[:id]
# @comments = @post.comments
# end
# end
#
# Will create a new controller action comments and will hook it up to
# the named route (comments_admin_post_path) /admin/posts/:id/comments
#
# You can treat everything within the block as a standard Rails controller
# action.
#
def action(set, name, options = {}, &block)
set << ControllerAction.new(name, options)
title = options.delete(:title)
controller do
before_action(only: [name]) { @page_title = title } if title
define_method(name, &block || Proc.new{})
end
end
def member_action(name, options = {}, &block)
action config.member_actions, name, options, &block
end
def collection_action(name, options = {}, &block)
action config.collection_actions, name, options, &block
end
def decorate_with(decorator_class)
# Force storage as a string. This will help us with reloading issues.
# Assuming decorator_class.to_s will return the name of the class allows
# us to handle a string or a class.
config.decorator_class_name = "::#{ decorator_class }"
end
# Defined Callbacks
#
# == After Build
# Called after the resource is built in the new and create actions.
#
# ActiveAdmin.register Post do
# after_build do |post|
# post.author = current_user
# end
# end
#
# == Before / After Create
# Called before and after a resource is saved to the db on the create action.
#
# == Before / After Update
# Called before and after a resource is saved to the db on the update action.
#
# == Before / After Save
# Called before and after the object is saved in the create and update action.
# Note: Gets called after the create and update callbacks
#
# == Before / After Destroy
# Called before and after the object is destroyed from the database.
#
delegate :before_build, :after_build, to: :controller
delegate :before_create, :after_create, to: :controller
delegate :before_update, :after_update, to: :controller
delegate :before_save, :after_save, to: :controller
delegate :before_destroy, :after_destroy, to: :controller
# This code defines both *_filter and *_action for Rails 4.0 to Rails 5.
actions = [
:before, :skip_before,
:after, :skip_after,
:around, :skip
]
[:action, :filter].each do |name|
actions.each do |action|
define_method "#{action}_#{name}" do |*args, &block|
controller.public_send "#{action}_action", *args, &block
end
end
end
# Specify which actions to create in the controller
#
# Eg:
#
# ActiveAdmin.register Post do
# actions :index, :show
# end
#
# Will only create the index and show actions (no create, update or delete)
delegate :actions, to: :controller
end
end