Module: AlchemizedBy

Defined in:
lib/alchemy/alchemized_by.rb

Instance Method Summary collapse

Instance Method Details

#alchemized_by(association_id, opts = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/alchemy/alchemized_by.rb', line 3

def alchemized_by(association_id, opts={})
	
	opts[:with] ||= "#{association_id}_id"
	opts[:with] = opts[:with].to_a
	opts[:on] ||= :id
	
	opts[:with].each do |method|

		alchemy_namespace = "#{self.name.underscore.pluralize.downcase}_#{method}"
		alchemy_listname = "#{method}_list_name"

		define_method(alchemy_listname) do
			"#{association_id.to_s.camelize}|#{send(method)}|#{alchemy_namespace}"
		end

		define_method("alchemize_#{method}") do
			ALCHEMY.set(send(alchemy_listname), self.send(opts[:on]))
		end

	end
end