Sha256: 137d3725f6ed0b98c7c784a7dfaa53614b222785ff8ab9901ee5ca300b10a263

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

module AbAdmin
  module Models
    module Settings
      extend ActiveSupport::Concern

      module ClassMethods
        def load
          configatron.configure_from_hash instance.all
          configatron
        end
      end

      def initialize
        @data = {}
        @editable_path = find_editable_path
        @paths = find_paths
      end

      def find_editable_path
        edited_settings_paths = [
            Rails.root.join('config', 'settings', "#{Rails.env}.local.yml"),
            Rails.root.join('config', 'settings', 'settings.local.yml')
        ]
        path = edited_settings_paths.detect { |path| File.exists?(path) }
        path or raise("Create settings file for editing: #{edited_settings_paths.join(' or ')}")
      end

      def find_paths
        [
            Rails.root.join('config', 'settings', 'settings.yml'),
            Rails.root.join('config', 'settings', "#{Rails.env}.yml"),
            @editable_path
        ].find_all { |path| File.exists?(path) }
      end

      def all
        @paths.each do |path|
          @data.deep_merge!(YAML.load_file(path))
        end
        @data
      end

      def editable
        YAML.load_file(@editable_path) rescue {}
      end

      def save(raw_config)
        conf = {}
        raw_config.each do |root_key, root_value|
          if root_value.is_a?(Hash)
            conf[root_key] ||= {}
            root_value.each do |key, value|
              conf[root_key][key] = case_value(value)
            end
          else
            conf[root_key] = case_value(root_value)
          end
        end
        File.open(@editable_path, 'w') { |file| file.write conf.to_yaml } and self.class.load
      end

      def case_value(value)
        if %w(true false 1 0).include?(value) || value.is_number?
          YAML::load(value)
        else
          value
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ab_admin-0.1.2 lib/ab_admin/models/settings.rb
ab_admin-0.1.1 lib/ab_admin/models/settings.rb
ab_admin-0.1.0 lib/ab_admin/models/settings.rb