From 5b11bbec688917a31203df7aa9cc945395bff245 Mon Sep 17 00:00:00 2001 From: Kane York Date: Tue, 14 Jul 2015 16:49:23 -0700 Subject: [PATCH] Finish group/category validation, add user lists --- .../discourse/components/param-input.js.es6 | 52 ++++++++++++++++--- .../admin/components/q-params/string.hbs | 2 +- .../admin/components/q-params/user_list.hbs | 2 + assets/stylesheets/explorer.scss | 4 ++ plugin.rb | 33 ++++++++---- 5 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 assets/javascripts/discourse/templates/admin/components/q-params/user_list.hbs diff --git a/assets/javascripts/discourse/components/param-input.js.es6 b/assets/javascripts/discourse/components/param-input.js.es6 index 4a86a1e..f06896f 100644 --- a/assets/javascripts/discourse/components/param-input.js.es6 +++ b/assets/javascripts/discourse/components/param-input.js.es6 @@ -1,3 +1,6 @@ +// import Category from 'discourse/models/category'; +const Category = Discourse.Category; + const layoutMap = { int: 'int', bigint: 'int', @@ -7,15 +10,15 @@ const layoutMap = { date: 'generic', datetime: 'generic', double: 'string', - inet: 'generic', user_id: 'user_id', post_id: 'string', - topic_id: 'int', - category_id: 'int', - group_id: 'int', - badge_id: 'int', + topic_id: 'generic', + category_id: 'generic', + group_id: 'generic', + badge_id: 'generic', int_list: 'generic', - string_list: 'generic' + string_list: 'generic', + user_list: 'user_list' }; function allowsInputTypeTime() { @@ -56,6 +59,7 @@ export default Ember.Component.extend({ const intVal = parseInt(value, 10); const intValid = !isNaN(intVal) && intVal < 2147483648 && intVal > -2147483649; + const isPositiveInt = /^\d+$/.test(value); switch (type) { case 'int': return /^-?\d+$/.test(value) && intValid; @@ -70,7 +74,39 @@ export default Ember.Component.extend({ return /^(-?\d+|null)$/.test(i.trim()); }); case 'post_id': - return /^\d+$/.test(value) || /\d+\/\d+(\?u=.*)?$/.test(value); + return isPositiveInt || /\d+\/\d+(\?u=.*)?$/.test(value); + case 'category_id': + const cats = Category.list(); + if (!isPositiveInt && value !== value.dasherize()) { + this.set('value', value.dasherize()); + } + + if (isPositiveInt) { + return !!cats.find(function(c) { + return c.get('id') === intVal; + }); + } else if (/\//.test(value)) { + const match = /(.*)\/(.*)/.exec(value); + if (!match) return false; + const result = Category.findBySlug(match[2].dasherize(), match[1].dasherize()); + return !!result; + } else { + return !!Category.findBySlug(value.dasherize()); + } + case 'group_id': + const groups = this.site.get('groups'); + if (isPositiveInt) { + return !!groups.find(function(g) { + return g.id === intVal; + }); + } else { + if (value !== value.underscore()) { + this.set('value', value.underscore()); + } + return !!groups.find(function(g) { + return g.name === value.underscore(); + }); + } } return true; }.property('value', 'info.type', 'info.nullable'), @@ -83,7 +119,7 @@ export default Ember.Component.extend({ if (layoutMap[type]) { return layoutMap[type]; } - return type; + return 'generic'; }.property('info.type'), layoutName: function() { diff --git a/assets/javascripts/discourse/templates/admin/components/q-params/string.hbs b/assets/javascripts/discourse/templates/admin/components/q-params/string.hbs index 2b0fd48..f1c5fdc 100644 --- a/assets/javascripts/discourse/templates/admin/components/q-params/string.hbs +++ b/assets/javascripts/discourse/templates/admin/components/q-params/string.hbs @@ -1,2 +1,2 @@ -{{text-field value=value}} +{{text-field value=value type="text"}} {{info.identifier}} diff --git a/assets/javascripts/discourse/templates/admin/components/q-params/user_list.hbs b/assets/javascripts/discourse/templates/admin/components/q-params/user_list.hbs new file mode 100644 index 0000000..b12da94 --- /dev/null +++ b/assets/javascripts/discourse/templates/admin/components/q-params/user_list.hbs @@ -0,0 +1,2 @@ +{{user-selector usernames=value}} +{{info.identifier}} diff --git a/assets/stylesheets/explorer.scss b/assets/stylesheets/explorer.scss index 761d107..0af683e 100644 --- a/assets/stylesheets/explorer.scss +++ b/assets/stylesheets/explorer.scss @@ -166,11 +166,15 @@ background-color: mix($danger, $secondary, 20%); } .param { + width: 300px; display: inline-block; overflow-x: visible; .ac-wrap { display: inline-block; } + input { + width: 190px; + } } .param-name { display: inline-block; diff --git a/plugin.rb b/plugin.rb index 2518d52..3e9c36c 100644 --- a/plugin.rb +++ b/plugin.rb @@ -389,11 +389,11 @@ SQL def self.types @types ||= Enum.new( # Normal types - :int, :bigint, :boolean, :string, :date, :time, :datetime, :double, :inet, + :int, :bigint, :boolean, :string, :date, :time, :datetime, :double, # Selection help :user_id, :post_id, :topic_id, :category_id, :group_id, :badge_id, # Arrays - :int_list, :string_list + :int_list, :string_list, :user_list ) end @@ -402,7 +402,6 @@ SQL integer: :int, text: :string, timestamp: :datetime, - ipaddr: :inet, } end @@ -458,15 +457,22 @@ SQL rescue ArgumentError => e invalid_format string, e.message end - when :ipaddr - begin - value = IPAddr.new string - rescue ArgumentError => e - invalid_format string, e.message - end when :double value = string.to_f - when :user_id, :post_id, :topic_id, :category_id, :group_id, :badge_id + when :category_id + if string =~ /(.*)\/(.*)/ + parent_name = $1 + child_name = $2 + parent = Category.query_parent_category(parent_name) + invalid_format string, "Could not find category named #{parent_name}" unless parent + object = Category.query_category(child_name, parent) + invalid_format string, "Could not find subcategory of #{parent_name} named #{child_name}" unless object + else + object = Category.where(id: string.to_i).first || Category.where(slug: string).first || Category.where(name: string).first + invalid_format string, "Could not find category named #{string}" unless object + end + value = object.id + when :user_id, :post_id, :topic_id, :group_id, :badge_id if string.gsub(/[ _]/, '') =~ /^-?\d+$/ clazz_name = (/^(.*)_id$/.match(type.to_s)[1].classify.to_sym) begin @@ -497,6 +503,10 @@ SQL invalid_format string, "The topic with id #{$1} was not found" end end + elsif type == :group_id + object = Group.where(name: string).first + invalid_format string, "The group named #{string} was not found" unless object + value = object.id else invalid_format string end @@ -506,6 +516,9 @@ SQL when :string_list value = string.split(',').map {|s| s.downcase == '#null' ? nil : s } invalid_format string, "can't be empty" if value.length == 0 + when :user_list + value = string.split(',').map {|s| User.find_by_username_or_email(s) } + invalid_format string, "can't be empty" if value.length == 0 else raise TypeError.new('unknown parameter type??? should not get here') end