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
|
# File 'vendor/plugins/refinery/lib/crud.rb', line 20
def crudify(model_name, new_options = {})
options = {
:title_attribute => "title",
:order => 'position ASC',
:conditions => '',
:sortable => true,
:searchable => true,
:include => [],
:paging => true,
:search_conditions => ''
}.merge!(new_options)
singular_name = model_name.to_s
class_name = singular_name.camelize
plural_name = singular_name.pluralize
module_eval %(
before_filter :find_#{singular_name}, :only => [:update, :destroy, :edit]
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}].merge!({:position => #{class_name}.maximum(:position, :conditions => "#{options[:conditions]}") + 1})
end
if (@#{singular_name} = #{class_name}.create(params[:#{singular_name}])).valid?
unless request.xhr?
flash[:notice] = "'\#{@#{singular_name}.#{options[:title_attribute]}}' was successfully created."
else
flash.now[:notice] = "'\#{@#{singular_name}.#{options[:title_attribute]}}' was successfully created."
end
unless params[:continue_editing] =~ /true|on|1/
redirect_to admin_#{plural_name}_url
else
unless request.xhr?
redirect_to :back
else
render :partial => "/shared/message"
end
end
else
unless request.xhr?
render :action => 'new'
else
render :partial => "/shared/admin/error_messages_for", :locals => {:symbol => :#{singular_name}, :object => @#{singular_name}}
end
end
end
def edit
@#{singular_name} = #{class_name}.find(params[:id])
end
def update
if @#{singular_name}.update_attributes(params[:#{singular_name}])
unless request.xhr?
flash[:notice] = "'\#{@#{singular_name}.#{options[:title_attribute]}}' was successfully updated."
else
flash.now[:notice] = "'\#{@#{singular_name}.#{options[:title_attribute]}}' was successfully updated."
end
unless params[:continue_editing] =~ /true|on|1/
redirect_to admin_#{plural_name}_url
else
unless request.xhr?
redirect_to :back
else
render :partial => "/shared/message"
end
end
else
unless request.xhr?
render :action => 'edit'
else
render :partial => "/shared/admin/error_messages_for", :locals => {:symbol => :#{singular_name}, :object => @#{singular_name}}
end
end
end
def destroy
flash[:notice] = "'\#{@#{singular_name}.#{options[:title_attribute]}}' was successfully deleted." if @#{singular_name}.destroy
redirect_to admin_#{plural_name}_url
end
def find_#{singular_name}
@#{singular_name} = #{class_name}.find(params[:id], :include => %w(#{options[:include].join(' ')}))
end
def find_all_#{plural_name}
@#{plural_name} = #{class_name}.find :all,
:order => "#{options[:order]}",
:conditions => "#{options[:conditions]}",
:include => %w(#{options[:include].join(' ')})
end
def paginate_all_#{plural_name}
@#{plural_name} = #{class_name}.paginate :page => params[:page],
:order => "#{options[:order]}",
:conditions => "#{options[:conditions]}",
:include => %w(#{options[:include].join(' ')})
end
def search_all_#{plural_name}
@#{plural_name} = #{class_name}.find_with_index params[:search],
:order => "#{options[:order]}",
:conditions => "#{options[:search_conditions]}",
:include => %w(#{options[:include].join(' ')})
end
def search_and_paginate_all_#{plural_name}
@#{plural_name} = #{class_name}.paginate_search params[:search],
:page => params[:page],
:order => "#{options[:order]}",
:conditions => "#{options[:search_conditions]}",
:include => %w(#{options[:include].join(' ')})
end
protected :find_#{singular_name}, :find_all_#{plural_name}, :paginate_all_#{plural_name}, :search_all_#{plural_name}, :search_and_paginate_all_#{plural_name}
)
if options[:searchable]
if options[:paging]
module_eval %(
def index
if searching?
search_and_paginate_all_#{plural_name}
else
paginate_all_#{plural_name}
end
end
)
else
module_eval %(
def index
if searching?
search_all_#{plural_name}
else
find_all_#{plural_name}
end
end
)
end
else
if options[:paging]
module_eval %(
def index
paginate_all_#{plural_name}
end
)
else
module_eval %(
def index
find_all_#{plural_name}
end
)
end
end
if options[:sortable]
module_eval %(
def reorder
find_all_#{plural_name}
end
def update_positions
unless params[:tree] == "true"
params[:sortable_list].each_with_index do |i, index|
#{class_name}.find(i).update_attribute(:position, index)
end
else
params[:sortable_list].each do |position, id_hash|
parse_branch(position, id_hash, nil)
end
end
find_all_#{plural_name}
render :partial => 'sortable_list', :layout => false, :locals => {:continue_reordering => params[:continue_reordering]}
end
# takes in a single branch and saves the nodes inside it
def parse_branch(position, id_hash, parent_id)
id_hash.each do |pos, id|
parse_branch(pos, id, id_hash["id"]) unless pos == "id"
end if id_hash.include?('0')
#{class_name}.update(id_hash["id"], :parent_id => parent_id, :position => position)
end
protected :parse_branch
)
end
end
|