In my Rails 4 app, I have created a app/extra folder, where I store some custom class methods.
For instance, inside the above folder, I have a process folder, and inside this folder, I have a process_base.rb file and some other process_method1.rb, process_method2.rb, etc.
I then declare these methods in my Post model, as follows:
def process_post
@process_method1 = Process::ProcessMethod1.new
@process_method1.process self
@process_method2 = Process::ProcessMethod2.new
@process_method2.process self
end
And finally, in my Posts#show view, I use:
<%= @post.process_post %>
This is working pretty well, since I get what I need when the post page is loaded.
What I would like to do now, is to leverage these methods in the Posts#new view, and apply them in real-time to the @post being created, with JavaScript.
For instance, let's say process_method1 counts the number of characters in the @post.message, I would like to be able to count the characters as the user is typing his message, pretty much like what you can see on Twitter when you compose a new tweet.
I am not a very experienced Rails developer, and I understand this question may be broad: I am aware I can't expect a full answer with detailed code, but I would appreciate any pointer that would allow me to understand in which direction I should investigate further.
Maybe there is a name for what I am trying to achieve, and knowing what this name is would be more than enough to look for relevant resources online.
Or, maybe, it would make more sense to re-write all the logic directly in JavaScript, instead of trying to apply Ruby methods with JavaScript.
—————
UPDATE: the question here is close to what I want to achieve, except I would like to know if I can/should reuse the Ruby methods defined in my app/extra folder to avoid code and logic duplication.
—————
Any kind of help would be highly appreciated.