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
94 95 96 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 94 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
85 86 87 88 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 85 def delete(model:, where:) delete_many(model: model, where: where) nil end |
#delete_many(model:, where:) ⇒ Object
90 91 92 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 90 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
98 99 100 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 98 def transaction connection.connection.transaction { yield self } end |
#update(model:, where:, update:) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 57 def update(model:, where:, update:) model = model.to_s ensure_update_input_has_fields!(model, update) existing = find_one(model: model, where: where) return nil unless existing update_many(model: model, where: where, update: update) lookup = record_lookup(model, existing) lookup ? find_one(model: model, where: [lookup]) : find_one(model: model, where: where) end |
#update_many(model:, where:, update:, returning: false) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/better_auth/rails/active_record_adapter.rb', line 68 def update_many(model:, where:, update:, returning: false) model = model.to_s ensure_update_input_has_fields!(model, update) data = transform_input(model, update, "update", true) ensure_update_data!(data) attributes = physical_attributes(model, data) 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 |