Class: SimpleSearchFilter::FilterField
- Inherits:
-
Object
- Object
- SimpleSearchFilter::FilterField
- Defined in:
- lib/simple_search_filter/filter_field.rb
Constant Summary collapse
- TYPE_STRING =
'string'- TYPE_INT =
'int'- TYPE_DATE =
'date'- FORM_TYPE_EMPTY =
'empty'- FORM_TYPE_TEXT =
'text'- FORM_TYPE_HIDDEN =
'hidden'- FORM_TYPE_SELECT =
drop-down select
'select'- FORM_TYPE_AUTOCOMPLETE =
text with autocomplete
'autocomplete'- QUERY_CONDITION_EMPTY =
condition types
'empty'- QUERY_CONDITION_CUSTOM =
'custom'- QUERY_CONDITION_EQUAL =
'equal'- QUERY_CONDITION_LIKE =
'like'- QUERY_CONDITION_LIKE_FULL =
'like_full'- QUERY_CONDITION_LIKE_LEFT =
'like_left'- QUERY_CONDITION_LIKE_RIGHT =
'like_right'- CONDITIONS =
{ :empty => QUERY_CONDITION_EMPTY, :custom => QUERY_CONDITION_CUSTOM, :equal => QUERY_CONDITION_EQUAL, :like => QUERY_CONDITION_LIKE, :like_full => QUERY_CONDITION_LIKE_FULL, :like_left=>QUERY_CONDITION_LIKE_LEFT, :like_right=>QUERY_CONDITION_LIKE_RIGHT }
Instance Attribute Summary collapse
-
#formtype ⇒ Object
readonly
Returns the value of attribute formtype.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #condition ⇒ Object
- #condition=(cond) ⇒ Object
- #condition_scope ⇒ Object
- #condition_where ⇒ Object
- #default_value ⇒ Object
- #fix_value(v) ⇒ Object
- #ignore_value ⇒ Object
- #ignore_value?(v) ⇒ Boolean
-
#initialize(name, type, formtype, opt = {}) ⇒ FilterField
constructor
A new instance of FilterField.
- #label ⇒ Object
- #make_where(v) ⇒ Object
- #placeholder ⇒ Object
Constructor Details
#initialize(name, type, formtype, opt = {}) ⇒ FilterField
Returns a new instance of FilterField.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/simple_search_filter/filter_field.rb', line 44 def initialize(name, type, formtype, opt={}) @name = name.to_sym @type = type.to_s @formtype = formtype = opt ||={} # some attributes self.condition= [:condition] unless [:condition].nil? end |
Instance Attribute Details
#formtype ⇒ Object (readonly)
Returns the value of attribute formtype.
38 39 40 |
# File 'lib/simple_search_filter/filter_field.rb', line 38 def formtype @formtype end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
36 37 38 |
# File 'lib/simple_search_filter/filter_field.rb', line 36 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
39 40 41 |
# File 'lib/simple_search_filter/filter_field.rb', line 39 def end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
37 38 39 |
# File 'lib/simple_search_filter/filter_field.rb', line 37 def type @type end |
Instance Method Details
#condition ⇒ Object
94 95 96 |
# File 'lib/simple_search_filter/filter_field.rb', line 94 def condition @condition || [:condition] || QUERY_CONDITION_EQUAL end |
#condition=(cond) ⇒ Object
98 99 100 |
# File 'lib/simple_search_filter/filter_field.rb', line 98 def condition=(cond) @condition= CONDITIONS[cond.to_sym] end |
#condition_scope ⇒ Object
102 103 104 |
# File 'lib/simple_search_filter/filter_field.rb', line 102 def condition_scope @condition_scope || [:condition_scope] || nil end |
#condition_where ⇒ Object
106 107 108 |
# File 'lib/simple_search_filter/filter_field.rb', line 106 def condition_where @condition_where || [:condition_where] || nil end |
#default_value ⇒ Object
65 66 67 |
# File 'lib/simple_search_filter/filter_field.rb', line 65 def default_value [:default_value] || nil end |
#fix_value(v) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/simple_search_filter/filter_field.rb', line 111 def fix_value(v) if type==TYPE_INT return (v.to_i rescue 0) elsif type==TYPE_STRING return v.to_s end v end |
#ignore_value ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/simple_search_filter/filter_field.rb', line 69 def ignore_value v = [:ignore_value] return v unless v.nil? # default ignore value from type t = type.to_s if t==TYPE_INT return 0 elsif t==TYPE_STRING return '' end nil end |
#ignore_value?(v) ⇒ Boolean
84 85 86 |
# File 'lib/simple_search_filter/filter_field.rb', line 84 def ignore_value?(v) v.nil? || v==ignore_value end |
#label ⇒ Object
61 62 63 |
# File 'lib/simple_search_filter/filter_field.rb', line 61 def label [:label] || '' end |
#make_where(v) ⇒ Object
124 125 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 |
# File 'lib/simple_search_filter/filter_field.rb', line 124 def make_where(v) cond = condition # ignore return nil if ignore_value? v # make where if cond==QUERY_CONDITION_EMPTY return nil elsif cond==QUERY_CONDITION_EQUAL return ["#{name}= ? ", v] elsif cond==QUERY_CONDITION_LIKE return ["#{name} LIKE ? ", v] elsif cond==QUERY_CONDITION_LIKE_LEFT return ["#{name} LIKE ? ", v+'%'] elsif cond==QUERY_CONDITION_LIKE_RIGHT return ["#{name} LIKE ? ", '%'+v] elsif cond==QUERY_CONDITION_LIKE_FULL return ["#{name} LIKE ? ", '%'+v+'%'] elsif cond==QUERY_CONDITION_CUSTOM w = condition_where if w.present? return [w, v] end end return nil end |
#placeholder ⇒ Object
57 58 59 |
# File 'lib/simple_search_filter/filter_field.rb', line 57 def placeholder [:placeholder] || '' end |