Module: Practical::Forms::Datatables::Base
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/lib/practical/forms/datatables/base.rb
Instance Method Summary collapse
- #initialize(attributes = {}) ⇒ Object
- #inverted_sort_direction_for(key:) ⇒ Object
- #matches_schema? ⇒ Boolean
- #merged_payload(filters: nil, sort_key: nil, sort_direction: nil) ⇒ Object
- #normalize_sort_key_and_direction ⇒ Object
- #payload ⇒ Object
- #sanitize! ⇒ Object
- #sanitized? ⇒ Boolean
- #sort_direction_for(key:) ⇒ Object
Instance Method Details
#initialize(attributes = {}) ⇒ Object
13 14 15 16 17 18 19 |
# File 'app/lib/practical/forms/datatables/base.rb', line 13 def initialize(attributes = {}) super self.sanitize! if self.filters.present? self.filters = self.class.filter_class.new(self.filters) end end |
#inverted_sort_direction_for(key:) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'app/lib/practical/forms/datatables/base.rb', line 61 def inverted_sort_direction_for(key:) return "asc" unless key.downcase.strip == self.sort_key.downcase.strip case sort_direction when "asc" "desc" else "asc" end end |
#matches_schema? ⇒ Boolean
76 77 78 79 |
# File 'app/lib/practical/forms/datatables/base.rb', line 76 def matches_schema? return if self.class.schema.validate?(payload) errors.add(:base, :payload_does_not_match_schema) end |
#merged_payload(filters: nil, sort_key: nil, sort_direction: nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/lib/practical/forms/datatables/base.rb', line 33 def merged_payload(filters: nil, sort_key: nil, sort_direction: nil) result = payload result[:filters] ||= {} if filters.present? result[:filters].merge!(filters) end result[:sort_key] = sort_key if sort_key.present? result[:sort_direction] = sort_direction if sort_direction.present? return result end |
#normalize_sort_key_and_direction ⇒ Object
71 72 73 74 |
# File 'app/lib/practical/forms/datatables/base.rb', line 71 def normalize_sort_key_and_direction self.sort_key = sort_key.to_s.downcase.strip self.sort_direction = sort_direction.to_s.downcase.strip end |
#payload ⇒ Object
25 26 27 28 29 30 31 |
# File 'app/lib/practical/forms/datatables/base.rb', line 25 def payload { sort_key: sort_key, sort_direction: sort_direction, filters: filters.to_h, } end |
#sanitize! ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'app/lib/practical/forms/datatables/base.rb', line 46 def sanitize! validate! self.sanitized = true rescue ActiveModel::ValidationError self.attributes = self.class.default_payload self.sanitized = true self.errors.clear self.validate! end |
#sanitized? ⇒ Boolean
21 22 23 |
# File 'app/lib/practical/forms/datatables/base.rb', line 21 def sanitized? return self.sanitized == true end |
#sort_direction_for(key:) ⇒ Object
56 57 58 59 |
# File 'app/lib/practical/forms/datatables/base.rb', line 56 def sort_direction_for(key:) return nil unless key.downcase.strip == self.sort_key.downcase.strip return sort_direction end |