How can I override SiteSettings.min_post_length from a plugin for special topics?

(Joe Seyfried) #1

Hi all,

I’m currently trying to figure out a way how to override the Discourse.SiteSettings.min_post_length from a plugin. I tried the following stuff to no avail:

  1. Set minimumPostLengthin the Composer model to 1. Seems to affect the display of “x characters to go” in the composer, but nothing else.
  2. Override missingReplyCharacters: function()and minimumPostLength: function() to return 0 or 1. Same effect as above. All looks fine, but if I click submit, I end up with this modal dialog:

    …and I can’t figure out where this is triggered. I tried to debug into the save method, but the modal dialog comes from some dark place beyond…

Any clues for the clueless? TIA!

(Jens Maier) #2

There is a validation on the Post model that checks the minimum post length. This happens in Ruby via ActiveRecord when the post is saved and, if it is too short, raises an exception which is caught, sent to the useragent and results in that message you’re seeing.

The code responsible for this lives in lib/validators/post_validator.rb.

(Kane York) #3

To be more clear: The validation is happening on the server. No need to dig through the JS code.

(Joe Seyfried) #4

thanks. It dawned upon me that I’m barking up the wrong tree some minutes after posting, but couldn’t put my finger on it.
I have now tried to do what you wrote some time ago, Kane… just redefine a class in the plugin.rb - but I cannot seem to get a grip on that, either - is that concept still valid? What I’m trying is the following:

register_asset "javascripts/stuff"

require 'validators/post_validator.rb'

class Validators::PostValidator

  def validate(record)
    old = SiteSetting.min_post_length
    SiteSetting.min_post_length = 1
    SiteSetting.min_post_length = old
    Rails.logger.warn "****************************************************** Test" # never shows up anywhere

  def stripped_length(post)

…but anything I define in my plugin seems to get ignored. I guess I’m doing some stupid rookie mistakes here…

Show "topic-excerpt" for all post on Latest Page
(Kane York) #5

Try sticking that in an after_initialize do block.

(Jens Maier) #6

Content in your plugin.rb file is instance_eval'd in an instance of Plugin::Instance. I’m not absolutely certain what Ruby does when you define a class in an instance of something that isn’t itself a Class instance, but you have two options to fix this:

  1. class ::Validators::PostValidator. The :: forces your class name into the root module where it will re-open the existing class.
  2. move the class declaration into a separate file and require it.

Dangit, five seconds too late. :grin:

(Joe Seyfried) #7

Kind of unfair - Kane was a bit shorter. Combining your two remarks, it works great now - from plugin.rb. When I’m done with fiddling around, I want to clean it up into a separate file. Thanks for now - I’ll come back later with another stupid question. :wink: