Class: BlueprintConfig::Backend::ActiveRecord
- Defined in:
- lib/blueprint_config/backend/active_record.rb
Constant Summary collapse
- MISSING_TABLE_WARNING =
<<-WARNING.gsub(/^ */, '') ======================================================================= Settings table not found. Please add the configuration table by running: bundle exec rails generate blueprint_config:install bundle exec rake db:migrate ======================================================================= WARNING
- MISSING_ATTRIBUTES_WARNING =
<<-WARNING.gsub(/^ */, '') ======================================================================= Settings table is missing required attributes: %s You can create a migration and adjust it to your needs by running: bundle exec rails generate blueprint_config:install ======================================================================= WARNING
- REQUIRED_ATTRIBUTES =
%w[key value type updated_at].freeze
Instance Method Summary collapse
- #fresh? ⇒ Boolean
- #has_required_attributes? ⇒ Boolean
-
#initialize(options = {}) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
- #load_keys ⇒ Object
- #table_exist? ⇒ Boolean
- #update_timestamp ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
30 31 32 33 34 35 36 |
# File 'lib/blueprint_config/backend/active_record.rb', line 30 def initialize( = {}) @options = @updated_at = nil @last_checked_at = nil @configured = true @mutex = Thread::Mutex.new end |
Instance Method Details
#fresh? ⇒ Boolean
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/blueprint_config/backend/active_record.rb', line 102 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 |
#has_required_attributes? ⇒ Boolean
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/blueprint_config/backend/active_record.rb', line 79 def has_required_attributes? Setting.reset_column_information return true if REQUIRED_ATTRIBUTES - Setting.attribute_names == [] @configured = false missing = (REQUIRED_ATTRIBUTES - Setting.attribute_names).join(', ') unless @options[:silence_warnings] puts MISSING_ATTRIBUTES_WARNING % missing if defined?(Rails) Rails.logger.warn(MISSING_TABLE_WARNING) end end false end |
#load_keys ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/blueprint_config/backend/active_record.rb', line 38 def load_keys @configured = true return {} unless table_exist? return {} unless has_required_attributes? data = Setting.all.map { |s| { s.key => s.parsed_value } }.reduce(:merge) || {} return data.transform_keys(&:to_sym) unless @options[:nest] nest_hash(data, @options[:nest_separator] || '.') rescue ::ActiveRecord::NoDatabaseError, ::ActiveRecord::ConnectionNotEstablished # database is not created yet @configured = false {} rescue ::ActiveRecord::StatementInvalid => e @configured = false unless @options[:silence_warnings] puts "Failed to load seetings from database: #{e.}" Rails.logger.warn(e.) if defined?(Rails) end {} end |
#table_exist? ⇒ Boolean
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/blueprint_config/backend/active_record.rb', line 63 def table_exist? Setting.reset_column_information return true if Setting.table_exists? @configured = false unless @options[:silence_warnings] puts MISSING_TABLE_WARNING if defined?(Rails) Rails.logger.warn(MISSING_TABLE_WARNING) end end false end |
#update_timestamp ⇒ Object
96 97 98 99 100 |
# File 'lib/blueprint_config/backend/active_record.rb', line 96 def @mutex.synchronize do @updated_at = Setting.maximum(:updated_at) end end |