Finish group/category validation, add user lists

This commit is contained in:
Kane York 2015-07-14 16:49:23 -07:00
parent 0672e99da2
commit 5b11bbec68
5 changed files with 74 additions and 19 deletions

View File

@ -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() {

View File

@ -1,2 +1,2 @@
{{text-field value=value}}
{{text-field value=value type="text"}}
<span class="param-name">{{info.identifier}}</span>

View File

@ -0,0 +1,2 @@
{{user-selector usernames=value}}
<span class="param-name">{{info.identifier}}</span>

View File

@ -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;

View File

@ -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