Class: BlueprintConfig::Backend::ActiveRecord
- Defined in:
- lib/blueprint_config/backend/active_record.rb
Constant Summary collapse
- MISSING_TABLE_WARNING =
" =======================================================================\n Settings table not found. Please add the configuration table by running:\n\n rails generate blueprint_config:install\n rake db:migrate\n =======================================================================\n".gsub(/^ */, '')
Instance Method Summary collapse
- #fresh? ⇒ Boolean
-
#initialize(options = {}) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
- #load_keys ⇒ Object
- #update_timestamp ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
19 20 21 22 23 24 25 |
# File 'lib/blueprint_config/backend/active_record.rb', line 19 def initialize( = {}) = @updated_at = nil @last_checked_at = nil @configured = true @mutex = Thread::Mutex.new end |
Instance Method Details
#fresh? ⇒ Boolean
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/blueprint_config/backend/active_record.rb', line 52 def fresh? # if database is not create/configured yet - don't try to refresh settings from it return true unless @configured return true if @last_checked_at.present? && @last_checked_at > 1.second.ago @mutex.synchronize do @last_checked_at = Time.now end max_updated_at = Setting.maximum(:updated_at) # if there is no settings in the database - don't try to refresh settings from it' return true if max_updated_at.blank? @updated_at.present? && @updated_at >= max_updated_at end |
#load_keys ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/blueprint_config/backend/active_record.rb', line 27 def load_keys @configured = true data = Setting.all.map { |s| { s.key => s.parsed_value } }.reduce(:merge) || {} return data.transform_keys(&:to_sym) unless [:nest] nest_hash(data, [:nest_separator] || '.') rescue ::ActiveRecord::NoDatabaseError => e # database is not created yet @configured = false {} rescue ::ActiveRecord::StatementInvalid => e @configured = false Rails.logger.warn(e.) Rails.logger.warn(MISSING_TABLE_WARNING) {} end |
#update_timestamp ⇒ Object
46 47 48 49 50 |
# File 'lib/blueprint_config/backend/active_record.rb', line 46 def @mutex.synchronize do @updated_at = Setting.maximum(:updated_at) end end |