I have a regex that I want to use for word count and validation. However, I don't want to have to go into every model and controller if I decide to change the regex I'm using. Is there a way to create a helper method that defines the regex?
This is what I've tried and it doesn't work:
module ApplicationHelper
def word_count(content_text)
content_text.scan(scan_regex).size
end
def scan_regex
return "/\b(([a-zA-Z’'-]))+((?![^<>]*>)+(?![^&;]*;))\b/"
end
end
You could remove the '/' from the regexp and then add them to scan:
module ApplicationHelper
def word_count(content_text)
content_text.scan(/#{scan_regex}/).size
end
def scan_regex
return "\b(([a-zA-Z’'-]))+((?![^<>]*>)+(?![^&;]*;))\b"
end
end
Related
I tried TO Google Can I redirect_to in rails modules but couldn't come up with anything. Basically, I have a method that I am going to use across a couple of Controllers.
lib/route_module.rb
module RouteModule
def self.user_has_active_chocolate(c_id, u_id)
chocolate_id = c_id
user_id = u_id
unless UserChocolate.where(id: chocolate_id).empty?
if UserChocolate.where(id: chocolate_id).last.active?
true
else
false
# BREAKS OVER HERE...
redirect_to "/user/new-chocolate/#{user_id}"
end
else
false
redirect_to "/admin"
end
end
end
app/controllers/user_controllers.rb
include RouteModule
before_filter :thingz, only: [:display_user_chocolate]
# private
def thingz
RouteModule.user_has_active_chocolate(params["chocolate_id"], params["user_id"])
end
But... whenever I run this... It will break as soon as it hit's redirect_to.
undefined method `redirect_to' for RouteModule:Module
My other option is use ActiveSupport::Concerns but I just trouble converting this Module into a Concern.
When you include a module, it acts as a mixin. That said, you include and get all the methods of the module in the context of your class. The proper way would be:
module RouteModule
def user_has_active_chocolate(c_id, u_id) # NO self
...
end
end
And in the class:
include RouteModule
def thingz
# NO module method call (NO RouteModule)
user_has_active_chocolate(params["chocolate_id"], params["user_id"])
end
I have two Helpers, ExamsHelper and ResultsHelper
exams_helper.rb
module ExamsHelper
def get_data
...
end
end
results_helper.rb
module ResultsHelper
def find_result
...
end
end
Is it possible to access the get_data method in ResultsHelper.
I know that if I am declaring it on the ApplicationHelper, I can access it. Is there any other solution for it?
You can always use include:
module ResultsHelper
include ExamsHelper
def find_result
get_data # works
end
end
I am working on adding some methods to a controller dynamically.
def self.add_command(method)
define_method(method) do
# Do something
end
end
add_command :method_name
I would like to add an around filter for these methods and only these, but do it dynamically. When I simply try to add an around_filter call in the add_command method it doesn't get triggered.
How is it possible to dynamically add an around filter?
def self.add_command(method)
define_method(method) do
# Do something
end
class_eval do
around_filter method.to_sym
end
end
In a controller there is a code
def action1
generic_call __method__
end
def action2
generic_call __method__
end
#......
def action_n
generic_call __method__
end
private
def generic_call method_name
#..........
end
To get rid off repeating, would it be better to generate actions dynamically? Would it be more costly compared to static definition? How can I do that?
The major cost is actually having to repeat all the code yourself, remember that the philosophy on rails is DRY (Don't repeat yourself).
If there is an overhead at all by defining the methods with metaprogramming, you won't notice it at all, you can however run some benchmarks yourself just to be sure, but even the rails source code is full of metaprogramming and dynamic methods all over the place, in special with ActiveRecord.
class MyController < ActionController::Base
[:action1, :action2, :action3].each do |method_name|
send :define_method, method_name do
generic_call __method__
end
end
end
I want to convert the title of a page to a friendly URL and store it in the database as a permalink. My problem is I can't use the parameterize method. It's not working. Other inflections are working like upcase or downcase but parameterize is not working. Is there a special case for parameterize?
This is my code:
Controller:
def create
params[:page][:permalink] = params[:page][:title].dup
#page = Page.new(params[:page])
end
Model:
class Page < ActiveRecord::Base
before_save :makeitpermalink
before_update :makeitpermalink
private
def makeitpermalink
permalink.parameterize!
end
end
According to the Rails' documentation, there is no bang (exclamation mark) version of the parameterize method, so try removing it:
def make_it_permalink
self.permalink = self.permalink.parameterize
end