Class: ActiveFilter::Base
- Inherits:
-
Object
- Object
- ActiveFilter::Base
- Extended by:
- ClassMethods
- Includes:
- Enumerable
- Defined in:
- lib/active_filter/base.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- TYPE_FILTER_MAP =
{ primary_key: Filter, string: StringFilter, text: TextFilter, integer: IntegerFilter, float: FloatFilter, decimal: DecimalFilter, datetime: DateTimeFilter, timestamp: Filter, time: TimeFilter, date: DateFilter, binary: Filter, boolean: BooleanFilter, }
Instance Attribute Summary collapse
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#initialize(data, scope = nil) ⇒ Base
constructor
A new instance of Base.
- #model ⇒ Object
- #to_scope ⇒ Object
Methods included from ClassMethods
Constructor Details
#initialize(data, scope = nil) ⇒ Base
Returns a new instance of Base.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_filter/base.rb', line 34 def initialize(data, scope=nil) @data = data @scope = scope columns = self.model.columns # fields で指定した列だけをフィルタ可能にする fields = self.class.__send__(:_fields) columns = _select_columns(columns, fields) unless fields.empty? # exclude で指定した列を除外する excludes = self.class.__send__(:_exclude) columns = _reject_columns(columns, excludes) unless excludes.empty? @filters = columns.map { |column| _create_filter_from_column(column) } end |
Instance Attribute Details
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
32 33 34 |
# File 'lib/active_filter/base.rb', line 32 def filters @filters end |
Instance Method Details
#each(&block) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/active_filter/base.rb', line 100 def each(&block) scope = self.to_scope unless block_given? return scope.each end scope.each do |obj| block.call(obj) end self end |
#model ⇒ Object
96 97 98 |
# File 'lib/active_filter/base.rb', line 96 def model self.class.__send__(:_model) end |
#to_scope ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/active_filter/base.rb', line 112 def to_scope scope = _scoped matched = false @filters.each do |filter| lookups = _lookups_from_filter(filter) lookups.each do |lookup, lookup_type| if @data.include?(lookup) converted_value = filter.convert_value(@data[lookup]) scope = filter.filter(scope, converted_value, lookup_type) matched = true elsif @data.include?(lookup.to_sym) converted_value = filter.convert_value(@data[lookup.to_sym]) scope = filter.filter(scope, converted_value, lookup_type) matched = true end end end # ユーザーの入力がフィルタにかからなかった場合は # 該当無しにする unless matched scope = scope.where("1 = 2") end self.class.__send__(:_orders).each do |order| scope = scope.order(order.to_s) end scope end |