make ransac predicate_end_matcher lazy loaded to support boot time predicate adittions

This commit is contained in:
2018-05-05 16:29:20 -03:00
parent 84f6f8313a
commit adc02128e4
+7 -2
View File
@@ -2,6 +2,7 @@ module Dunlop::Ember::ApiBaseController
extend ActiveSupport::Concern
included do
cattr_accessor :predicate_end_matcher
before_action :authenticate_user_from_token!
before_action :authenticate_user!
before_action :set_current_user
@@ -174,6 +175,7 @@ module Dunlop::Ember::ApiBaseController
{}
end
# predicate_end_matcher =>
# /(_eq|_eq_any|_eq_all|_not_eq|_not_eq_any|_not_eq_all|_matches|_matches_any|_matches_all|
# _does_not_match|_does_not_match_any|_does_not_match_all|_lt|_lt_any|_lt_all|_lteq|_lteq_any|
# _lteq_all|_gt|_gt_any|_gt_all|_gteq|_gteq_any|_gteq_all|_in|_in_any|_in_all|_not_in|_not_in_any|
@@ -181,13 +183,13 @@ module Dunlop::Ember::ApiBaseController
# _not_cont_all|_i_not_cont|_i_not_cont_any|_i_not_cont_all|_start|_start_any|_start_all|_not_start|
# _not_start_any|_not_start_all|_end|_end_any|_end_all|_not_end|_not_end_any|_not_end_all|
# _true|_not_true|_false|_not_false|_present|_blank|_null|_not_null)$/
PREDICATE_END_MATCHER = Regexp.new("(_#{Ransack.predicates.keys.join('|_')})$")
#TODO: make me pretty, specs are in place
def query
self.class.predicate_end_matcher ||= Regexp.new("(_#{Ransack.predicates.keys.join('|_')})$")
mapped_query = {}
(params[:q] || {}).each do |key, value|
key = "#{key}_eq" unless %w[s sorts].include?(key) or record_class.try(:ransackable_scopes).try(:include?, key.to_sym) or key =~ PREDICATE_END_MATCHER # expect equality when no predicate is given
key = "#{key}_eq" unless %w[s sorts].include?(key) or record_class.try(:ransackable_scopes).try(:include?, key.to_sym) or key =~ self.class.predicate_end_matcher # expect equality when no predicate is given
if key =~ /^type_/
# map type to sti_type
mapped_value = case value
@@ -204,6 +206,9 @@ module Dunlop::Ember::ApiBaseController
end
def _render_with_renderer_json(resource, options = {})
if resource == nil
return params[:id].blank? ? '{"data":[]}' : '{"data":null}'
end
return resource.to_json if resource.is_a?(Hash)
return resource if resource.is_a?(String)