Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/common_lib/active_record/base.rb
Class Method Summary collapse
- .acts_like_a_hash(*args) ⇒ Object
-
.alias_attribute_with_memory(new_name, old_name) ⇒ Object
cattr_accessor here would create a class variable for ActiveRecord::Base What I want is the subclass to have one so just wait until its used the first time and create the class variable them cattr_accessor :aliased_attributes.
-
.aliased_attributes ⇒ Object
for those classes that don’t use the feature, just add the method.
- .nilify_if_blank(*args) ⇒ Object
-
.random ⇒ Object
def acts_like_a_hash(*args).
- .validates_uniqueness_of_with_nilification(*args) ⇒ Object
-
.validations_from_yaml_file ⇒ Object
I have found that the validations are really cluttering up the models.
Class Method Details
.acts_like_a_hash(*args) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/common_lib/active_record/base.rb', line 91 def self.acts_like_a_hash(*args) = { :key => :key, :value => :description }.update(args.) cattr_accessor :acts_like_a_hash_options cattr_accessor :acts_like_a_hash_memory class_eval do self. = self.acts_like_a_hash_memory = {} validates_presence_of [:key], [:value] validates_uniqueness_of [:key], [:value] validates_length_of [:key], [:value], :maximum => 250, :allow_blank => true # Treats the class a bit like a Hash and # searches for a record with a matching key. def self.[](key) self.acts_like_a_hash_memory[key.to_s.downcase.to_s] ||= where(self.arel_table[self.[:key]].matches(key)).first end end # class_eval do end |
.alias_attribute_with_memory(new_name, old_name) ⇒ Object
cattr_accessor here would create a class variable for ActiveRecord::Base What I want is the subclass to have one so just wait until its used the first time and create the class variable them cattr_accessor :aliased_attributes
139 140 141 142 143 144 145 146 |
# File 'lib/common_lib/active_record/base.rb', line 139 def self.alias_attribute_with_memory(new_name, old_name) unless self.class_variable_defined? '@@aliased_attributes' cattr_accessor :aliased_attributes self.aliased_attributes = {}.with_indifferent_access end self.aliased_attributes[new_name] = old_name alias_attribute_without_memory(new_name, old_name) end |
.aliased_attributes ⇒ Object
for those classes that don’t use the feature, just add the method.
131 132 133 |
# File 'lib/common_lib/active_record/base.rb', line 131 def self.aliased_attributes {} end |
.nilify_if_blank(*args) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/common_lib/active_record/base.rb', line 80 def self.nilify_if_blank(*args) # ONLY IF THE FIELD IS A STRING! class_eval do args.each do |arg| before_save { self.send("#{arg}=", nil) if self.send(arg).blank? } end end end |
.random ⇒ Object
def acts_like_a_hash(*args)
120 121 122 123 124 125 126 127 |
# File 'lib/common_lib/active_record/base.rb', line 120 def self.random count = count() if count > 0 offset(rand(count)).limit(1).first else nil end end |
.validates_uniqueness_of_with_nilification(*args) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/common_lib/active_record/base.rb', line 60 def self.validates_uniqueness_of_with_nilification(*args) # NOTE ANY field that has a unique index in the database NEEDS # to NOT be blank. Multiple nils are acceptable in index, # but multiple blanks are NOT. Nilify ALL fields with # unique indexes in the database. At least those that # would appear on a form, as an empty text box is sent # as '' and not nil, hence the initial problem. # The first one will work, but will fail after. # ONLY IF THE FIELD IS A STRING! class_eval do validates_uniqueness_of args, :allow_blank => true args.each do |arg| before_validation { self.send("#{arg}=", nil) if self.send(arg).blank? } end end end |
.validations_from_yaml_file ⇒ Object
I have found that the validations are really cluttering up the models. Moving them into a yml file and pulling them in like so seems to be working well.
Not all validations are going to work, but so far so good.
codereview.stackexchange.com/questions/159/use-of-a-regex-stored-inside-yaml-file email_regex: !ruby/regexp /^([w.%+-]+)@(\.)
([w]2,)$/i stackoverflow.com/questions/3337020/how-to-specify-ranges-in-yaml :in: !ruby/range 5..250 www.opensource.apple.com/source/ruby/ruby-14/ruby/lib/yaml/rubytypes.rb yaml4r.sourceforge.net/doc/page/objects_in_yaml.htm
/opt/local/lib/ruby1.9/1.9.1/syck/rubytypes.rb /opt/local/lib/ruby1.9/1.9.1/psych/visitors/to_ruby.rb
I really like this and I may very well make it a default at some point.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/common_lib/active_record/base.rb', line 43 def self.validations_from_yaml_file # warn "validations_from_yaml_file is now a default from common_lib. You can remove the call from your model." validation_file = File.join(Rails.root,"config/validations/#{self.to_s.underscore}.yml") if File.exists?(validation_file) h = YAML::load( ERB.new( IO.read( validation_file )).result) # if the yml file is empty, h is false ( added "if h" condition ) h.each do |validation| attributes=[validation.delete(:attributes), validation.delete(:attribute) ].compact.flatten self.validates *attributes, validation end if h # else # puts "YAML validations file not found so not using." end end |