Class: Filter::Base
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Filter::Base
- Includes:
- ActiveModel::Model
- Defined in:
- lib/filter/base.rb
Constant Summary collapse
- DEFAULTS =
{}
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#original_relation ⇒ Object
readonly
Returns the value of attribute original_relation.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
Instance Method Summary collapse
- #advance(record, advancement) ⇒ Object
- #fork(attributes: {}, skip: []) ⇒ Object
- #ids ⇒ Object
- #index(record) ⇒ Object
-
#initialize(attributes: {}, relation: nil) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #next(current_record) ⇒ Object
- #previous(current_record) ⇒ Object
- #to_query ⇒ Object
- #to_url(path = nil) ⇒ Object
Constructor Details
#initialize(attributes: {}, relation: nil) ⇒ Base
Returns a new instance of Base.
12 13 14 15 16 |
# File 'lib/filter/base.rb', line 12 def initialize(attributes: {}, relation: nil) @original_relation = relation || default_relation setup_ostruct(attributes) perform_filtering end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/filter/base.rb', line 5 def attributes @attributes end |
#original_relation ⇒ Object (readonly)
Returns the value of attribute original_relation.
5 6 7 |
# File 'lib/filter/base.rb', line 5 def original_relation @original_relation end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
5 6 7 |
# File 'lib/filter/base.rb', line 5 def relation @relation end |
Instance Method Details
#advance(record, advancement) ⇒ Object
33 34 35 36 |
# File 'lib/filter/base.rb', line 33 def advance(record, advancement) advanced_id = ids[(index(record) + advancement) % ids.count] relation.find(advanced_id) end |
#fork(attributes: {}, skip: []) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/filter/base.rb', line 18 def fork(attributes: {}, skip: []) skip = Array.wrap(skip) attributes = to_h.merge(attributes) attributes.delete_if { |k, v| skip.include?(k) } self.class.new(attributes: attributes, relation: original_relation) end |
#ids ⇒ Object
42 43 44 |
# File 'lib/filter/base.rb', line 42 def ids @ids ||= relation.pluck(:id).uniq end |
#index(record) ⇒ Object
38 39 40 |
# File 'lib/filter/base.rb', line 38 def index(record) ids.index(record.id) end |
#inspect ⇒ Object
55 56 57 |
# File 'lib/filter/base.rb', line 55 def inspect super.gsub('OpenStruct', "Filter (#{original_relation.to_s})") end |
#next(current_record) ⇒ Object
25 26 27 |
# File 'lib/filter/base.rb', line 25 def next(current_record) advance(current_record, 1) end |
#previous(current_record) ⇒ Object
29 30 31 |
# File 'lib/filter/base.rb', line 29 def previous(current_record) advance(current_record, -1) end |
#to_query ⇒ Object
51 52 53 |
# File 'lib/filter/base.rb', line 51 def to_query { filter_base: to_h }.to_query end |
#to_url(path = nil) ⇒ Object
46 47 48 49 |
# File 'lib/filter/base.rb', line 46 def to_url(path = nil) path ||= '' "#{path}?"+to_query end |