Sha256: 53a5f9b6ca3ac04c69b80b28bc7ee18eeeb78d673d237fe2e080010bdaad2954

Contents?: true

Size: 1.52 KB

Versions: 55

Compression:

Stored size: 1.52 KB

Contents

require 'active_support/concern'

module ActiveAdmin

  # Adds a class method to a class to create settings with default values.
  #
  # Example:
  #
  #   class Configuration
  #     include ActiveAdmin::Settings
  #
  #     setting :site_title, "Default Site Title"
  #   end
  #
  #   conf = Configuration.new
  #   conf.site_title #=> "Default Site Title"
  #   conf.site_title = "Override Default"
  #   conf.site_title #=> "Override Default"
  #
  module Settings
    extend ActiveSupport::Concern

    def read_default_setting(name)
      default_settings[name]
    end

    private

    def default_settings
      self.class.default_settings
    end

    module ClassMethods

      def setting(name, default)
        default_settings[name] = default
        attr_accessor(name)

        # Create an accessor that grabs from the defaults
        # if @name has not been set yet
        class_eval <<-EOC, __FILE__, __LINE__ + 1
          def #{name}
            if instance_variable_defined? :@#{name}
              @#{name}
            else
              read_default_setting(:#{name})
            end
          end
        EOC
      end

      def deprecated_setting(name, default, message = nil)
        message = message || "The #{name} setting is deprecated and will be removed."
        setting(name, default)

        ActiveAdmin::Deprecation.deprecate self, name, message
        ActiveAdmin::Deprecation.deprecate self, :"#{name}=", message
      end

      def default_settings
        @default_settings ||= {}
      end

    end
  end
end

Version data entries

55 entries across 55 versions & 2 rubygems

Version Path
lalala-4.0.0.dev.141 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.136 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.135 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.134 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.133 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.132 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.131 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.129 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.128 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.126 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.125 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.124 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.123 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.118 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.116 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.114 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.113 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.111 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.109 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb
lalala-4.0.0.dev.107 vendor/deps/active_admin/lib/active_admin/helpers/settings.rb