Ruby on rails DRY strip whitespace from selective input forms - ruby-on-rails

I'm fairly new to rails so bear with me.
I want to strip whitespace from a selective group of input forms.
But I would like a DRY solution.
So I was thinking there might be a solution such as a helper method, or a custom callback. Or a combination such as before_validation strip_whitespace(:attribute, :attribute2, etc)
Any help is awesome! Thanks!
EDIT
I have this in my model file ...
include ApplicationHelper
strip_whitespace_from_attributes :employer_name
... and I have this in my ApplicationHelper ...
def strip_whitespace_from_attributes(*args)
args.each do |attribute|
attribute.gsub('\s*', '')
end
end
but now I'm getting the error message:
undefined method `strip_whitespace_from_attributes' for "the test":String
EDIT II -- SUCCESS
I added this StripWhitespace module file to the lib directory
module StripWhitespace
extend ActiveSupport::Concern
module ClassMethods
def strip_whitespace_from_attributes(*args)
args.each do |attribute|
define_method "#{attribute}=" do |value|
#debugger
value = value.gsub(/\s*/, "")
#debugger
super(value)
end
end
end
end
end
ActiveRecord::Base.send(:include, StripWhitespace)
and then added this to any model class this wants to strip whitespace ...
include StripWhitespace
strip_whitespace_from_attributes #add any attributes that need whitespace stripped

I would go with sth like this (not tested):
module Stripper # yeah!
extend ActiveSupport::Concern
module ClassMethods
def strip_attributes(*args)
mod = Module.new
args.each do |attribute|
define_method "#{attribute}=" do |value|
value = value.strip if value.respond_to? :strip
super(value)
end
end
end
include mod
end
end
end
class MyModel < ActiveRecord::Base
include Stripper
strip_attributes :foo, :bar
end
m = MyModel.new
m.foo = ' stripped '
m.foo #=> 'stripped'

If you can get your attributes in to a single array (perhaps there's a [:params] key you can use instead), you can do the following:
class FooController < ApplicationController
before_create strip_whitespace(params)
private
def strip_whitespace(*params)
params.map{ |attr| attr.strip }
end
end

Related

Ruby Module's included hook instead of regular extending?

There is a general way of adding class methods from Module via its included hook, and following extending base class with ClassMethods submodule. This way is described in book "Metaprogramming Ruby 2: Program Like the Ruby Pros". Here is an example from there:
module CheckedAttributes
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def attr_checked(attribute, &validation)
define_method "#{attribute}=" do |value|
raise 'Invalid attribute' unless validation.call(value)
instance_variable_set("##{attribute}", value)
end
define_method attribute do
instance_variable_get "##{attribute}"
end
end
end
end
class Person
include CheckedAttributes
attr_checked :age do |v|
v >= 18
end
end
But what is the reason of including the almost empty module first, and then extending its includer with one more module? Why not extend the class right the way with target module itself?
module CheckedAttributes
def attr_checked(attribute, &validation)
define_method "#{attribute}=" do |value|
raise 'Invalid attribute' unless validation.call(value)
instance_variable_set("##{attribute}", value)
end
define_method attribute do
instance_variable_get "##{attribute}"
end
end
end
class Person
extend CheckedAttributes
attr_checked :age do |v|
v >= 18
end
end
Is code above totally equal to initial example from this book? Or there are any pitfalls?
I have no idea where you took this code from, but this pattern involving ClassMethods is normally used in the cases when you want to alter both class and eigenclass to avoid the necessity to call both include Foo and extend Bar.
module Named
def self.included(base)
base.extend ClassMethods
end
def describe
"Person is: #{name}"
end
module ClassMethods
def name!
define_method "name=" do |value|
raise 'Invalid attribute' unless validation.call(value)
instance_variable_set("#name", value)
end
define_method "name" do
instance_variable_get "#name"
end
end
end
end
class Person
include Named
name!
end
p = Person.new
p.name = "Trump"
p.describe #⇒ "Person is: Trump"
In your example, it makes zero sense.

How would I implement my own Rails-style validates() method in Ruby?

I'm trying to understand some Ruby metaprogramming concepts.
I think I understand classes, objects, and metaclasses. Unfortunately, I'm very unclear on exactly what happens with included Modules with respect to their instance/'class' variables.
Here's a contrived question whose solution will answer my questions:
Suppose I'm writing my own crappy Rails "validates" method, but I want it to come from a mixed-in module, not a base class:
module MyMixin
# Somehow validates_wordiness_of() is defined/injected here.
def valid?
# Run through all of the fields enumerated in a class that uses
# "validate_wordiness_of" and make sure they .match(/\A\w+\z/)
end
end
class MyClass
include MyMixin
# Now I can call this method in my class definition and it will
# validate the word-ness of my string fields.
validate_wordiness_of :string_field1, :string_field2, :string_field3
# Insert rest of class here...
end
# This should work.
MyMixin.new.valid?
Ok, so how would you store that list of fields from the validate_wordiness_of invocation (in MyClass) in such a way that it can be used in the valid? method (from MyMixin)?
Or am I coming at this all wrong? Any info would be super appreciated!
So here are two alternative ways of doing it:
With "direct" access
module MyMixin
def self.included(base)
base.extend(ClassMethods)
end
def wordy?(value)
value.length > 2
end
module ClassMethods
def validates_wordiness_of(*attrs)
define_method(:valid?) do
attrs.all? do |attr|
wordy?(send(attr))
end
end
end
end
end
class MyClass
include MyMixin
validates_wordiness_of :foo, :bar
def foo
"a"
end
def bar
"asrtioenarst"
end
end
puts MyClass.new.valid?
The downside to this approach is that several consecutive calls to validates_wordiness_of will overwrite each other.
So you can't do this:
validates_wordiness_of :foo
validates_wordiness_of :bar
Saving validated attribute names in the class
You could also do this:
require 'set'
module MyMixin
def self.included(base)
base.extend(ClassMethods)
end
module Validation
def valid?
self.class.wordy_attributes.all? do |attr|
wordy?(self.send(attr))
end
end
def wordy?(value)
value.length > 2
end
end
module ClassMethods
def wordy_attributes
#wordy_attributes ||= Set.new
end
def validates_wordiness_of(*attrs)
include(Validation) unless validation_included?
wordy_attributes.merge(attrs)
end
def validation_included?
ancestors.include?(Validation)
end
end
end
class MyClass
include MyMixin
validates_wordiness_of :foo, :bar
def foo
"aastrarst"
end
def bar
"asrtioenarst"
end
end
MyClass.new.valid?
# => true
I chose to make the valid? method unavailable until you actually add a validation. This may be unwise. You could probably just have it return true if there are no validations.
This solution will quickly become unwieldy if you introduce other kinds of validations. In that case I would start wrapping validations in validator objects.

Ruby on Rails, including a module with arguments

Is there a way to use arguments when including a ruby module? I have a module Assetable which is included across many classes. I want to be able to generate attr_accessor's on the fly.
module Assetable
extend ActiveSupport::Concern
included do
(argument).times do |i|
attr_accessor "asset_#{i}".to_sym
attr_accessible "asset_#{i}".to_sym
end
end
end
There is a trick: making a class that's inheriting from a module so that you could pass any arguments to the module like class.
class Assetable < Module
def initialize(num)
#num = num
end
def included(base)
num = #num
base.class_eval do
num.times do |i|
attr_accessor "asset_#{i}"
end
end
end
end
class A
include Assetable.new(3)
end
a = A.new
a.asset_0 = 123
a.asset_0 # => 123
The details are blogged at http://kinopyo.com/en/blog/ruby-include-module-with-arguments, hope you'll find it useful.
There is no way of passing arguments when including the module. The best next thing would be to define a class method that lets you create what you need afterwards:
module Assetable
extend ActiveSupport::Concern
module ClassMethods
def total_assets(number)
number.times do |i|
attr_accessor "asset_#{i}"
attr_accessible "asset_#{i}"
end
end
end
end
class C
include Assetable
total_assets 3
end
o = C.new
o.asset_2 = "Some value."
o.asset_2 #=> "Some value."
Also be careful when overriding the included method within a concern because it's also used by ActiveSupport::Concern. You should call super within the overriden method in order to ensure proper initialization.
You can generate and include an anonymous module without polluting global namespaces:
module Assetable
def self.[](argument)
Module.new do
extend ActiveSupport::Concern
included do
(argument).times do |i|
attr_accessor :"asset_#{i}"
attr_accessible :"asset_#{i}"
end
end
end
end
end
class Foo
include Assetable[5]
end
You can't pass arguments to a module. In fact, you can't pass arguments to anything except a message send.
So, you have to use a message send:
module Kernel
private def Assetable(num)
#__assetable_cache__ ||= []
#__assetable_cache__[num] ||= Module.new do
num.times do |i|
attr_accessor :"asset_#{i}"
attr_accessible :"asset_#{i}"
end
end
end
end
class Foo
include Assetable 3
end
Note: I didn't see why you would need ActiveSupport::Concern here at all, but it's easy to add back in.

Mark ActiveRecord attribute as html_safe

We have an ActiveRecord model with an html attribute (say Post#body). Is there a nice way that calling body on a post returns an html_safe? string? E.g.:
class Post < ActiveRecord::Base
# is_html_escaped :body or somesuch magic
end
Post.first.body.html_safe? # => true
The problem otherwise is that we have to call raw everything we show that field.
Here's a way I found:
class Post < ActiveRecord::Base
def message
super.html_safe
end
def message=(new_mess)
new_mess = ERB::Util.html_escape(new_mess.sanitize) unless new_mess.html_safe?
super(new_mess)
end
end
FYI. I made a module for this
module SanitizeOnly
def self.included(mod)
mod.extend(ClassMethods)
end
module ClassMethods
def sanitize_on_input_only(*attribute_names)
attribute_names.map(&:to_s).each do | attribute_name |
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{attribute_name}
super.html_safe
end
def #{attribute_name}=(new_val)
new_val = ERB::Util.html_escape(new_val.sanitize) unless new_val.html_safe?
super(new_val)
end
RUBY
end
end
end
end
to use it just include it in your model and add the attributes you want to avoid using raw for to a sanitize_on_input_only line like the following:
sanitize_on_input_only :message, :another_attribute, ...

Ruby on Rails - Share one method with 2 models

I have the following module
module SharedMethods
# Class method
module ClassMethods
#
# Remove white space from end of strings
def remove_whitespace
self.attributes.each do |key,value|
if value.kind_of?(String) && !value.blank?
write_attribute key, value.strip
end
end
end
end
#
#
def self.included(base)
base.extend(ClassMethods)
end
end
and I am using it in my models like
include SharedMethods
before_validation :remove_whitespace
However whenever I submit the form I get a "undefined method `remove_whitespace'" message
What can I do to correct this error?
That's because :remove_whitespace must be an instance method, not a class method.
module SharedMethods
def self.included(base)
base.send :include, InstanceMethods
end
module InstanceMethods
# Remove white space from end of strings
def remove_whitespace
self.attributes.each do |key,value|
if value.kind_of(String) && !value.blank?
write_attribute key, value.strip
end
end
end
end
end
Unless you need the module to provide both class and instance methods, you can also skip the usage of self.included and simplify your module in this way:
module SharedMethods
# Remove white space from end of strings
def remove_whitespace
self.attributes.each do |key,value|
if value.kind_of(String) && !value.blank?
write_attribute key, value.strip
end
end
end
end

Resources