FIX: keeps initial key of the custom field

FIX: keeps initial key of the custom field

Custom fields can be defined with dashes or underscores, both should work and be returned using this format in the json.

Although, in the raw of the post, the format will be camelcase as this is only what’s supported by bbode.

diff --git a/assets/javascripts/lib/raw-event-helper.js.es6 b/assets/javascripts/lib/raw-event-helper.js.es6
index bc4bbde..c29c570 100644
--- a/assets/javascripts/lib/raw-event-helper.js.es6
+++ b/assets/javascripts/lib/raw-event-helper.js.es6
@@ -89,8 +89,8 @@ export function replaceRaw(params, raw) {
 function camelCase(input) {
   return input
     .toLowerCase()
-    .replace(/_/g, "-")
-    .replace(/-(.)/g, function (match, group1) {
+    .replace(/-/g, "_")
+    .replace(/_(.)/g, function (match, group1) {
       return group1.toUpperCase();
     });
 }
diff --git a/lib/discourse_post_event/event_parser.rb b/lib/discourse_post_event/event_parser.rb
index 5e142f9..bc772d1 100644
--- a/lib/discourse_post_event/event_parser.rb
+++ b/lib/discourse_post_event/event_parser.rb
@@ -17,8 +17,12 @@ module DiscoursePostEvent
       cooked = PrettyText.cook(post.raw, topic_id: post.topic_id, user_id: post.user_id)
       valid_options = VALID_OPTIONS.map { |o| "data-#{o}" }
 
+      valid_custom_fields = []
       SiteSetting.discourse_post_event_allowed_custom_fields.split('|').each do |setting|
-        valid_options << "data-#{setting}"
+        valid_custom_fields << {
+          original: "data-#{setting}",
+          normalized: "data-#{setting.gsub(/_/, '-')}"
+        }
       end
 
       Nokogiri::HTML(cooked).css('div.discourse-post-event').map do |doc|
@@ -27,9 +31,16 @@ module DiscoursePostEvent
           name = attribute.name
           value = attribute.value
 
-          if valid_options.include?(name) && value
+          if value && valid_options.include?(name)
             event ||= {}
-            event[name['data-'.length..-1].to_sym] = CGI.escapeHTML(value)
+            event[name.sub('data-', '').to_sym] = CGI.escapeHTML(value)
+          end
+
+          valid_custom_fields.each do |valid_custom_field|
+            if value && valid_custom_field[:normalized] == name
+              event ||= {}
+              event[valid_custom_field[:original].sub('data-', '').to_sym] = CGI.escapeHTML(value)
+            end
           end
         end
         event
diff --git a/spec/acceptance/allowed_custom_fields_setting.rb b/spec/acceptance/allowed_custom_fields_setting.rb
index 3a2f2df..f9a11f8 100644
--- a/spec/acceptance/allowed_custom_fields_setting.rb
+++ b/spec/acceptance/allowed_custom_fields_setting.rb
@@ -11,11 +11,12 @@ describe 'discourse_post_event_allowed_custom_fields' do
   let(:post_event_1) { Fabricate(:event, post: post_1) }
 
   before do
-    SiteSetting.discourse_post_event_allowed_custom_fields = 'foo|bar'
+    SiteSetting.discourse_post_event_allowed_custom_fields = 'foo|bar|foo-bar|foo_baz'
     post_event_1.update!(custom_fields: {})
 
     SiteSetting.calendar_enabled = true
     SiteSetting.discourse_post_event_enabled = true
+    Jobs.run_immediately!
   end
 
   it 'removes the key on the custom fields when removing a key from site setting' do
@@ -37,4 +38,18 @@ describe 'discourse_post_event_allowed_custom_fields' do
       post_event_1.update!(custom_fields: { baz: 3 })
     }.to raise_error(ActiveRecord::RecordInvalid)
   end
+
+  it 'works with a setting containing dash' do
+    post = create_post_with_event(user_1, "fooBar='1'")
+    expect(post.event.custom_fields['foo-bar']).to eq('1')
+    expect(post.event.custom_fields['foo_bar']).to eq(nil)
+    expect(post.event.custom_fields['fooBar']).to eq(nil)
+  end
+
+  it 'works with a setting containing underscore' do
+    post = create_post_with_event(user_1, "fooBaz='1'")
+    expect(post.event.custom_fields['foo_baz']).to eq('1')
+    expect(post.event.custom_fields['foo-baz']).to eq(nil)
+    expect(post.event.custom_fields['fooBaz']).to eq(nil)
+  end
 end

GitHub sha: 45712d30