From 811a0df68b5ad206bd0145bbb6177086cba11945 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 11 Jun 2013 14:23:52 -0400 Subject: [PATCH] Make s3 region site setting a drop down --- .../javascripts/admin/models/site_setting.js | 8 +++++-- .../site_settings/setting_enum.js.handlebars | 2 +- .../discourse/views/combobox_view.js | 2 +- app/models/s3_region_site_setting.rb | 9 ++++++++ app/models/site_setting.rb | 2 +- config/locales/client.en.yml | 1 + .../admin/site_settings_controller_spec.rb | 4 ++++ spec/models/s3_region_site_setting_spec.rb | 21 +++++++++++++++++++ 8 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 app/models/s3_region_site_setting.rb create mode 100644 spec/models/s3_region_site_setting_spec.rb diff --git a/app/assets/javascripts/admin/models/site_setting.js b/app/assets/javascripts/admin/models/site_setting.js index 5768208ad..3824c295c 100644 --- a/app/assets/javascripts/admin/models/site_setting.js +++ b/app/assets/javascripts/admin/models/site_setting.js @@ -83,10 +83,14 @@ Discourse.SiteSetting = Discourse.Model.extend({ validValues: function() { var vals; vals = Em.A(); - this.get("valid_values").each(function(v){ - vals.addObject({ name: v, value: v }); + _.each(this.get('valid_values'), function(v) { + if(v.length > 0) vals.addObject({ name: v, value: v }); }); return vals; + }.property('valid_values'), + + allowsNone: function() { + if ( _.indexOf(this.get('valid_values'), '') >= 0 ) return 'admin.site_settings.none'; }.property('valid_values') }); diff --git a/app/assets/javascripts/admin/templates/site_settings/setting_enum.js.handlebars b/app/assets/javascripts/admin/templates/site_settings/setting_enum.js.handlebars index a3d7920eb..b8e8addd9 100644 --- a/app/assets/javascripts/admin/templates/site_settings/setting_enum.js.handlebars +++ b/app/assets/javascripts/admin/templates/site_settings/setting_enum.js.handlebars @@ -3,7 +3,7 @@

{{unbound setting}}

- {{combobox valueAttribute="value" content=validValues value=value}} + {{combobox valueAttribute="value" content=validValues value=value none=allowsNone}}
{{unbound description}}
{{#if dirty}} diff --git a/app/assets/javascripts/discourse/views/combobox_view.js b/app/assets/javascripts/discourse/views/combobox_view.js index 18a6a9fc4..9e7ec10e2 100644 --- a/app/assets/javascripts/discourse/views/combobox_view.js +++ b/app/assets/javascripts/discourse/views/combobox_view.js @@ -15,7 +15,7 @@ Discourse.ComboboxView = Discourse.View.extend({ // Add none option if required if (this.get('none')) { - buffer.push(""); + buffer.push('"); } var selected = this.get('value'); diff --git a/app/models/s3_region_site_setting.rb b/app/models/s3_region_site_setting.rb new file mode 100644 index 000000000..dda532580 --- /dev/null +++ b/app/models/s3_region_site_setting.rb @@ -0,0 +1,9 @@ +class S3RegionSiteSetting + def self.valid_value?(val) + all_values.include? val + end + + def self.all_values + @all_values ||= ['', 'us-east-1', 'us-west-1', 'us-west-2', 'ap-southeast-1', 'ap-southeast-2', 'ap-northeast-1', 'sa-east-1'].sort + end +end \ No newline at end of file diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index acdf6ff46..527aadac8 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -168,7 +168,7 @@ class SiteSetting < ActiveRecord::Base setting(:enable_s3_uploads, false) setting(:s3_access_key_id, '') setting(:s3_secret_access_key, '') - setting(:s3_region, '') + setting(:s3_region, '', enum: 'S3RegionSiteSetting') setting(:s3_upload_bucket, '') setting(:default_trust_level, 0) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 75e63eb5f..cfe794c7d 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1183,3 +1183,4 @@ en: show_overriden: 'Only show overridden' title: 'Settings' reset: 'reset to default' + none: 'none' diff --git a/spec/controllers/admin/site_settings_controller_spec.rb b/spec/controllers/admin/site_settings_controller_spec.rb index 1644ff2e3..671347425 100644 --- a/spec/controllers/admin/site_settings_controller_spec.rb +++ b/spec/controllers/admin/site_settings_controller_spec.rb @@ -34,6 +34,10 @@ describe Admin::SiteSettingsController do xhr :put, :update, id: 'test_setting', value: 'hello' end + # it 'allows value to be a blank string' do + # SiteSetting.expects(:'test_setting=').with('').once + # xhr :put, :update, id: 'test_setting', value: '' + # end end end diff --git a/spec/models/s3_region_site_setting_spec.rb b/spec/models/s3_region_site_setting_spec.rb new file mode 100644 index 000000000..312f52627 --- /dev/null +++ b/spec/models/s3_region_site_setting_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe S3RegionSiteSetting do + + describe 'valid_value?' do + it 'returns true for a valid S3 region' do + expect(S3RegionSiteSetting.valid_value?('us-west-1')).to eq(true) + end + + it 'returns false for an invalid S3 region' do + expect(S3RegionSiteSetting.valid_value?('the-moon')).to eq(false) + end + end + + describe 'all_values' do + it 'returns all the S3 regions and blank' do + expect(S3RegionSiteSetting.all_values.sort).to eq(['', 'us-east-1', 'us-west-1', 'us-west-2', 'ap-southeast-1', 'ap-southeast-2', 'ap-northeast-1', 'sa-east-1'].sort) + end + end + +end \ No newline at end of file