Sha256: f4a3300bb41d6fac7fd0fb96632a84bade49f44265433478b9c8b0ca890a0a86

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

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

		##
		#Declare traits for attributes
		#
		#+attributes+:: array of symbols of attributes
		#+traits+:: array of symbols of traits
		def declare_traits_for attributes, traits
			attributes.each do |k|
				traits.each { |t| sent(t, k) }
			end

		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

		def declare_has_many_nested attributes
			declare_taits_for attributes, [:has_and_belongs_to_many, :accepts_nested_attributes_for]
		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.7 lib/rails_model_stacker/rms.rb
rails_model_stacker-0.0.6 lib/rails_model_stacker/rms.rb