FIX: validates 'ThemeField' name when used in a SCSS variable

This commit is contained in:
Régis Hanol
2017-12-19 16:10:44 +01:00
parent ca8e4dfb43
commit 24e89b6b38
3 changed files with 32 additions and 14 deletions

View File

@ -16,16 +16,17 @@ export default Ember.Controller.extend(ModalFunctionality, {
@computed('name') @computed('name')
nameValid(name) { nameValid(name) {
return name && name.match(/^[a-zA-Z0-9-_]+$/); return name && name.match(/\A[a-z_][a-z0-9_-]*\z/i);
}, },
@observes('name') @observes('name')
uploadChanged(){ uploadChanged() {
let file = $('#file-input')[0]; const file = $('#file-input')[0];
this.set('fileSelected', file && file.files[0]); this.set('fileSelected', file && file.files[0]);
}, },
actions: { actions: {
updateName() { updateName() {
let name = this.get('name'); let name = this.get('name');
if (Em.isEmpty(name)) { if (Em.isEmpty(name)) {
@ -34,20 +35,21 @@ export default Ember.Controller.extend(ModalFunctionality, {
} }
this.uploadChanged(); this.uploadChanged();
}, },
upload() {
let options = { upload() {
type: 'POST' const file = $('#file-input')[0].files[0];
const options = {
type: 'POST',
processData: false,
contentType: false,
data: new FormData()
}; };
options.processData = false;
options.contentType = false;
options.data = new FormData();
let file = $('#file-input')[0].files[0];
options.data.append('file', file); options.data.append('file', file);
ajax('/admin/themes/upload_asset', options).then(result=>{ ajax('/admin/themes/upload_asset', options).then(result => {
let upload = { const upload = {
upload_id: result.upload_id, upload_id: result.upload_id,
name: this.get('name'), name: this.get('name'),
original_filename: file.name original_filename: file.name
@ -57,7 +59,6 @@ export default Ember.Controller.extend(ModalFunctionality, {
}).catch(e => { }).catch(e => {
popupAjaxError(e); popupAjaxError(e);
}); });
} }
} }
}); });

View File

@ -14,6 +14,9 @@ class ThemeField < ActiveRecord::Base
@theme_var_type_ids ||= [2, 3, 4] @theme_var_type_ids ||= [2, 3, 4]
end end
validates :name, format: { with: /\A[a-z_][a-z0-9_-]*\z/i },
if: Proc.new { |field| ThemeField.theme_var_type_ids.include?(field.type_id) }
COMPILER_VERSION = 5 COMPILER_VERSION = 5
belongs_to :theme belongs_to :theme
@ -124,7 +127,6 @@ COMPILED
if will_save_change_to_error? if will_save_change_to_error?
update_columns(error: self.error) update_columns(error: self.error)
end end
end end
end end

View File

@ -29,4 +29,19 @@ HTML
expect(field.error).to eq(nil) expect(field.error).to eq(nil)
end end
def create_upload_theme_field!(name)
ThemeField.create!(
theme_id: 1,
target_id: 0,
value: "",
type_id: ThemeField.types[:theme_upload_var],
name: name,
)
end
it "ensures we don't use invalid SCSS variable names" do
expect { create_upload_theme_field!("42") }.to raise_error(ActiveRecord::RecordInvalid)
expect { create_upload_theme_field!("a42") }.not_to raise_error
end
end end