Class: BetterAuth::Rails::ActiveRecordAdapter

Inherits:
Adapters::Base
  • Object
show all
Defined in:
lib/better_auth/rails/active_record_adapter.rb

Defined Under Namespace

Classes: ApplicationRecord

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, connection: nil) ⇒ ActiveRecordAdapter

Returns a new instance of ActiveRecordAdapter.



23
24
25
26
27
# File 'lib/better_auth/rails/active_record_adapter.rb', line 23

def initialize(options, connection: nil)
  super(options)
  @connection = connection || ::ActiveRecord::Base
  @models = {}
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



21
22
23
# File 'lib/better_auth/rails/active_record_adapter.rb', line 21

def connection
  @connection
end

Instance Method Details

#count(model:, where: nil) ⇒ Object



81
82
83
# File 'lib/better_auth/rails/active_record_adapter.rb', line 81

def count(model:, where: nil)
  relation_for(model.to_s, where: where || []).count
end

#create(model:, data:, force_allow_id: false) ⇒ Object



29
30
31
32
33
34
# File 'lib/better_auth/rails/active_record_adapter.rb', line 29

def create(model:, data:, force_allow_id: false)
  model = model.to_s
  input = transform_input(model, data, "create", force_allow_id)
  record = model_class(model).create!(physical_attributes(model, input))
  normalize_record(model, record)
end

#delete(model:, where:) ⇒ Object



70
71
72
73
74
75
# File 'lib/better_auth/rails/active_record_adapter.rb', line 70

def delete(model:, where:)
  model = model.to_s
  record = relation_for(model, where: where).first
  record&.destroy!
  nil
end

#delete_many(model:, where:) ⇒ Object



77
78
79
# File 'lib/better_auth/rails/active_record_adapter.rb', line 77

def delete_many(model:, where:)
  relation_for(model.to_s, where: where).delete_all
end

#find_many(model:, where: [], sort_by: nil, limit: nil, offset: nil, select: nil, join: nil) ⇒ Object



40
41
42
43
44
45
# File 'lib/better_auth/rails/active_record_adapter.rb', line 40

def find_many(model:, where: [], sort_by: nil, limit: nil, offset: nil, select: nil, join: nil)
  model = model.to_s
  relation = relation_for(model, where: where, sort_by: sort_by, limit: limit, offset: offset, select: select, join: join)
  records = relation.map { |record| normalize_record(model, record, join: join) }
  collection_join?(model, join) ? aggregate_collection_joins(records) : records
end

#find_one(model:, where: [], select: nil, join: nil) ⇒ Object



36
37
38
# File 'lib/better_auth/rails/active_record_adapter.rb', line 36

def find_one(model:, where: [], select: nil, join: nil)
  find_many(model: model, where: where, select: select, join: join, limit: 1).first
end

#transactionObject



85
86
87
# File 'lib/better_auth/rails/active_record_adapter.rb', line 85

def transaction
  connection.connection.transaction { yield self }
end

#update(model:, where:, update:) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/better_auth/rails/active_record_adapter.rb', line 47

def update(model:, where:, update:)
  model = model.to_s
  record = relation_for(model, where: where).first
  return nil unless record

  record.update!(physical_attributes(model, transform_input(model, update, "update", true)))
  normalize_record(model, record)
end

#update_many(model:, where:, update:, returning: false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/better_auth/rails/active_record_adapter.rb', line 56

def update_many(model:, where:, update:, returning: false)
  model = model.to_s
  attributes = physical_attributes(model, transform_input(model, update, "update", true))
  relation = relation_for(model, where: where)
  if returning
    relation.map do |record|
      record.update!(attributes)
      normalize_record(model, record)
    end
  else
    relation.update_all(attributes)
  end
end