mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -05:00
allow skipping the validations on creation if its an api call AND skip_validations is specified
this allows wordpress plugin to post very very short titles or titles that would otherwise be disallowed
This commit is contained in:
parent
68d98ec94e
commit
f6b850e7a4
5 changed files with 31 additions and 15 deletions
|
@ -189,18 +189,27 @@ class PostsController < ApplicationController
|
||||||
private
|
private
|
||||||
|
|
||||||
def create_params
|
def create_params
|
||||||
params.require(:raw)
|
permitted = [
|
||||||
params.permit(
|
:raw,
|
||||||
:raw,
|
:topic_id,
|
||||||
:topic_id,
|
:title,
|
||||||
:title,
|
:archetype,
|
||||||
:archetype,
|
:category,
|
||||||
:category,
|
:target_usernames,
|
||||||
:target_usernames,
|
:reply_to_post_number,
|
||||||
:reply_to_post_number,
|
:image_sizes,
|
||||||
:image_sizes,
|
|
||||||
:auto_close_days
|
:auto_close_days
|
||||||
).tap do |whitelisted|
|
]
|
||||||
|
|
||||||
|
if api_key_valid?
|
||||||
|
# php seems to be sending this incorrectly, don't fight with it
|
||||||
|
params[:skip_validations] = params[:skip_validations].to_s == "true"
|
||||||
|
permitted << :skip_validations
|
||||||
|
end
|
||||||
|
|
||||||
|
params.require(:raw)
|
||||||
|
params.permit(*permitted).tap do |whitelisted|
|
||||||
|
# TODO this does not feel right, we should name what meta_data is allowed
|
||||||
whitelisted[:meta_data] = params[:meta_data]
|
whitelisted[:meta_data] = params[:meta_data]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -35,8 +35,6 @@ class Topic < ActiveRecord::Base
|
||||||
rate_limit :limit_topics_per_day
|
rate_limit :limit_topics_per_day
|
||||||
rate_limit :limit_private_messages_per_day
|
rate_limit :limit_private_messages_per_day
|
||||||
|
|
||||||
before_validation :sanitize_title
|
|
||||||
|
|
||||||
validates :title, :presence => true,
|
validates :title, :presence => true,
|
||||||
:topic_title_length => true,
|
:topic_title_length => true,
|
||||||
:quality_title => { :unless => :private_message? },
|
:quality_title => { :unless => :private_message? },
|
||||||
|
@ -47,6 +45,7 @@ class Topic < ActiveRecord::Base
|
||||||
:collection => Proc.new{ Topic.listable_topics } }
|
:collection => Proc.new{ Topic.listable_topics } }
|
||||||
|
|
||||||
before_validation do
|
before_validation do
|
||||||
|
self.sanitize_title
|
||||||
self.title = TextCleaner.clean_title(TextSentinel.title_sentinel(title).text) if errors[:title].empty?
|
self.title = TextCleaner.clean_title(TextSentinel.title_sentinel(title).text) if errors[:title].empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -208,7 +208,7 @@ class PostCreator
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_post
|
def save_post
|
||||||
unless @post.save
|
unless @post.save(validate: !@opts[:skip_validations])
|
||||||
@errors = @post.errors
|
@errors = @post.errors
|
||||||
raise ActiveRecord::Rollback.new
|
raise ActiveRecord::Rollback.new
|
||||||
end
|
end
|
||||||
|
|
|
@ -56,7 +56,7 @@ class TopicCreator
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_topic
|
def save_topic
|
||||||
unless @topic.save
|
unless @topic.save(validate: !@opts[:skip_validations])
|
||||||
@errors = @topic.errors
|
@errors = @topic.errors
|
||||||
raise ActiveRecord::Rollback.new
|
raise ActiveRecord::Rollback.new
|
||||||
end
|
end
|
||||||
|
|
|
@ -341,5 +341,13 @@ describe PostCreator do
|
||||||
post.created_at.should be_within(10.seconds).of(created_at)
|
post.created_at.should be_within(10.seconds).of(created_at)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'disable validations' do
|
||||||
|
it 'can save a post' do
|
||||||
|
creator = PostCreator.new(user, raw: 'q', title: 'q', skip_validations: true)
|
||||||
|
post = creator.create
|
||||||
|
creator.errors.should be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue