Module: RearUtils
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
-
.ar?(model) ⇒ Boolean
-
.associated_model_controller(model, ensure_mounted = false) ⇒ Object
-
.dm?(model) ⇒ Boolean
-
.dom_id_generator(*args) ⇒ Object
-
.extract_ar_assocs(model, any = false) ⇒ Object
-
.extract_ar_columns(model) ⇒ Object
-
.extract_associated_ar_model(model, assoc) ⇒ Object
-
.extract_assocs(model, *args) ⇒ Object
-
.extract_columns(model) ⇒ Object
-
.extract_constant(smth) ⇒ Object
-
.extract_dm_assocs(model, any = false) ⇒ Object
-
.extract_dm_columns(model) ⇒ Object
-
.extract_sq_assocs(model, any = false) ⇒ Object
-
.extract_sq_columns(model) ⇒ Object
-
.initialize_model_controller(model) ⇒ Object
just define controller, do not set model.
-
.is_orm? ⇒ Object
-
.normalize_html_attrs(attrs) ⇒ Object
-
.number_with_delimiter(n) ⇒ Object
-
.orm(model) ⇒ Object
-
.quote_ar_column(model, column) ⇒ Object
-
.quote_column(model, column) ⇒ Object
-
.quote_dm_column(model, column) ⇒ Object
-
.quote_sq_column(model, column) ⇒ Object
-
.sq?(model) ⇒ Boolean
Class Method Details
.ar?(model) ⇒ Boolean
238
239
240
241
242
|
# File 'lib/rear/utils.rb', line 238
def ar? model
[:connection, :columns, :reflect_on_all_associations].all? do |m|
model.respond_to?(m)
end
end
|
.associated_model_controller(model, ensure_mounted = false) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/rear/utils.rb', line 15
def associated_model_controller model, ensure_mounted = false
ctrl = ObjectSpace.each_object(Class).find do |o|
EUtils.is_app?(o) && o.respond_to?(:model) && o.model == model
end
unless ctrl
ctrl = initialize_model_controller(model)
ctrl.label(false) end
ctrl.mounted? || ctrl.mount if ensure_mounted
ctrl
end
|
.dm?(model) ⇒ Boolean
245
246
247
248
249
|
# File 'lib/rear/utils.rb', line 245
def dm? model
[:repository, :properties, :relationships].all? do |m|
model.respond_to?(m)
end
end
|
.dom_id_generator(*args) ⇒ Object
276
277
278
|
# File 'lib/rear/utils.rb', line 276
def dom_id_generator *args
(args + [args.__id__]).flatten.join('__').gsub(/\W+/, '')
end
|
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
|
# File 'lib/rear/utils.rb', line 51
def model, any = false
model.reflect_on_all_associations.inject(ASSOCS__STRUCT.call) do |map, r|
target_model = (model, r) ||
raise(NameError, "Was unable to detect model for %s relation" % r)
target_pkey = r.options[:primary_key] || target_model.primary_key
if any || target_pkey
target_pkey = target_pkey.to_sym if target_pkey
readonly = nil
belongs_to_keys = {source: nil, target: nil}
assoc_type = case r.macro
when :belongs_to
belongs_to_keys[:source] = (r.options[:foreign_key] || ActiveSupport::Inflector.foreign_key(r.name)).to_sym
belongs_to_keys[:target] = target_pkey
:belongs_to
when :has_one
readonly = true if r.options[:through]
:has_one
else
:has_many
end
map[assoc_type].update r.name => {
type: assoc_type,
name: r.name,
remote_model: target_model,
remote_pkey: target_pkey,
readonly: readonly,
dom_id: dom_id_generator(model, assoc_type, r.name),
belongs_to_keys: belongs_to_keys
}.freeze
end
map
end.freeze
end
|
.extract_ar_columns(model) ⇒ Object
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/rear/utils.rb', line 169
def extract_ar_columns model
unless model.table_exists?
puts "WARN: %s table does not exists!" % model.table_name
return [[], :id]
end
pkey = nil
columns = model.columns.inject([]) do |f,c|
name, type = c.name.to_sym, c.type.to_s.downcase.to_sym
type = COLUMNS__DEFAULT_TYPE unless COLUMNS__HANDLED_TYPES.include?(type)
(c.primary && pkey = name) ? f : f << [name, type]
end
[columns.freeze, pkey]
end
|
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/rear/utils.rb', line 33
def model, assoc
class_name = assoc.options[:class_name] ||
ActiveSupport::Inflector.camelize(ActiveSupport::Inflector.singularize(assoc.name.to_s))
if (ns = class_name.to_s.split('::')).size > 1
return ns.inject(Object) {|n,c| n.const_get c}
else
if (ns = model.name.split('::')).size > 1
ns.pop
model_namespace = ns.inject(Object) {|n,c| n.const_get c}
else
model_namespace = Object
end
return model_namespace.const_get(class_name)
end
nil
end
|
28
29
30
|
# File 'lib/rear/utils.rb', line 28
def model, *args
send('extract_%s_assocs' % orm(model), model, *args)
end
|
.extract_columns(model) ⇒ Object
164
165
166
|
# File 'lib/rear/utils.rb', line 164
def extract_columns model
send('extract_%s_columns' % orm(model), model)
end
|
281
282
283
284
285
|
# File 'lib/rear/utils.rb', line 281
def smth
return Object.const_get(smth) if smth.is_a?(Symbol)
return smth.sub('::', '').split('::').inject(Object) {|o,c| o.const_get(c)} if smth.is_a?(String)
smth
end
|
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
|
# File 'lib/rear/utils.rb', line 91
def model, any = false
model.relationships.entries.inject(ASSOCS__STRUCT.call) do |map, r|
_, target_pkey = extract_dm_columns(r.target_model)
if any || target_pkey
readonly = nil
belongs_to_keys = {source: nil, target: nil}
assoc_type = case r.class.name.split('::')[2]
when 'ManyToOne'
belongs_to_keys[:source] = r.source_key.first.name.to_sym
belongs_to_keys[:target] = r.target_key.first.name.to_sym
:belongs_to
when 'OneToOne'
readonly = true if r.options[:through]
:has_one
else
:has_many
end
map[assoc_type].update r.name => {
type: assoc_type,
name: r.name,
remote_model: r.target_model,
remote_pkey: target_pkey,
readonly: readonly,
dom_id: dom_id_generator(model, assoc_type, r.name),
belongs_to_keys: belongs_to_keys
}.freeze
end
map
end.freeze
end
|
.extract_dm_columns(model) ⇒ Object
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/rear/utils.rb', line 184
def extract_dm_columns model
pkey = nil
columns = model.properties.inject([]) do |f,c|
name, type = c.name.to_sym, c.class.name.to_s.split('::').last.downcase.to_sym
type = COLUMNS__DEFAULT_TYPE unless [:serial].concat(COLUMNS__HANDLED_TYPES).include?(type)
(type == :serial && pkey = name) ? f : f << [name, type]
end
[columns.freeze, pkey]
end
|
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
|
# File 'lib/rear/utils.rb', line 126
def model, any = false
model.associations.inject(ASSOCS__STRUCT.call) do |map,an|
a = model.association_reflection(an)
target_model = (a[:class_name])
_, target_pkey = extract_sq_columns(target_model)
if any || target_pkey readonly = nil
belongs_to_keys = {source: nil, target: nil}
assoc_type = case a[:type]
when :many_to_one
belongs_to_keys[:source] = a[:key]
belongs_to_keys[:target] = a[:primary_key] || target_pkey
:belongs_to
when :one_to_one
readonly = true if a[:through]
:has_one
else
:has_many
end
map[assoc_type].update a[:name] => {
type: assoc_type,
name: a[:name],
remote_model: target_model,
remote_pkey: target_pkey,
readonly: readonly,
dom_id: dom_id_generator(model, assoc_type, a[:name]),
belongs_to_keys: belongs_to_keys
}.freeze
end
map
end.freeze
end
|
.extract_sq_columns(model) ⇒ Object
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/rear/utils.rb', line 196
def extract_sq_columns model
pkey = nil
columns = model.db_schema.inject([]) do |map,(n,s)|
if s[:primary_key]
pkey = n
map
else
type = s[:db_type].to_s.split(/\W/).first.to_s.downcase.to_sym
unless COLUMNS__HANDLED_TYPES.include?(type)
type = s[:type].to_s.downcase.to_sym
unless COLUMNS__HANDLED_TYPES.include?(type)
type = COLUMNS__DEFAULT_TYPE
end
end
map << [n, type]
end
end
[columns.freeze, pkey]
end
|
.initialize_model_controller(model) ⇒ Object
just define controller, do not set model
5
6
7
8
9
10
11
12
|
# File 'lib/rear/utils.rb', line 5
def initialize_model_controller model
return model.const_get(:RearController) if model.const_defined?(:RearController)
ctrl = model.const_set(:RearController, Class.new(E))
RearControllerSetup.init(ctrl)
ctrl.map EUtils.class_to_route(model)
ctrl.model model
ctrl
end
|
.is_orm? ⇒ Object
262
263
264
|
# File 'lib/rear/utils.rb', line 262
def orm model
[:ar, :dm, :sq].find {|o| send('%s?' % o, model)}
end
|
.normalize_html_attrs(attrs) ⇒ Object
271
272
273
|
# File 'lib/rear/utils.rb', line 271
def normalize_html_attrs attrs
(attrs||{}).inject({}) {|h,(k,v)| h.merge k.to_s.downcase => v}
end
|
.number_with_delimiter(n) ⇒ Object
266
267
268
|
# File 'lib/rear/utils.rb', line 266
def number_with_delimiter n
n.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, '\1,')
end
|
.orm(model) ⇒ Object
259
260
261
|
# File 'lib/rear/utils.rb', line 259
def orm model
[:ar, :dm, :sq].find {|o| send('%s?' % o, model)}
end
|
.quote_ar_column(model, column) ⇒ Object
222
223
224
|
# File 'lib/rear/utils.rb', line 222
def quote_ar_column model, column
model.connection.quote_column_name(column)
end
|
.quote_column(model, column) ⇒ Object
217
218
219
|
# File 'lib/rear/utils.rb', line 217
def quote_column model, column
send('quote_%s_column' % orm(model), model, column)
end
|
.quote_dm_column(model, column) ⇒ Object
227
228
229
230
|
# File 'lib/rear/utils.rb', line 227
def quote_dm_column model, column
property = model.properties.find {|p| p.name == column}
model.repository(model.repository_name).adapter.property_to_column_name(property, false)
end
|
.quote_sq_column(model, column) ⇒ Object
233
234
235
|
# File 'lib/rear/utils.rb', line 233
def quote_sq_column model, column
model.db.quote_identifier(column)
end
|
.sq?(model) ⇒ Boolean
252
253
254
255
256
|
# File 'lib/rear/utils.rb', line 252
def sq? model
[:db_schema, :columns, :dataset, :associations].all? do |m|
model.respond_to?(m)
end
end
|