Sha256: 3dc8175817eabe50cd542c025b72782bf8456f90241c696038e819505aa89f4b

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 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

		##
		#see declare_join_on
		def declare_nested attributes
			declare_trait_for attributes, :accepts_nested_attributes_for
		end

	end
end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_model_stacker-0.0.5 lib/rails_model_stacker/rms.rb