Class: SuperSettings::Storage::ActiveRecordStorage

Inherits:
Object
  • Object
show all
Includes:
SuperSettings::Storage
Defined in:
lib/super_settings/storage/active_record_storage.rb,
lib/super_settings/storage/active_record_storage/models.rb

Overview

ActiveRecord implementation of the SuperSettings::Storage model.

To use this model, you must run the migration included with the gem. If the gem is mounted as an engine in a Rails applicationThe migration can be installed with

Examples:

rake app:super_settings:install:migrations

Defined Under Namespace

Classes: ApplicationRecord, HistoryModel, Model

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SuperSettings::Storage

#==, #create_history, #created_at, #created_at=, #deleted=, #deleted?, #description, #description=, included, #key, #key=, #persisted?, #raw_value, #raw_value=, #updated_at, #updated_at=, #value_type, #value_type=

Constructor Details

#initialize(attributes = {}) ⇒ ActiveRecordStorage

Returns a new instance of ActiveRecordStorage.



95
96
97
98
99
100
101
# File 'lib/super_settings/storage/active_record_storage.rb', line 95

def initialize(attributes = {})
  @model = if attributes.is_a?(Model)
    attributes
  else
    Model.new(attributes)
  end
end

Class Method Details

.activeObject



28
29
30
31
32
33
34
# File 'lib/super_settings/storage/active_record_storage.rb', line 28

def active
  if Model.available?
    Model.where(deleted: false).collect { |model| new(model) }
  else
    []
  end
end

.allObject



20
21
22
23
24
25
26
# File 'lib/super_settings/storage/active_record_storage.rb', line 20

def all
  if Model.available?
    Model.all.collect { |model| new(model) }
  else
    []
  end
end

.create_history(key:, changed_by:, created_at:, value: nil, deleted: false) ⇒ Object



55
56
57
# File 'lib/super_settings/storage/active_record_storage.rb', line 55

def create_history(key:, changed_by:, created_at:, value: nil, deleted: false)
  HistoryModel.create!(key: key, value: value, deleted: deleted, changed_by: changed_by, created_at: created_at)
end

.destroy_allObject



75
76
77
78
79
80
# File 'lib/super_settings/storage/active_record_storage.rb', line 75

def destroy_all
  Model.transaction do
    Model.delete_all
    HistoryModel.delete_all
  end
end

.find_by_key(key) ⇒ Object



44
45
46
47
# File 'lib/super_settings/storage/active_record_storage.rb', line 44

def find_by_key(key)
  model = Model.where(deleted: false).find_by(key: key) if Model.available?
  new(model) if model
end

.last_updated_atObject



49
50
51
52
53
# File 'lib/super_settings/storage/active_record_storage.rb', line 49

def last_updated_at
  if Model.available?
    Model.maximum(:updated_at)
  end
end

.transaction(&block) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/super_settings/storage/active_record_storage.rb', line 67

def transaction(&block)
  if Model.available?
    Model.transaction(&block)
  else
    yield
  end
end

.updated_since(time) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/super_settings/storage/active_record_storage.rb', line 36

def updated_since(time)
  if Model.available?
    Model.where("updated_at > ?", time).collect { |model| new(model) }
  else
    []
  end
end

.with_connection(&block) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/super_settings/storage/active_record_storage.rb', line 59

def with_connection(&block)
  if Model.available?
    Model.connection_pool.with_connection(&block)
  else
    yield
  end
end

Instance Method Details

#history(limit: nil, offset: 0) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/super_settings/storage/active_record_storage.rb', line 140

def history(limit: nil, offset: 0)
  finder = @model.history_items.order(id: :desc).offset(offset)
  finder = finder.limit(limit) if limit
  finder.collect do |record|
    HistoryItem.new(key: key, value: record.value, changed_by: record.changed_by, created_at: record.created_at, deleted: record.deleted?)
  end
end

#save!Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/super_settings/storage/active_record_storage.rb', line 103

def save!
  # Check if another record with the same key exists. If it does, then we need to update
  # that record instead and delete the current one.
  attempts = 0
  begin
    # Each attempt runs in a savepoint so a duplicate key failure can be rolled back
    # and retried; otherwise the surrounding transaction would be left in an aborted
    # state on PostgreSQL.
    @model.class.transaction(requires_new: true) do
      duplicate = @model.class.find_by(key: @model.key)
      if duplicate.nil? || duplicate == @model
        @model.save!
      else
        duplicate.raw_value = @model.raw_value
        duplicate.value_type = @model.value_type
        duplicate.description = @model.description
        duplicate.deleted = @model.deleted

        if @model.persisted?
          begin
            @model.reload.update!(deleted: true)
          rescue ActiveRecord::RecordNotFound
          end
        end
        duplicate.save!
        @model = duplicate
      end
    end
  rescue ActiveRecord::RecordNotUnique
    # A record with the same key was inserted concurrently; retry so it is
    # found as a duplicate and merged.
    attempts += 1
    retry if attempts <= 1
    raise
  end
end