module RailsModelStacker

Adds Some helpers for declaring traits and valdation on multiple fields in ActiveRecord

Public Instance Methods

declare_belongs_to(attributes) click to toggle source

see declare_join_on

# File lib/rails_model_stacker/rms.rb, line 50
def declare_belongs_to attributes
        declare_trait_for attributes, :belongs_to
end
declare_has_many(attributes) click to toggle source

see declare_join_on

# File lib/rails_model_stacker/rms.rb, line 56
def declare_has_many attributes
        declare_trait_for attributes, :has_many
end
declare_has_many_nested(attributes) click to toggle source
# File lib/rails_model_stacker/rms.rb, line 66
def declare_has_many_nested attributes
        declare_taits_for attributes, [:has_and_belongs_to_many, :accepts_nested_attributes_for]
end
declare_join_on(attributes) click to toggle source

declares the trait has_and_belongs_to_many on all attributes

attributes

array of symbols of field names

# File lib/rails_model_stacker/rms.rb, line 44
def declare_join_on attributes
        declare_trait_for attributes, :has_and_belongs_to_many
end
declare_nested(attributes) click to toggle source

see declare_join_on

# File lib/rails_model_stacker/rms.rb, line 62
def declare_nested attributes
        declare_trait_for attributes, :accepts_nested_attributes_for
end
declare_trait_for(attributes, trait) click to toggle source

Declare a trait or validation on attributes

declare_trait [:name, :email], :validates_presence_of
attributes

array of symbol of field names

trait

symbol of the trait

# File lib/rails_model_stacker/rms.rb, line 14
def declare_trait_for(attributes, trait)
        attributes.each { |key| send(trait,key) }
end
declare_traits_for(attributes, traits) click to toggle source

Declare traits for attributes

attributes

array of symbols of attributes

traits

array of symbols of traits

# File lib/rails_model_stacker/rms.rb, line 23
def declare_traits_for attributes, traits
        attributes.each do |k|
                traits.each { |t| sent(t, k) }
        end

end
require_presence_of(attributes) click to toggle source

Ensures attributes will be present

attributes

array of symbols of field names

# File lib/rails_model_stacker/rms.rb, line 33
def require_presence_of attributes
        declare_trait_for attributes, :validates_presence_of
end
require_uniqness_of(attributes) click to toggle source
# File lib/rails_model_stacker/rms.rb, line 37
def require_uniqness_of attributes
        delcare_trait_for attributes, :validates_uniqueness_of
end