Class: Trenni::Formatters::HTML::OptionSelect

Inherits:
Object
  • Object
show all
Defined in:
lib/trenni/formatters/html/option_select.rb

Overview

Standard drop-down select box:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formatter, options, builder) ⇒ OptionSelect

Returns a new instance of OptionSelect.



34
35
36
37
38
39
40
41
42
# File 'lib/trenni/formatters/html/option_select.rb', line 34

def initialize(formatter, options, builder)
	@formatter = formatter
	@object = formatter.object
	@field = options[:field]
	
	@options = options
	
	@builder = builder
end

Class Method Details

.call(formatter, options, builder, &block) ⇒ Object



28
29
30
31
32
# File 'lib/trenni/formatters/html/option_select.rb', line 28

def self.call(formatter, options, builder, &block)
	instance = self.new(formatter, options, builder)
	
	instance.call(options, &block)
end

Instance Method Details

#call(options = {}, &block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/trenni/formatters/html/option_select.rb', line 95

def call(options = {}, &block)
	Builder.fragment(@builder) do |builder|
		builder.tag :select, select_attributes_for(options) do
			if options[:optional]
				item(:title => '', :value => '', :builder => builder)
			end
			
			builder.append Trenni::Template.capture(self, &block)
		end
	end
end

#group(options = {}, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/trenni/formatters/html/option_select.rb', line 77

def group(options = {}, &block)
	Builder.fragment do |builder|
		builder.tag :optgroup, group_attributes_for(options) do
			if options[:optional]
				item(:title => '', :value => '', :builder => builder)
			end
			
			builder.append Trenni::Template.capture(&block)
		end
	end
end

#group_attributes_for(options) ⇒ Object



71
72
73
74
75
# File 'lib/trenni/formatters/html/option_select.rb', line 71

def group_attributes_for(options)
	return {
		:label => title_for(options)
	}
end

#item(options = {}) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/trenni/formatters/html/option_select.rb', line 63

def item(options = {})
	options[:field] ||= 'id'
	
	Builder.fragment(options[:builder]) do |builder|
		builder.inline(:option, option_attributes_for(options)) { builder.text title_for(options) }
	end
end

#name_for(options) ⇒ Object



44
45
46
# File 'lib/trenni/formatters/html/option_select.rb', line 44

def name_for(options)
	@formatter.name_for(options)
end

#option_attributes_for(options) ⇒ Object



56
57
58
59
60
61
# File 'lib/trenni/formatters/html/option_select.rb', line 56

def option_attributes_for(options)
	return {
		:value => value_for(options),
		:selected => options.fetch(:selected){ value_for(@options) == value_for(options) },
	}
end

#select_attributes_for(options) ⇒ Object



89
90
91
92
93
# File 'lib/trenni/formatters/html/option_select.rb', line 89

def select_attributes_for(options)
	return {
		:name => name_for(options)
	}
end

#title_for(options) ⇒ Object



52
53
54
# File 'lib/trenni/formatters/html/option_select.rb', line 52

def title_for(options)
	@formatter.title_for(options)
end

#value_for(options) ⇒ Object



48
49
50
# File 'lib/trenni/formatters/html/option_select.rb', line 48

def value_for(options)
	@formatter.value_for(options)
end