Class: Databasium::Record
- Inherits:
-
Object
- Object
- Databasium::Record
- Defined in:
- app/services/databasium/record.rb
Instance Method Summary collapse
- #bulk_destroy(ids) ⇒ Object
- #create_new(attributes:) ⇒ Object
- #filter_records(filter) ⇒ Object
-
#initialize(model: nil) ⇒ Record
constructor
A new instance of Record.
- #update(record, params) ⇒ Object
- #update_by_id(id, attributes:) ⇒ Object
Constructor Details
#initialize(model: nil) ⇒ Record
Returns a new instance of Record.
2 3 4 |
# File 'app/services/databasium/record.rb', line 2 def initialize(model: nil) @model = model end |
Instance Method Details
#bulk_destroy(ids) ⇒ Object
25 26 27 28 |
# File 'app/services/databasium/record.rb', line 25 def bulk_destroy(ids) return nil if @model.blank? || ids.blank? @model.where(id: ids).destroy_all end |
#create_new(attributes:) ⇒ Object
11 12 13 14 |
# File 'app/services/databasium/record.rb', line 11 def create_new(attributes:) return false unless @model @model.create(attributes) end |
#filter_records(filter) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/services/databasium/record.rb', line 30 def filter_records(filter) records = @model&.all return records if filter.nil? || @model.nil? connectors = Array(filter[:operator_types]).map(&:to_s) allowed_operators = %w[eq not_eq gt lt gteq lteq matches does_not_match] combined_predicate = nil predicate_index = 0 filter .except(:operator_types) .each do |name, value| next if value[:operator].blank? || value[:value].blank? operator = value[:operator].to_s next unless allowed_operators.include?(operator) column = @model.arel_table[name] predicate_value = value[:value].to_s predicate_value = "%#{predicate_value}%" if %w[matches does_not_match].include?(operator) current_predicate = column.public_send(operator, predicate_value) if combined_predicate.nil? combined_predicate = current_predicate else connector = connectors[predicate_index - 1] == "or" ? :or : :and combined_predicate = combined_predicate.public_send(connector, current_predicate) end predicate_index += 1 end return records if combined_predicate.nil? records.where(combined_predicate) end |
#update(record, params) ⇒ Object
6 7 8 9 |
# File 'app/services/databasium/record.rb', line 6 def update(record, params) return false unless record record.update(params) end |
#update_by_id(id, attributes:) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'app/services/databasium/record.rb', line 16 def update_by_id(id, attributes:) return nil unless @model || id.blank? record = @model.find(id) return nil unless record record.update(attributes) record end |