Module: Instructions::FormHelper

Includes:
AttributeState, Formatters
Defined in:
lib/instructions/form_helper.rb

Instance Method Summary collapse

Methods included from Formatters

#merge

Methods included from AttributeState

#blank_attribute?, #blank_valid_attribute?, #determine, #initial_get_of_new_form?, #valid_attribute?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



43
44
45
# File 'lib/instructions/form_helper.rb', line 43

def method_missing(method_name, *args) 
	"<div class=\"method_missing\" style=\"font-weight: bold; color: red;\">No helper found to respond to \"#{method_name}\"</div>"
end

Instance Method Details

#active_model_message(method, message, generator = nil) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/instructions/form_helper.rb', line 29

def active_model_message(method, message, generator=nil)
	generator ||= generator(@object)
	if message.is_a? Hash
		return generator.generate_message(method, message.keys[0], :count => message.values[0])
	else
		return generator.generate_message(method, message)
	end
end

#active_model_messages(method, message) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/instructions/form_helper.rb', line 20

def active_model_messages(method, message)
	messages = []
	generator = generator(@object)
	message.each do |m|
		messages << active_model_message(method, m, generator)
	end
	Configuration.message_joiner.call messages
end

#generator(object) ⇒ Object



38
39
40
# File 'lib/instructions/form_helper.rb', line 38

def generator(object)
	ActiveModel::Errors.new object
end

#instructions(method, message = nil, options = {}) ⇒ Object



9
10
11
12
# File 'lib/instructions/form_helper.rb', line 9

def instructions(method, message=nil, options={})
	text = message_text method, message
	renderer(method, text, options).render
end

#message_text(method, message) ⇒ Object



14
15
16
17
18
# File 'lib/instructions/form_helper.rb', line 14

def message_text(method, message)
	return message if message.is_a? String
	return active_model_message(method, message) if [Symbol, Hash].include? message.class
	return active_model_messages(method, message) if message.is_a? Array
end

#renderer(method, text, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/instructions/form_helper.rb', line 48

def renderer(method, text, options={})
	attribute_state = determine method
	formatters = merge attribute_state, options
	
	if attribute_state == :new
		return InstructionsTag.new method, @object, @object_name, text, formatters
	end
	
	if attribute_state == :valid
		return ValidInstructionsTag.new method, @object, @object_name, text, formatters
	end
	
	if attribute_state == :invalid
		return ErrorMessageTag.build method, @object, @object_name, formatters
	end
end

#testObject



42
43
44
45
46
# File 'lib/instructions/form_helper.rb', line 42

def test
	def method_missing(method_name, *args) 
		"<div class=\"method_missing\" style=\"font-weight: bold; color: red;\">No helper found to respond to \"#{method_name}\"</div>"
	end
end