Class: SimpleDataPresentation::SimpleDataList::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_data_presentation/simple_data_list/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(context, resource, options = {}, &content_block) ⇒ Base

Returns a new instance of Base.



2
3
4
5
6
7
8
9
10
# File 'lib/simple_data_presentation/simple_data_list/base.rb', line 2

def initialize(context, resource, options = {}, &content_block)
	@context = context
	@resource = resource
	@options = options
	@content_block = content_block
	@resource_class = @options.delete(:resource_class) || @resource.class
	@html_options = SimpleDataPresentation::HtmlOptions.new(@options.delete(:html) || {})
	@html_options[:class] = "dl-horizontal"
end

Instance Method Details

#item(*args, &block) ⇒ Object



12
13
14
# File 'lib/simple_data_presentation/simple_data_list/base.rb', line 12

def item(*args, &block)
	self.title(*args.clone) + self.value(*args.clone, &block)
end

#render!Object



54
55
56
# File 'lib/simple_data_presentation/simple_data_list/base.rb', line 54

def render!
	@context.raw @context. :dl, @context.capture(self, &@content_block), @html_options
end

#title(*args, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/simple_data_presentation/simple_data_list/base.rb', line 16

def title(*args, &block)
	options = args.extract_options!
	content = if block.present?
		@context.capture(@resource_class, &block)
	else
		if options[:label].is_a?(Symbol)
			@resource_class.human_attribute_name options[:label]
		elsif !options[:label].nil?
			options[:label]
		elsif args.any?
			@resource_class.human_attribute_name args.join("_").to_sym
		end
	end
	@context. :dt, content, SimpleDataPresentation::HtmlOptions.new(options[:html] || {}).merge(options[:label_html] || {})
end

#value(*args, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/simple_data_presentation/simple_data_list/base.rb', line 32

def value(*args, &block)
	options = args.extract_options!
	if block.present?
		content = @context.capture @resource, &block
	else
		if (value_option = options[:value]).present?
			content = value_option.respond_to?(:call) ? value_option.call(@resource) : value_option
		else
			content = value_for_method_chain *args
		end
		as_formatter = options[:as]
		if as_formatter.present?
			as_formatter = @context.method(as_formatter).to_proc if as_formatter.is_a?(Symbol)
			content = case as_formatter.arity
			when 1; as_formatter.call content
			when 2; as_formatter.call @resource, content
			end
		end
	end
	@context. :dd, content, SimpleDataPresentation::HtmlOptions.new(options[:html] || {}).merge(options[:value_html] || {})
end