Module: BlockHelper

Includes:
RailsBlocks::Names, RailsBlocks::Path
Defined in:
app/helpers/block_helper.rb

Constant Summary collapse

BEM_KEYS =
[:tag, :class, :attrs, :mods, :mix, :data, :levels, :content]

Constants included from RailsBlocks::Path

RailsBlocks::Path::ELEMENT_FILE_PREFIX, RailsBlocks::Path::MOD_FILE_PREFIX

Instance Method Summary collapse

Methods included from RailsBlocks::Names

#block_classes, #block_name, #element_classes, #element_name

Methods included from RailsBlocks::Path

#block_template, blocks_dir, #element_template, reload_tree

Instance Method Details

#add_mix(mix) ⇒ Object



114
115
116
117
118
# File 'app/helpers/block_helper.rb', line 114

def add_mix(mix)
	@current_options[:mix] ||= []
	return @current_options[:mix].push(mix) if mix.is_a? Hash
	@current_options[:mix].push(*mix) if mix.is_a? Array
end

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

TODO бога ради, отрефактори это дерьмо



23
24
25
26
27
28
29
30
31
# File 'app/helpers/block_helper.rb', line 23

def b(b_name, options = {}, &block)
	options = page_options.merge options
	options[:parent_block] = context_block
	push_context_block b_name
	template = block_template b_name, options
	result = entity(template, :block, b_name, options, &block)
	pop_context_block
	result
end

#b_classes(b_name, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'app/helpers/block_helper.rb', line 33

def b_classes(b_name, options = {})
	parent_block = context_block
	push_context_block b_name
	options = page_options.merge options
	options[:parent_block] = parent_block if parent_block
	classes = block_classes b_name, options
	pop_context_block
	classes
end

#b_context(b_name, &block) ⇒ Object



43
44
45
46
47
48
# File 'app/helpers/block_helper.rb', line 43

def b_context(b_name, &block)
	push_context_block b_name
	result = capture(&block)
	pop_context_block
	result
end

#bem_attrsObject



85
86
87
88
89
90
91
92
93
94
95
# File 'app/helpers/block_helper.rb', line 85

def bem_attrs
	if @attrs.nil?
		@attrs = {class: current_entity_classes.join(' ')}
		@attrs.merge! @current_options[:attrs] if @current_options[:attrs]
		data = current_bem_data
		@attrs['data-bem'] = data.to_json if data
		@attrs[:tag] = @current_options[:tag] || 'div'
	end

	@attrs
end

#bem_attrs_without_tagObject



97
98
99
# File 'app/helpers/block_helper.rb', line 97

def bem_attrs_without_tag
	bem_attrs.except :tag
end

#bem_page(options, &block) ⇒ Object



10
11
12
13
# File 'app/helpers/block_helper.rb', line 10

def bem_page(options, &block)
	@page_options = options
	block_given? ? capture(&block) : ''
end

#bem_tagObject



81
82
83
# File 'app/helpers/block_helper.rb', line 81

def bem_tag
	bem_attrs[:tag]
end

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



50
51
52
53
54
55
56
57
# File 'app/helpers/block_helper.rb', line 50

def e(e_name, options = {}, &block)
	options = page_options.merge options
	parent_block = options[:b] || context_block
	raise RailsBlocks::NoBlockContextError unless parent_block
	options[:parent_block] = parent_block
	template = element_template parent_block, e_name, options
	entity(template, :elem, e_name, options, &block)
end

#e_classes(e_name, options = {}) ⇒ Object



59
60
61
62
63
64
65
# File 'app/helpers/block_helper.rb', line 59

def e_classes(e_name, options = {})
	parent_block = options[:b] || context_block
	raise RailsBlocks::NoBlockContextError unless parent_block
	options = page_options.merge options
	options[:parent_block] = parent_block
	element_classes(parent_block, e_name, options).join(' ')
end

#empty(content) ⇒ Object



77
78
79
# File 'app/helpers/block_helper.rb', line 77

def empty(content)
	 bem_tag, content, bem_attrs_without_tag
end

#entity(template, type, name, options, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'app/helpers/block_helper.rb', line 67

def entity(template, type, name, options, &block)
	content = block ? capture(&block) : options[:content]
	#важно заполнять публичные поля только после того, как закапчюрится то, что внутри энтити, иначе они перезапишутся.
	@attrs = nil
	@current_options = options
	@current_entity = {type: type, name: name}
	p template
	template.nil? ? empty(content) : render(template: template, locals: {content: content, options: options})
end

#page_optionsObject



15
16
17
18
19
20
# File 'app/helpers/block_helper.rb', line 15

def page_options
	defaults = {
		levels: RailsBlocks.config.levels
	}
	defaults.merge @page_options || {}
end

#set_js(value) ⇒ Object



105
106
107
# File 'app/helpers/block_helper.rb', line 105

def set_js(value)
	@current_options[:js] = value
end

#set_mod(name, value) ⇒ Object



109
110
111
112
# File 'app/helpers/block_helper.rb', line 109

def set_mod(name, value)
	@current_options[:mods] ||= {}
	@current_options[:mods][name] = value
end

#set_tag(tag) ⇒ Object



101
102
103
# File 'app/helpers/block_helper.rb', line 101

def set_tag(tag)
	@current_options[:tag] = tag
end