Class: RearControllerSetup
- Inherits:
-
Object
- Object
- RearControllerSetup
show all
- Includes:
- RearConstants
- Defined in:
- lib/rear/controller_setup.rb
Constant Summary
RearConstants::ASSETS__PATH, RearConstants::ASSETS__SUFFIX, RearConstants::ASSETS__SUFFIX_REGEXP, RearConstants::ASSOCS__STRUCT, RearConstants::COLUMNS__BOOLEAN_MAP, RearConstants::COLUMNS__DEFAULT_TYPE, RearConstants::COLUMNS__HANDLED_TYPES, RearConstants::COLUMNS__PANE_MAX_LENGTH, RearConstants::FILTERS__DECORATIVE_CMP, RearConstants::FILTERS__DEFAULT_TYPE, RearConstants::FILTERS__HANDLED_TYPES, RearConstants::FILTERS__QUERY_MAP, RearConstants::FILTERS__STR_TO_BOOLEAN, RearConstants::PAGER__SIDE_PAGES, RearConstants::PATH__TEMPLATES
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.crudify(*args, &proc) ⇒ Object
10
11
12
|
# File 'lib/rear/controller_setup.rb', line 10
def crudify(*args, &proc)
new.crudify(*args, &proc)
end
|
.init(*args, &proc) ⇒ Object
6
7
8
|
# File 'lib/rear/controller_setup.rb', line 6
def init(*args, &proc)
new.init(*args, &proc)
end
|
Instance Method Details
#crudify(ctrl, model, opts, &proc) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/rear/controller_setup.rb', line 35
def crudify ctrl, model, opts, &proc
(@ctrl = ctrl).class_exec do
@__rear__orm = RearUtils.orm(model)
@__rear__assocs = RearUtils.(model)
@__rear__real_columns, @__rear__pkey = RearUtils.extract_columns(model)
@__rear__real_columns.each do |c|
input(*c) unless @__rear__assocs[:belongs_to].any? {|a,s|
s[:belongs_to_keys][:source] == c.first
}
end
end
setup_crudifier(opts, &proc)
define_association_hooks
define_pane_hooks
define_editor_hooks
define_default_filters
end
|
#define_association_hooks ⇒ Object
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
|
# File 'lib/rear/controller_setup.rb', line 130
def define_association_hooks
@ctrl.class_exec do
before /reverse_assoc/ do
ctrl = action_params[:source_ctrl].split('::').inject(Object) {|f,c| f.const_get(c)}
assoc = ctrl.assocs[action_params[:assoc_type].to_sym][action_params[:assoc_name].to_sym]
readonly = ctrl.readonly_assocs.include?(assoc[:name]) || assoc[:readonly] || ctrl.readonly?
halt(400, '%s is in readonly mode' % model) if readonly && (post? || delete?)
struct = {
type: assoc[:type],
name: assoc[:name],
dom_id: assoc[:dom_id] + (action_params[:attached] ? '' : '_detached'),
readonly: readonly ? true : false, attached: action_params[:attached],
route: route(action, *action_params__array[0..3]),
source_item: RearORM.new(ctrl.model, ctrl.pkey)[action_params[:item_id].to_i],
target_item: orm[params[:target_item_id].to_i]||{},
source_key: nil,
target_key: nil,
}
if struct[:type] == :belongs_to && (struct[:attached] || struct[:source_item].nil?)
struct[:source_key], struct[:target_key] =
assoc[:belongs_to_keys].values_at(:source, :target)
end
@reverse_assoc = Struct.new(*struct.keys.map(&:to_sym)).new(*struct.values).freeze
end
end
end
|
#define_default_filters ⇒ Object
73
74
75
76
77
78
|
# File 'lib/rear/controller_setup.rb', line 73
def define_default_filters
@ctrl.class_exec do
filter(pkey, cmp: :csl, class: 'input-small search-query')
end
end
|
#define_editor_hooks ⇒ Object
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/rear/controller_setup.rb', line 234
def define_editor_hooks
@ctrl.class_exec do
before :get_edit do
if (id = action_params[:id].to_i) > 0
@item = orm[id] || halt(400, "Item with ID %s not found" % id)
@item_id = id
else
@item = @brand_new_item = model.new
@item_id = 0
end
end
end
end
|
#define_error_handlers ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/rear/controller_setup.rb', line 114
def define_error_handlers
@ctrl.class_exec do
error 500 do |e|
error = if e.respond_to?(:backtrace)
e.backtrace.inject([e.message]) {|bt,l| bt << l}
elsif e.is_a?(Array)
e
else
[e.to_s]
end
out = rq.xhr? ? error*"\n" : reander_l(:layout) {reander_p :error, error: error}
halt 500, out
end
end
end
|
#define_pane_hooks ⇒ Object
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
|
# File 'lib/rear/controller_setup.rb', line 167
def define_pane_hooks
@ctrl.class_exec do
before :get_index, :get_reverse_assoc, :get_quickview do
conditions = filters_to_sql
source_item, source_assoc = nil
if @reverse_assoc && @reverse_assoc.attached
if source_item = @reverse_assoc.source_item
source_assoc = @reverse_assoc.name
else
conditions = {conditions: {pkey => 0}}
end
end
total_items = source_item ?
orm.assoc_count(source_assoc, source_item, conditions) :
orm.count(conditions)
total_pages = (total_items.to_f / __rear__.ipp.to_f).ceil
side_pages = xhr? ? 2 : PAGER__SIDE_PAGES
current_page = params[:page].to_i
current_page = 1 if current_page < 1
current_page = total_pages if current_page > total_pages
page_min = current_page - side_pages
page_min = total_pages - (side_pages * 2) if (current_page + side_pages) > total_pages
page_min = 1 if page_min < 1
page_max = current_page + side_pages
page_max = side_pages * 2 if current_page < side_pages
page_max = total_pages if page_max > total_pages
offset = (current_page - 1) * __rear__.ipp
offset = 0 if offset < 0
counter = [offset + 1, offset + __rear__.ipp, total_items]
counter[0] = offset if counter[0] > total_items
counter[1] = total_items if counter[1] > total_items
@pager_context = {
total_items: total_items,
total_pages: total_pages,
current_page: current_page,
page_min: page_min,
page_max: page_max,
counter: counter.map {|n| RearUtils.number_with_delimiter(n)}
}
@pager = total_pages > 1 ? reander_p(:pager, @pager_context) : ''
conditions[:limit ] = __rear__.ipp
conditions[:offset] = offset
if order = order_params_to_sql || __rear__.order_by
conditions[:order] = order
end
@items = if source_item
orm.assoc_filter(source_assoc, source_item, conditions)
else
orm.filter(conditions)
end
end
end
end
|
#define_setup_methods ⇒ Object
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
|
# File 'lib/rear/controller_setup.rb', line 80
def define_setup_methods
@ctrl.class_exec do
def rear_templates path
return @__rear__templates_fullpath = path.to_s if path =~ /\A(\w\:)?\// && @__rear__templates_fullpath.nil?
@__rear__templates_path = path.to_s if @__rear__templates_path.nil?
end
define_setup_method :rear_templates
def rear_assets path
return @__rear__assets_fullpath = path.to_s if path =~ /\A(\w\:)?\// && @__rear__assets_fullpath.nil?
@__rear__assets_path = path.to_s if @__rear__assets_path.nil?
end
define_setup_method :rear_assets
end
end
|
#init(ctrl) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/rear/controller_setup.rb', line 16
def init ctrl
(@ctrl = ctrl).class_exec do
extend RearSetup
extend RearHelpers::ClassMixin
include RearConstants
include RearHelpers::InstanceMixin
import RearActions
include EL::Ace if defined?(EL::Ace)
include EL::CKE if defined?(EL::CKE)
reject_automount!
EUtils.
end
define_setup_methods
define_error_handlers
end
|
#setup_crudifier(opts, &proc) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/rear/controller_setup.rb', line 54
def setup_crudifier opts, &proc
@ctrl.class_exec do
opts[:delete] ||= :destroy! if orm == :dm
crudify(model, :crud, opts, &proc)
alias_before :post_crud, :create, :save
alias_before :put_crud, :update, :save
alias_before :delete_crud, :destroy
before :create, :update, :destroy do
halt(400, '%s is in readonly mode' % model) if readonly?
end
end
end
|