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_BOOLEAN =
'boolean'- TYPE_DATE =
'date'- FORM_TYPE_EMPTY =
'empty'- FORM_TYPE_TEXT =
'text'- FORM_TYPE_HIDDEN =
'hidden'- FORM_TYPE_CHECKBOX =
'checkbox'- 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.
Class Method Summary collapse
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.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/simple_search_filter/filter_field.rb', line 47 def initialize(name, type, formtype, opt={}) @name = name.to_sym @type = type.to_s @formtype = formtype @options = opt @options||={} # some attributes self.condition= [:condition] unless [:condition].nil? end |
Instance Attribute Details
#formtype ⇒ Object (readonly)
Returns the value of attribute formtype.
41 42 43 |
# File 'lib/simple_search_filter/filter_field.rb', line 41 def formtype @formtype end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
39 40 41 |
# File 'lib/simple_search_filter/filter_field.rb', line 39 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
42 43 44 |
# File 'lib/simple_search_filter/filter_field.rb', line 42 def @options end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
40 41 42 |
# File 'lib/simple_search_filter/filter_field.rb', line 40 def type @type end |
Class Method Details
.fix_value_boolean(v) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/simple_search_filter/filter_field.rb', line 127 def self.fix_value_boolean(v) # if it is boolean => use as is return v if !!v==v return false if v.nil? # fix from other types res = false if v.is_a? String if v.blank? v = "0" end res = v.to_i == 1 elsif v.is_a? Fixnum res = v.to_i == 1 else res = !(v.empty?) end res end |
Instance Method Details
#condition ⇒ Object
97 98 99 |
# File 'lib/simple_search_filter/filter_field.rb', line 97 def condition @condition || [:condition] || QUERY_CONDITION_EQUAL end |
#condition=(cond) ⇒ Object
101 102 103 |
# File 'lib/simple_search_filter/filter_field.rb', line 101 def condition=(cond) @condition= CONDITIONS[cond.to_sym] end |
#condition_scope ⇒ Object
105 106 107 |
# File 'lib/simple_search_filter/filter_field.rb', line 105 def condition_scope @condition_scope || [:condition_scope] || nil end |
#condition_where ⇒ Object
109 110 111 |
# File 'lib/simple_search_filter/filter_field.rb', line 109 def condition_where @condition_where || [:condition_where] || nil end |
#default_value ⇒ Object
68 69 70 |
# File 'lib/simple_search_filter/filter_field.rb', line 68 def default_value [:default_value] end |
#fix_value(v) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/simple_search_filter/filter_field.rb', line 114 def fix_value(v) if type==TYPE_INT return (v.to_i rescue 0) elsif type==TYPE_STRING return v.to_s elsif type==TYPE_BOOLEAN return FilterField.fix_value_boolean(v) end v end |
#ignore_value ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/simple_search_filter/filter_field.rb', line 72 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
87 88 89 |
# File 'lib/simple_search_filter/filter_field.rb', line 87 def ignore_value?(v) v.nil? || v==ignore_value end |
#label ⇒ Object
64 65 66 |
# File 'lib/simple_search_filter/filter_field.rb', line 64 def label @options[:label] || '' end |
#make_where(v) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/simple_search_filter/filter_field.rb', line 154 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
60 61 62 |
# File 'lib/simple_search_filter/filter_field.rb', line 60 def placeholder @options[:placeholder] || @options[:label] || '' end |