forked from refinery/refinerycms
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcrud.rb
335 lines (289 loc) · 11.8 KB
/
crud.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# Base methods for CRUD actions
# Simply override any methods in your action controller you want to be customised
# Don't forget to add:
# resources :plural_model_name_here
# or for scoped:
# scope(:as => 'module_module', :module => 'module_name') do
# resources :plural_model_name_here
# end
# to your routes.rb file.
# Full documentation about CRUD and resources go here:
# -> http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Resources.html#method-i-resources
# Example (add to your controller):
# crudify :foo, :title_attribute => 'name' for CRUD on Foo model
# crudify :'foo/bar', :title_attribute => 'name' for CRUD on Foo::Bar model
# Note: @singular_name will result in @foo for :foo and @bar for :'foo/bar'
module Refinery
module Crud
def self.default_options(model_name)
class_name = "#{model_name.to_s.camelize.gsub('/', '::')}".gsub('::::', '::')
this_class = class_name.constantize.base_class
singular_name = ActiveModel::Naming.param_key(this_class)
plural_name = singular_name.pluralize
{
:conditions => '',
:include => [],
:order => ('position ASC' if this_class.table_exists? && this_class.column_names.include?('position')),
:paging => true,
:per_page => false,
:redirect_to_url => "refinery.#{Refinery.route_for_model(class_name.constantize, :plural => true)}",
:searchable => true,
:search_conditions => '',
:sortable => true,
:title_attribute => "title",
:xhr_paging => false,
:class_name => class_name,
:singular_name => singular_name,
:plural_name => plural_name
}
end
def self.append_features(base)
super
base.extend(ClassMethods)
end
module ClassMethods
def crudify(model_name, options = {})
options = ::Refinery::Crud.default_options(model_name).merge(options)
class_name = options[:class_name]
singular_name = options[:singular_name]
plural_name = options[:plural_name]
module_eval %(
def self.crudify_options
#{options.inspect}
end
prepend_before_filter :find_#{singular_name},
:only => [:update, :destroy, :edit, :show]
def new
@#{singular_name} = #{class_name}.new
end
def create
# if the position field exists, set this object as last object, given the conditions of this class.
if #{class_name}.column_names.include?("position") && params[:#{singular_name}][:position].nil?
params[:#{singular_name}].merge!({
:position => ((#{class_name}.maximum(:position, :conditions => #{options[:conditions].inspect})||-1) + 1)
})
end
if (@#{singular_name} = #{class_name}.create(params[:#{singular_name}])).valid?
flash.notice = t(
'refinery.crudify.created',
:what => "'\#{@#{singular_name}.#{options[:title_attribute]}}'"
)
unless from_dialog?
unless params[:continue_editing] =~ /true|on|1/
redirect_back_or_default(#{options[:redirect_to_url]})
else
unless request.xhr?
redirect_to :back
else
render :partial => '/refinery/message'
end
end
else
self.index
@dialog_successful = true
render :index
end
else
unless request.xhr?
render :action => 'new'
else
render :partial => '/refinery/admin/error_messages', :locals => {
:object => @#{singular_name},
:include_object_name => true
}
end
end
end
def edit
# object gets found by find_#{singular_name} function
end
def update
if @#{singular_name}.update_attributes(params[:#{singular_name}])
flash.notice = t(
'refinery.crudify.updated',
:what => "'\#{@#{singular_name}.#{options[:title_attribute]}}'"
)
unless from_dialog?
unless params[:continue_editing] =~ /true|on|1/
redirect_back_or_default(#{options[:redirect_to_url]})
else
unless request.xhr?
redirect_to :back
else
render :partial => '/refinery/message'
end
end
else
self.index
@dialog_successful = true
render :index
end
else
unless request.xhr?
render :action => 'edit'
else
render :partial => '/refinery/admin/error_messages', :locals => {
:object => @#{singular_name},
:include_object_name => true
}
end
end
end
def destroy
# object gets found by find_#{singular_name} function
title = @#{singular_name}.#{options[:title_attribute]}
if @#{singular_name}.destroy
flash.notice = t('destroyed', :scope => 'refinery.crudify', :what => "'\#{title}'")
end
redirect_to #{options[:redirect_to_url]}
end
# Finds one single result based on the id params.
def find_#{singular_name}
@#{singular_name} = #{class_name}.find(params[:id],
:include => #{options[:include].map(&:to_sym).inspect})
end
# Find the collection of @#{plural_name} based on the conditions specified into crudify
# It will be ordered based on the conditions specified into crudify
# And eager loading is applied as specified into crudify.
def find_all_#{plural_name}(conditions = #{options[:conditions].inspect})
@#{plural_name} = #{class_name}.where(conditions).includes(
#{options[:include].map(&:to_sym).inspect}
).order("#{options[:order]}")
end
# Paginate a set of @#{plural_name} that may/may not already exist.
def paginate_all_#{plural_name}
# If we have already found a set then we don't need to again
find_all_#{plural_name} if @#{plural_name}.nil?
per_page = if #{options[:per_page].present?.inspect}
#{options[:per_page].inspect}
elsif #{class_name}.methods.map(&:to_sym).include?(:per_page)
#{class_name}.per_page
end
@#{plural_name} = @#{plural_name}.paginate(:page => params[:page], :per_page => per_page)
end
# If the controller is being accessed via an ajax request
# then render only the collection of items.
def render_partial_response?
if request.xhr?
render :text => render_to_string(:partial => '#{plural_name}', :layout => false).html_safe,
:layout => 'refinery/flash' and return false
end
end
# Returns a weighted set of results based on the query specified by the user.
def search_all_#{plural_name}
# First find normal results.
find_all_#{plural_name}(#{options[:search_conditions].inspect})
# Now get weighted results by running the query against the results already found.
@#{plural_name} = @#{plural_name}.with_query(params[:search])
end
# Ensure all methods are protected so that they should only be called
# from within the current controller.
protected :find_#{singular_name},
:find_all_#{plural_name},
:paginate_all_#{plural_name},
:render_partial_response?,
:search_all_#{plural_name}
)
# Methods that are only included when this controller is searchable.
if options[:searchable]
if options[:paging]
module_eval %(
def index
search_all_#{plural_name} if searching?
paginate_all_#{plural_name}
render_partial_response?
end
)
else
module_eval %(
def index
unless searching?
find_all_#{plural_name}
else
search_all_#{plural_name}
end
render_partial_response?
end
)
end
else
if options[:paging]
module_eval %(
def index
paginate_all_#{plural_name}
render_partial_response?
end
)
else
module_eval %(
def index
find_all_#{plural_name}
render_partial_response?
end
)
end
end
if options[:sortable]
module_eval %(
def reorder
find_all_#{plural_name}
end
# Based upon http://github.com/matenia/jQuery-Awesome-Nested-Set-Drag-and-Drop
def update_positions
previous = nil
params[:ul].each do |_, list|
# After we drop Ruby 1.8.x support the following line can be changed back to
# list.each do |index, hash|
# because there won't be an ordering issue (see https://github.com/resolve/refinerycms/issues/1585)
list.sort_by {|k, v| k.to_i}.map { |item| item[1] }.each_with_index do |hash, index|
moved_item_id = hash['id'].split(/#{singular_name}\_?/).reject(&:empty?).first
@current_#{singular_name} = #{class_name}.find_by_id(moved_item_id)
if @current_#{singular_name}.respond_to?(:move_to_root)
if previous.present?
@current_#{singular_name}.move_to_right_of(#{class_name}.find_by_id(previous))
else
@current_#{singular_name}.move_to_root
end
else
@current_#{singular_name}.update_attributes :position => index
end
if hash['children'].present?
update_child_positions(hash, @current_#{singular_name})
end
previous = moved_item_id
end
end
#{class_name}.rebuild! if #{class_name}.respond_to?(:rebuild!)
render :nothing => true
end
def update_child_positions(_node, #{singular_name})
list = _node['children']['0']
list.sort_by {|k, v| k.to_i}.map { |item| item[1] }.each_with_index do |child, index|
child_id = child['id'].split(/#{singular_name}\_?/).reject(&:empty?).first
child_#{singular_name} = #{class_name}.where(:id => child_id).first
child_#{singular_name}.move_to_child_of(#{singular_name})
if child['children'].present?
update_child_positions(child, child_#{singular_name})
end
end
end
)
end
module_eval %(
class << self
def pageable?
#{options[:paging].to_s}
end
alias_method :paging?, :pageable?
def sortable?
#{options[:sortable].to_s}
end
def searchable?
#{options[:searchable].to_s}
end
end
)
end
end
end
end