Module: Pureapi::Controller
- Defined in:
- lib/pureapi/controller.rb
Overview
Implement methods use for handle with table records like
- Pagination
- Sort
- Search
- Include
Constant Summary collapse
- FIELD_NAME =
Define constants of regex table and column name
'[a-z_][a-z0-9_]*'- NAME_REGEX =
/#{FIELD_NAME}/- FULLBEFORE_REGEX =
/#{FIELD_NAME}\{/- BEFORE_REGEX =
/\{/- AFTER_REGEX =
/\}/
Instance Method Summary collapse
-
#advanced_search_params ⇒ Object
Strong parameters for default advanced search query.
-
#default_cond_filter(objects) ⇒ Object
Begin setup of feature comparison conditions Filter conditions of object criteria Need upgrade calculate compcond_params.
-
#default_incond_filter(objects) ⇒ Object
Begin setup of feature in conditions (logical) Filter conditions of object criteria Need upgrade calculate compcond_params.
-
#default_index_filter(objects) ⇒ Object
Begin 3 main API for GET data Filter list objects contain search, sort, paginate.
-
#default_report_filter(objects) ⇒ Object
Filter list objects contain search, sort, limit.
-
#default_search_filter(objects) ⇒ Object
Begin setup of default_search_filter Filter searchs of object criteria.
-
#default_sort_filter(objects) ⇒ Object
Begin setup of default_sort_filter (.order asc|desc) Filter orders of object criteria.
-
#deparse_fields(fields) ⇒ Object
Hash
fieldsreturn String is params query. -
#filter_jsons ⇒ Object
Define json filters for params in render json api.
-
#include_relations(fields) ⇒ Object
return all relations be included by fields.
- #merge_includes(target, source) ⇒ Object
-
#openandclose(before, after) ⇒ Object
Require length of before and after is equal
before.length == after.length > 1. -
#paging_params ⇒ Object
Begin declare strong parameters.
-
#paging_standard(criteria_model) ⇒ Object
Paging standard Criteria model that will be pagination Return criteria_model.paginate and params[:page, :per_page, :page_total].
-
#parse_compconds(compconds = {}, column_names = []) ⇒ Object
Not use under code because lte => lt matchs = /([a-z0-9_]+).(#Pureapi::Model::COMPARISON_OPERATORS.keys.join('|'))/.match(key) if matchs && column_names.include?(matchs) results << matchs[1], o: Pureapi::Model::COMPARISON_OPERATORS[matchs[2].to_sym], v: value end.
-
#parse_fields(str, deep = 2) ⇒ Object
Methods parse string fields user for as_json Example str = "a,b, c,d,e,f,gk,l,h{b,i},wm,j,tgh, l{bb}, fmm, vv" return result = "b", "c", "d", "e", "f", "j", "vv"], :include=>{"g"=>{:only=>["k", "l"], :include=>{"h"=>{:only=>["b", "i"]}}, "w"=>"m"], "t"=>"gh"], :include=>{"l"=>{:only=>["bb"]}}, "f"=>"mm"]}}.
-
#parse_includes(fields, model, includes_deep_level = 2) ⇒ Object
Begin feature records_json and record_json which render format json Methods return hash contain full params use for ActiveRecord includes() and ActiveModel::Serializers::JSON as_json().
-
#parse_logicconds(compconds = {}, column_names = []) ⇒ Object
References from parse_compconds methods Upgrade results.push(!value.blank?) >>
parse_compconds. -
#parse_orders(orders = [], column_names = []) ⇒ Object
array orders contains field.asc or field array column_names contains valid fields return hash of field => sort type (asc, desc).
-
#record_asjson(model) ⇒ Object
Return json for show action.
-
#record_json(model, includes_deep_level = 2) ⇒ Object
Return json for show action.
-
#records_asjson(criterias) ⇒ Object
Return json for index action.
-
#records_json(criterias, includes_deep_level = 2) ⇒ Object
Return json for index action.
- #report_filter_jsons ⇒ Object
-
#search_params ⇒ Object
Strong parameters for default search query.
-
#standard_index_filter(objects) ⇒ Object
Begin options API for GET data Filter list objects contain search, sort, paginate.
-
#substr_index(str, sub_str) ⇒ Object
return Array all index of sub_str in str.
-
#validate_page(page, page_total) ⇒ Object
Begin setup of paging_standard.
-
#without_paging_index_filter(objects) ⇒ Object
Filter list objects contain search, sort.
Instance Method Details
#advanced_search_params ⇒ Object
Strong parameters for default advanced search query
482 483 484 |
# File 'lib/pureapi/controller.rb', line 482 def advanced_search_params params.permit() end |
#default_cond_filter(objects) ⇒ Object
Begin setup of feature comparison conditions Filter conditions of object criteria Need upgrade calculate compcond_params
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/pureapi/controller.rb', line 199 def default_cond_filter(objects) compcond_params = [] objects.compcond_columns.each do |column_name| Pureapi::Model::COMPARISON_OPERATORS.each do |key, value| compcond_params << [column_name, key].join('.') end end compconds = parse_compconds(params.permit(compconds: compcond_params)[:compconds].to_h, objects.column_names) params[:compconds] = {} compconds.each do |compcond| params[:compconds]["#{compcond[:f]}.#{Pureapi::Model::COMPARISON_OPERATORS_INVERT[compcond[:o]]}"] = compcond[:v] end objects.compconds(compconds) end |
#default_incond_filter(objects) ⇒ Object
Begin setup of feature in conditions (logical) Filter conditions of object criteria Need upgrade calculate compcond_params
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/pureapi/controller.rb', line 248 def default_incond_filter(objects) logic_params = [] objects.incond_columns.each do |column_name| logic_params << {[column_name, :in].join('.') => []} end logics = parse_logicconds(params.permit(logics: logic_params)[:logics].to_h, objects.column_names) params[:logics] = {} logics.each do |logic| params[:logics]["#{logic[:f]}.#{Pureapi::Model::LOGICAL_OPERATORS_INVERT[logic[:o]]}"] = logic[:v] end objects.logicconds(logics) end |
#default_index_filter(objects) ⇒ Object
Begin 3 main API for GET data Filter list objects contain search, sort, paginate
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/pureapi/controller.rb', line 20 def default_index_filter(objects) # 1. search # 2. sort and default sort is id.desc objects = default_sort_filter(objects) # 3. conditions objects = default_cond_filter(objects) objects = default_incond_filter(objects) # 4. paginate objects = paging_standard(objects) end |
#default_report_filter(objects) ⇒ Object
Filter list objects contain search, sort, limit
32 33 34 35 36 37 38 39 |
# File 'lib/pureapi/controller.rb', line 32 def default_report_filter(objects) # 1. search # 2. sort and default sort is id.desc # 3. conditions objects = default_cond_filter(objects) objects = default_incond_filter(objects) # 4. limit similar paginate end |
#default_search_filter(objects) ⇒ Object
Begin setup of default_search_filter Filter searchs of object criteria
164 165 166 167 168 169 |
# File 'lib/pureapi/controller.rb', line 164 def default_search_filter(objects) params[:searches] = search_params advanced_search_params.each{|k, v| params[:searches][k] = v} objects = objects.full_search(search_params, advanced_search_params) end |
#default_sort_filter(objects) ⇒ Object
Begin setup of default_sort_filter (.order asc|desc) Filter orders of object criteria
174 175 176 177 178 179 |
# File 'lib/pureapi/controller.rb', line 174 def default_sort_filter(objects) orders = parse_orders(params.permit(orders: [])[:orders], objects.column_names) orders[:id] = :desc if orders.blank? params[:orders] = orders.map{|field, order_type| "#{field}.#{order_type}"}.uniq objects.order(orders) end |
#deparse_fields(fields) ⇒ Object
Hash fields
return String is params query
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/pureapi/controller.rb', line 381 def deparse_fields(fields) return '' unless fields.is_a? Hash _arr = [] if fields[:only].is_a? Array _arr += fields[:only] end if fields[:methods].is_a? Array _arr += fields[:methods] end if fields[:include].is_a?(Hash) && !fields[:include].blank? fields[:include].each do |k, v| _arr << "#{k}{#{deparse_fields(v)}}" end end _arr.join(',') end |
#filter_jsons ⇒ Object
Define json filters for params in render json api
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/pureapi/controller.rb', line 109 def filter_jsons { paging: { page: params[:page], per_page: params[:per_page], page_total: params[:page_total], record_total: params[:record_total], next: params[:page] < params[:page_total] ? (params[:page] + 1) : nil, prev: params[:page] > 1 ? (params[:page] - 1) : nil, }, orders: params[:orders] || [], searches: params[:searches] || {}, compconds: params[:compconds] || {}, logics: params[:logics] || {}, fields: params[:fields] || '' } end |
#include_relations(fields) ⇒ Object
return all relations be included by fields
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/pureapi/controller.rb', line 434 def include_relations(fields) result = [] if fields[:include].is_a? Hash fields[:include].each do |k, v| if (v.is_a? Hash) && (v[:include].is_a? Hash) result << { k => include_relations(v)} else result << k end end end result end |
#merge_includes(target, source) ⇒ Object
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/pureapi/controller.rb', line 450 def merge_includes(target ,source) result = {} target.each do |k, v| result[k] = {} if v.is_a?(Hash) && source[k].is_a?(Hash) if source[k][:only].is_a?(Array) result[k][:only] = v[:only].is_a?(Array) ? (v[:only] & source[k][:only]) : source[k][:only] end if v[:include].is_a?(Hash) && source[k][:include].is_a?(Hash) result[k][:include] = merge_includes(v[:include], source[k][:include]) end end end result.delete_if { |k, v| v.blank? } end |
#openandclose(before, after) ⇒ Object
Require length of before and after is equal
before.length == after.length > 1
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/pureapi/controller.rb', line 405 def openandclose(before, after) results = {} index = 0 results[before[index]] = after[index] before.each_with_index do |value, i| if results[before[index]] > value results[before[index]] = after[i] else index = i results[before[index]] = after[index] end end return results end |
#paging_params ⇒ Object
Begin declare strong parameters
472 473 474 |
# File 'lib/pureapi/controller.rb', line 472 def paging_params params.permit(:page, :per_page) end |
#paging_standard(criteria_model) ⇒ Object
Paging standard Criteria model that will be pagination Return criteria_model.paginate and params[:page, :per_page, :page_total]
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/pureapi/controller.rb', line 293 def paging_standard(criteria_model) per_page = [paging_params[:per_page].try(:to_i) || Pureapi::Model::Pagination::DEFAULT_PER_PAGE, 1].max # Paging standard record_total = criteria_model.count per_page = record_total if paging_params[:per_page] == Pureapi::Model::Pagination::INFINITE_PER_PAGE\ && record_total > 0 page_total = (record_total.to_f / per_page).ceil page = validate_page(paging_params[:page].try(:to_i) || 1, page_total) page = [page, 1].max # Assign paging params to params helper params[:page] = page params[:per_page] = per_page params[:page_total] = page_total params[:record_total] = record_total criteria_model.paginate(page: page, per_page: per_page) end |
#parse_compconds(compconds = {}, column_names = []) ⇒ Object
Not use under code because lte => lt matchs = /([a-z0-9_]+).(#Pureapi::Model::COMPARISON_OPERATORS.keys.join('|'))/.match(key) if matchs && column_names.include?(matchs) results << matchs[1], o: Pureapi::Model::COMPARISON_OPERATORS[matchs[2].to_sym], v: value end
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/pureapi/controller.rb', line 223 def parse_compconds(compconds = {}, column_names = []) results = [] compconds.map do |key, value| splits = key.split('.') if splits.length == 2 && column_names.include?(splits[0]) if splits[1].to_sym == :eq && Pureapi::Model::NULL_OPERATOR_KEYS.include?(value) results << { f: splits[0], o: Pureapi::Model::COMPARISON_OPERATORS[splits[1].to_sym], v: Pureapi::Model::NULL_OPERATORS[value]} else results << {f: splits[0], o: Pureapi::Model::COMPARISON_OPERATORS[splits[1].to_sym], v: value} end end end unless compconds.blank? results end |
#parse_fields(str, deep = 2) ⇒ Object
Methods parse string fields user for as_json Example str = "a,b, c,d,e,f,gk,l,h{b,i},wm,j,tgh, l{bb}, fmm, vv" return result = "b", "c", "d", "e", "f", "j", "vv"], :include=>{"g"=>{:only=>["k", "l"], :include=>{"h"=>{:only=>["b", "i"]}}, "w"=>"m"], "t"=>"gh"], :include=>{"l"=>{:only=>["bb"]}}, "f"=>"mm"]}}
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/pureapi/controller.rb', line 341 def parse_fields(str, deep = 2) result = { only: [], include: {} } fullbefore = substr_index(str, FULLBEFORE_REGEX) after = substr_index(str, AFTER_REGEX) if fullbefore.length == after.length if fullbefore.length == 0 result[:only] = str.scan(NAME_REGEX) else kvs = openandclose(fullbefore, after) _kvs = openandclose(substr_index(str, BEFORE_REGEX), after).invert s = 0 kvs.each do |k, v| if s < k result[:only] += str[s..(k - 1)].scan(NAME_REGEX) s = v + 1 elsif s = v + 1 end if k < v && deep > 0 result[:include][str[k..v][NAME_REGEX].to_sym] =\ parse_fields(str[(_kvs[v] + 1)..(v - 1)], deep - 1) end end if s < (str.length - 1) result[:only] += str[s..-1].scan(NAME_REGEX) end end end result[:only] = result[:only].map(&:to_sym) return result.delete_if { |k, v| v.blank? } end |
#parse_includes(fields, model, includes_deep_level = 2) ⇒ Object
Begin feature records_json and record_json which render format json Methods return hash contain full params use for ActiveRecord includes() and ActiveModel::Serializers::JSON as_json()
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/pureapi/controller.rb', line 317 def parse_includes(fields, model, includes_deep_level = 2) _fields = parse_fields(fields, includes_deep_level) if _fields[:only].is_a?(Array) && !_fields[:only].blank? _fields[:methods] = _fields[:only] & model.json_methods _fields[:only] = _fields[:only] & model.column_names.map(&:to_sym) end if _fields[:include].is_a?(Hash) && !_fields[:include].blank? _fields[:include] = merge_includes(_fields[:include], model.asjson_fields) end { includes: include_relations(_fields), as_json: _fields.delete_if { |k, v| v.blank? } } end |
#parse_logicconds(compconds = {}, column_names = []) ⇒ Object
References from parse_compconds methods
Upgrade results.push(!value.blank?) >> parse_compconds
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/pureapi/controller.rb', line 267 def parse_logicconds(compconds = {}, column_names = []) results = [] compconds.map do |key, value| next if value.blank? splits = key.split('.') if splits.length == 2 && column_names.include?(splits[0]) results << {f: splits[0], o: Pureapi::Model::LOGICAL_OPERATORS[splits[1].to_sym], v: value} end end unless compconds.blank? results end |
#parse_orders(orders = [], column_names = []) ⇒ Object
array orders contains field.asc or field array column_names contains valid fields return hash of field => sort type (asc, desc)
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/pureapi/controller.rb', line 184 def parse_orders(orders = [], column_names = []) results = {} orders.map do |order| matchs = /(#{FIELD_NAME})\.(asc|desc)/.match(order) || /(#{FIELD_NAME})/i.match(order) results[matchs[1]] = matchs[2] || 'asc' if matchs && column_names.include?(matchs[1]) end unless orders.blank? results end |
#record_asjson(model) ⇒ Object
Return json for show action
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/pureapi/controller.rb', line 95 def record_asjson(model) = {} if params.permit(:fields)[:fields] jsons = parse_includes(params.permit(:fields)[:fields], model.class) = jsons[:as_json] if !jsons[:as_json].blank? end [:only] = [:only].blank? ? model.class.default_onlyasjsons : [:only] model.as_json() end |
#record_json(model, includes_deep_level = 2) ⇒ Object
Return json for show action
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/pureapi/controller.rb', line 61 def record_json(model, includes_deep_level = 2) = {} if params.permit(:fields)[:fields] jsons = parse_includes(params.permit(:fields)[:fields], model.class, includes_deep_level) = jsons[:as_json] if !jsons[:as_json].blank? end [:only] = [:only].blank? ? model.class.default_onlyasjsons : [:only] [:methods] = model.class.json_methods model.as_json() end |
#records_asjson(criterias) ⇒ Object
Return json for index action
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/pureapi/controller.rb', line 76 def records_asjson(criterias) = {} includes = [] if params.permit(:fields)[:fields] jsons = parse_includes(params.permit(:fields)[:fields], criterias) includes += jsons[:includes] if !jsons[:includes].blank? = jsons[:as_json] if !jsons[:as_json].blank? end params[:fields] = deparse_fields() [:only] = [:only].blank? ? criterias.default_onlyasjsons : [:only] includes += criterias.json_method_includes.slice(*[:methods]).values if ![:methods].blank? criterias.includes(includes.uniq).as_json() end |
#records_json(criterias, includes_deep_level = 2) ⇒ Object
Return json for index action
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pureapi/controller.rb', line 42 def records_json(criterias, includes_deep_level = 2) = {} includes = criterias.default_includes if params.permit(:fields)[:fields] jsons = parse_includes(params.permit(:fields)[:fields], criterias, includes_deep_level) includes += jsons[:includes] if !jsons[:includes].blank? = jsons[:as_json] if !jsons[:as_json].blank? end params[:fields] = deparse_fields() [:methods] = criterias.json_methods [:only] = [:only].blank? ? criterias.default_onlyasjsons : [:only] criterias = criterias.includes(includes) if !includes.blank? criterias.as_json() end |
#report_filter_jsons ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/pureapi/controller.rb', line 127 def report_filter_jsons { searches: params[:searches] || {}, compconds: params[:compconds] || {}, logics: params[:logics] || {}, } end |
#search_params ⇒ Object
Strong parameters for default search query
477 478 479 |
# File 'lib/pureapi/controller.rb', line 477 def search_params params.permit() end |
#standard_index_filter(objects) ⇒ Object
Begin options API for GET data Filter list objects contain search, sort, paginate
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/pureapi/controller.rb', line 138 def standard_index_filter(objects) # 1. search objects = default_search_filter(objects) # 2. sort and default sort is id.desc objects = default_sort_filter(objects) # 3. conditions objects = default_cond_filter(objects) objects = default_incond_filter(objects) # 4. paginate objects = paging_standard(objects) end |
#substr_index(str, sub_str) ⇒ Object
return Array all index of sub_str in str
423 424 425 426 427 428 429 430 431 |
# File 'lib/pureapi/controller.rb', line 423 def substr_index(str, sub_str) i = 0 all = [] while (i = str.index(sub_str, i)).is_a? Integer all << i i += str[i..-1][sub_str].length end all end |
#validate_page(page, page_total) ⇒ Object
Begin setup of paging_standard. If not use will_paginate, write in mode class Method validate page number (like page params)
286 287 288 |
# File 'lib/pureapi/controller.rb', line 286 def validate_page(page, page_total) page.blank? || page <= 0 ? 1 : (page > page_total ? page_total : page) end |
#without_paging_index_filter(objects) ⇒ Object
Filter list objects contain search, sort
151 152 153 154 155 156 157 158 159 |
# File 'lib/pureapi/controller.rb', line 151 def without_paging_index_filter(objects) # 1. search objects = default_search_filter(objects) # 2. sort and default sort is id.desc objects = default_sort_filter(objects) # 3. conditions objects = default_cond_filter(objects) objects = default_incond_filter(objects) end |