Class: Trenni::Formatters::HTML::RadioSelect

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

Overview

Table based select boxes using per-row checkboxes.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formatter, builder, **options) ⇒ RadioSelect

Returns a new instance of RadioSelect.



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

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

Class Method Details

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



30
31
32
33
34
# File 'lib/trenni/formatters/html/radio_select.rb', line 30

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

Instance Method Details

#call(&block) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/trenni/formatters/html/radio_select.rb', line 105

def call(&block)
	Builder.fragment(@builder) do |builder|
		builder.tag :table do
			builder.tag :tbody do
				if self.optional?
					builder << item(title: optional_title_for(**@options), value: nil)
				end
				
				builder.capture(self, &block)
			end
		end
	end
end

#item(**options, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/trenni/formatters/html/radio_select.rb', line 75

def item(**options, &block)
	Builder.fragment(block&.binding || @builder) do |builder|
		builder.tag :tr do
			builder.inline(:td, :class => :handle) do
				builder.tag :input, radio_attributes_for(**options)
			end
			
			builder.inline(:td, :class => :item) do
				if block_given?
					builder.capture(self, &block)
				else
					builder.text title_for(**options)
				end
			end
		end
	end
end

#name_for(**options) ⇒ Object



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

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

#optional?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/trenni/formatters/html/radio_select.rb', line 101

def optional?
	@options[:optional]
end

#optional_title_for(**options) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/trenni/formatters/html/radio_select.rb', line 93

def optional_title_for(**options)
	if options[:optional] == true
		options[:blank] || ''
	else
		options[:optional]
	end
end

#radio_attributes_for(**options) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/trenni/formatters/html/radio_select.rb', line 64

def radio_attributes_for(**options)
	return {
		:type => :radio,
		:name => @field,
		# We set a default value to empty string, otherwise it becomes "on".
		:value => value_for(**options) || "",
		:checked => options.fetch(:selected) {raw_value == raw_value_for(**options)},
		:data => options[:data],
	}
end

#raw_valueObject



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

def raw_value
	@raw_value ||= raw_value_for(**@options)
end

#raw_value_for(**options) ⇒ Object



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

def raw_value_for(**options)
	@formatter.raw_value_for(**options)
end

#title_for(**options) ⇒ Object



60
61
62
# File 'lib/trenni/formatters/html/radio_select.rb', line 60

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

#value_for(**options) ⇒ Object



56
57
58
# File 'lib/trenni/formatters/html/radio_select.rb', line 56

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