Sha256: e153ac4af65ad0823dce67bd08c3e3824c6202b72b1d5c6d1b4d52965a86683b

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require 'active_support/concern'
#Adds Some helpers for declaring traits and valdation on multiple fields in ActiveRecord
module RailsModelStacker
	extend ActiveSupport::Concern

	class_methods do
		##
		#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
		def declare_trait_for(attributes, trait)
			attributes.each { |key| send(trait,key) }
		end

		##
		#Ensures attributes will be present
		#+attributes+:: array of symbols of field names
		def require_presence_of attributes
			declare_trait_for attributes, :validates_presence_of
		end

		def require_uniqness_of attributes
			delcare_trait_for attributes, :validates_uniqueness_of
		end

		##
		#declares the trait has_and_belongs_to_many on all attributes
		#+attributes+:: array of symbols of field names
		def declare_join_on attributes
			declare_trait_for attributes, :has_and_belongs_to_many
		end

		##
		#see declare_join_on
		def declare_belongs_to attributes
			declare_trait_for attributes, :belongs_to
		end

		##
		#see declare_join_on
		def declare_has_many attributes
			declare_trait_for attributes, :has_many
		end

	end
end

ActiveRecord::Base.send(:include, RailsModelStacker)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_model_stacker-0.0.4 lib/rails_model_stacker/rms.rb
rails_model_stacker-0.0.3 lib/rails_model_stacker/rms.rb