Plugin settings.yml selection box

plugins

(CJ) #1

When creating a plugin and setting up the different attributes in the settings.yml, how do I create a selection box with a few static options?

In some of the options in the site it requires a ruby model, such as the S3 Region setting, which requires the S3RegionSiteSetting model s3_region_site_setting.rb

  s3_region:
    default: ''
    enum: 'S3RegionSiteSetting'

Is there a way to the enum options directly in the settings.yml like this:

my_plugin:
  my_first_string_option:
    client: true
    default: ''
  my_first_checkbox_option:
    client: true
    default: false
  my_selection_option:
    default: 1
    enum: 
      first_option: 'first option'
      second_option: 'second option'

When I try to use the model method for the plugin I get this error when including the model in plugins settings.yml

my_plugin:
  my_first_string_option:
    client: true
    default: ''
  my_first_checkbox_option:
    client: true
    default: false
  my_selection_option:
    client: 1
    enum: 'MySelectionSetting'

plugins/my_plugin/app/models/my_selection_setting.rb

require_dependency 'enum_site_setting'

class MySelectionSetting < EnumSiteSetting
  def self.valid_value?(val)
    valid_values.include? val
  end

  def self.values
    @values ||= valid_values.sort.map {|x| {name: x, value: x} }
  end

  def self.valid_values
    [ 'first_option',
      'second_option',
      'third_option']
  end

  private_class_method :valid_values
end

Error:

/usr/local/rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/inflector/methods.rb:238:in `const_get': uninitialized constant MySelectionSetting (NameError)
	from /usr/local/rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/inflector/methods.rb:238:in `block in constantize'
	from /usr/local/rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/inflector/methods.rb:236:in `each'
	from /usr/local/rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/inflector/methods.rb:236:in `inject'
	from /usr/local/rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/inflector/methods.rb:236:in `constantize'
	from /usr/local/rvm/gems/ruby-2.1.5/gems/activesupport-4.1.8/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
	from /vagrant/lib/site_setting_extension.rb:78:in `block in setting'
	from /vagrant/lib/site_setting_extension.rb:71:in `synchronize'
	from /vagrant/lib/site_setting_extension.rb:71:in `setting'
	from /vagrant/app/models/site_setting.rb:20:in `block in load_settings'
	from /vagrant/lib/site_settings/yaml_loader.rb:29:in `block (2 levels) in load'

I’m not sure where I’m going wrong here, which is why I was looking for a quick static method to provide several selection options.


(Robin Ward) #2

I think @neil added the custom options? Any insight?


(Sam Saffron) #3

Ahh yeah, you need to load the file that has the const in it, it will not take otherwise, eg: