@@ -47,38 +47,64 @@ def parse_aggregation(node)
4747 end
4848
4949 def parse_condition ( condition )
50- ensure_valid_condition ( condition )
50+ where = parse_condition_without_smart_field ( condition )
5151
52- operator = condition [ 'operator' ]
53- value = condition [ 'value' ]
54- field = condition [ 'field' ]
52+ field_name = condition [ 'field' ]
5553
56- if @operator_date_parser . is_date_operator? ( operator )
57- condition = @operator_date_parser . get_date_filter ( operator , value )
58- return "#{ parse_field_name ( field ) } #{ condition } "
54+ if ForestLiana ::SchemaHelper . is_smart_field? ( @resource , field_name )
55+ schema = ForestLiana . schema_for_resource ( @resource )
56+ field_schema = schema . fields . find do |field |
57+ field [ :field ] . to_s == field_name
58+ end
59+
60+ unless field_schema . try ( :[] , :filter )
61+ raise ForestLiana ::Errors ::NotImplementedMethodError . new ( "method filter on smart field '#{ field_name } ' not found" )
62+ end
63+
64+ return field_schema [ :filter ] . call ( condition , where )
5965 end
6066
61- if is_belongs_to ( field )
62- association = field . partition ( ':' ) . first . to_sym
63- association_field = field . partition ( ':' ) . last
67+ where
68+ end
69+
70+ def get_association_field_and_resource ( field_name )
71+ if is_belongs_to ( field_name )
72+ association = field_name . partition ( ':' ) . first . to_sym
73+ association_field = field_name . partition ( ':' ) . last
6474
6575 unless @resource . reflect_on_association ( association )
6676 raise ForestLiana ::Errors ::HTTP422Error . new ( "Association '#{ association } ' not found" )
6777 end
6878
6979 current_resource = @resource . reflect_on_association ( association ) . klass
80+
81+ return association_field , current_resource
7082 else
71- association_field = field
72- current_resource = @resource
83+ return field_name , @resource
7384 end
85+ end
86+
87+ def parse_condition_without_smart_field ( condition )
88+ ensure_valid_condition ( condition )
89+
90+ operator = condition [ 'operator' ]
91+ value = condition [ 'value' ]
92+ field_name = condition [ 'field' ]
93+
94+ if @operator_date_parser . is_date_operator? ( operator )
95+ condition = @operator_date_parser . get_date_filter ( operator , value )
96+ return "#{ parse_field_name ( field_name ) } #{ condition } "
97+ end
98+
99+ association_field , current_resource = get_association_field_and_resource ( field_name )
74100
75101 # NOTICE: Set the integer value instead of a string if "enum" type
76102 # NOTICE: Rails 3 do not have a defined_enums method
77103 if current_resource . respond_to? ( :defined_enums ) && current_resource . defined_enums . has_key? ( association_field )
78104 value = current_resource . defined_enums [ association_field ] [ value ]
79105 end
80106
81- parsed_field = parse_field_name ( field )
107+ parsed_field = parse_field_name ( field_name )
82108 parsed_operator = parse_operator ( operator )
83109 parsed_value = parse_value ( operator , value )
84110 field_and_operator = "#{ parsed_field } #{ parsed_operator } "
@@ -149,16 +175,16 @@ def parse_field_name(field)
149175
150176 association = get_association_name_for_condition ( field )
151177 quoted_table_name = ActiveRecord ::Base . connection . quote_column_name ( association )
152- quoted_field_name = ActiveRecord :: Base . connection . quote_column_name ( field . split ( ':' ) [ 1 ] )
178+ field_name = field . split ( ':' ) [ 1 ]
153179 else
154180 quoted_table_name = @resource . quoted_table_name
155- quoted_field_name = ActiveRecord ::Base . connection . quote_column_name ( field )
156181 current_resource = @resource
182+ field_name = field
157183 end
184+ quoted_field_name = ActiveRecord ::Base . connection . quote_column_name ( field_name )
158185
159186 column_found = current_resource . columns . find { |column | column . name == field . split ( ':' ) . last }
160-
161- if column_found . nil?
187+ if column_found . nil? && !ForestLiana ::SchemaHelper . is_smart_field? ( current_resource , field_name )
162188 raise ForestLiana ::Errors ::HTTP422Error . new ( "Field '#{ field } ' not found" )
163189 end
164190
0 commit comments