Class: Spree::Admin::Table::FilterGroup
- Inherits:
-
Object
- Object
- Spree::Admin::Table::FilterGroup
- Defined in:
- app/models/spree/admin/table/filter_group.rb
Constant Summary collapse
- COMBINATORS =
%i[and or].freeze
Instance Attribute Summary collapse
-
#combinator ⇒ Object
Returns the value of attribute combinator.
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#groups ⇒ Object
Returns the value of attribute groups.
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
-
.from_params(params) ⇒ FilterGroup
Create from params hash.
Instance Method Summary collapse
-
#add_filter(filter) ⇒ Object
Add a filter to this group.
-
#add_group(group) ⇒ Object
Add a nested group.
-
#empty? ⇒ Boolean
Check if group is empty.
-
#initialize(combinator: :and, filters: [], groups: [], id: nil) ⇒ FilterGroup
constructor
A new instance of FilterGroup.
-
#remove_filter(filter_id) ⇒ Object
Remove a filter by id.
-
#remove_group(group_id) ⇒ Object
Remove a nested group by id.
-
#to_h ⇒ Hash
Convert to hash for JSON serialization.
-
#to_ransack_params ⇒ Hash
Convert to ransack params format AND groups use regular params: { name_cont: ‘value’, status_eq: ‘active’ } OR groups use ransack groupings: { g: { ‘0’ => { m: ‘or’, c: { … } } } }.
Constructor Details
#initialize(combinator: :and, filters: [], groups: [], id: nil) ⇒ FilterGroup
Returns a new instance of FilterGroup.
9 10 11 12 13 14 |
# File 'app/models/spree/admin/table/filter_group.rb', line 9 def initialize(combinator: :and, filters: [], groups: [], id: nil) @combinator = combinator.to_sym @filters = filters @groups = groups @id = id || SecureRandom.hex(8) end |
Instance Attribute Details
#combinator ⇒ Object
Returns the value of attribute combinator.
7 8 9 |
# File 'app/models/spree/admin/table/filter_group.rb', line 7 def combinator @combinator end |
#filters ⇒ Object
Returns the value of attribute filters.
7 8 9 |
# File 'app/models/spree/admin/table/filter_group.rb', line 7 def filters @filters end |
#groups ⇒ Object
Returns the value of attribute groups.
7 8 9 |
# File 'app/models/spree/admin/table/filter_group.rb', line 7 def groups @groups end |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'app/models/spree/admin/table/filter_group.rb', line 7 def id @id end |
Class Method Details
.from_params(params) ⇒ FilterGroup
Create from params hash
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/models/spree/admin/table/filter_group.rb', line 72 def self.from_params(params) return new if params.blank? params = params.symbolize_keys if params.respond_to?(:symbolize_keys) group = new( combinator: params[:combinator] || :and, id: params[:id] ) Array(params[:filters]).each do |filter_params| filter = Filter.from_params(filter_params) group.add_filter(filter) if filter end Array(params[:groups]).each do |group_params| nested_group = from_params(group_params) group.add_group(nested_group) if nested_group end group end |
Instance Method Details
#add_filter(filter) ⇒ Object
Add a filter to this group
18 19 20 |
# File 'app/models/spree/admin/table/filter_group.rb', line 18 def add_filter(filter) @filters << filter end |
#add_group(group) ⇒ Object
Add a nested group
24 25 26 |
# File 'app/models/spree/admin/table/filter_group.rb', line 24 def add_group(group) @groups << group end |
#empty? ⇒ Boolean
Check if group is empty
42 43 44 |
# File 'app/models/spree/admin/table/filter_group.rb', line 42 def empty? @filters.empty? && @groups.empty? end |
#remove_filter(filter_id) ⇒ Object
Remove a filter by id
30 31 32 |
# File 'app/models/spree/admin/table/filter_group.rb', line 30 def remove_filter(filter_id) @filters.reject! { |f| f.id == filter_id } end |
#remove_group(group_id) ⇒ Object
Remove a nested group by id
36 37 38 |
# File 'app/models/spree/admin/table/filter_group.rb', line 36 def remove_group(group_id) @groups.reject! { |g| g.id == group_id } end |
#to_h ⇒ Hash
Convert to hash for JSON serialization
60 61 62 63 64 65 66 67 |
# File 'app/models/spree/admin/table/filter_group.rb', line 60 def to_h { combinator: combinator, filters: filters.map(&:to_h), groups: groups.map(&:to_h), id: id } end |
#to_ransack_params ⇒ Hash
Convert to ransack params format AND groups use regular params: { name_cont: ‘value’, status_eq: ‘active’ } OR groups use ransack groupings: { g: { ‘0’ => { m: ‘or’, c: { … } } } }
50 51 52 53 54 55 56 |
# File 'app/models/spree/admin/table/filter_group.rb', line 50 def to_ransack_params if combinator == :or to_or_ransack_params else to_and_ransack_params end end |