Class: BetterAuth::Rails::ActiveRecordAdapter
- Inherits:
-
Adapters::Base
- Object
- Adapters::Base
- BetterAuth::Rails::ActiveRecordAdapter
- Includes:
- Adapters::JoinSupport
- Defined in:
- lib/better_auth/rails/active_record_adapter.rb
Defined Under Namespace
Classes: ApplicationRecord
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #call(options) ⇒ Object
- #count(model:, where: nil) ⇒ Object
- #create(model:, data:, force_allow_id: false) ⇒ Object
- #delete(model:, where:) ⇒ Object
- #delete_many(model:, where:) ⇒ Object
- #find_many(model:, where: [], sort_by: nil, limit: nil, offset: nil, select: nil, join: nil) ⇒ Object
- #find_one(model:, where: [], select: nil, join: nil) ⇒ Object
-
#initialize(options = nil, connection: nil) ⇒ ActiveRecordAdapter
constructor
A new instance of ActiveRecordAdapter.
- #transaction ⇒ Object
- #update(model:, where:, update:) ⇒ Object
- #update_many(model:, where:, update:, returning: false) ⇒ Object
Constructor Details
#initialize(options = nil, connection: nil) ⇒ ActiveRecordAdapter
Returns a new instance of ActiveRecordAdapter.
29 30 31 32 33 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 29 def initialize( = nil, connection: nil) super() @connection = connection || ( ? ::ActiveRecord::Base : nil) @models = {} end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
27 28 29 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 27 def connection @connection end |
Instance Method Details
#call(options) ⇒ Object
35 36 37 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 35 def call() self.class.new(, connection: connection) end |
#count(model:, where: nil) ⇒ Object
89 90 91 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 89 def count(model:, where: nil) relation_for(model.to_s, where: where || []).count end |
#create(model:, data:, force_allow_id: false) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 39 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
80 81 82 83 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 80 def delete(model:, where:) delete_many(model: model, where: where) nil end |
#delete_many(model:, where:) ⇒ Object
85 86 87 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 85 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
50 51 52 53 54 55 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 50 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(model, records, join) : records end |
#find_one(model:, where: [], select: nil, join: nil) ⇒ Object
46 47 48 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 46 def find_one(model:, where: [], select: nil, join: nil) find_many(model: model, where: where, select: select, join: join, limit: 1).first end |
#transaction ⇒ Object
93 94 95 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 93 def transaction connection.connection.transaction { yield self } end |
#update(model:, where:, update:) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 57 def update(model:, where:, update:) model = model.to_s existing = find_one(model: model, where: where, select: ["id"]) return nil unless existing update_many(model: model, where: where, update: update) find_one(model: model, where: [{field: "id", value: existing.fetch("id")}]) end |
#update_many(model:, where:, update:, returning: false) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 66 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 |