Class: SimpleAttribute::Attributes::Base
- Inherits:
-
Object
- Object
- SimpleAttribute::Attributes::Base
show all
- Defined in:
- lib/simple_attribute/attributes/base.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(context, options) ⇒ Base
Returns a new instance of Base.
6
7
8
9
10
11
12
|
# File 'lib/simple_attribute/attributes/base.rb', line 6
def initialize(context, options)
@context = context
@options = options.reverse_merge defaults
@record = options.fetch :record
@attribute = options.fetch :attribute
@value = @record.send attribute if attribute
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
97
98
99
|
# File 'lib/simple_attribute/attributes/base.rb', line 97
def method_missing(method, *args, &block)
@context.respond_to?(method) ? @context.send(method, *args, &block) : super
end
|
Instance Attribute Details
#attribute ⇒ Object
Returns the value of attribute attribute.
4
5
6
|
# File 'lib/simple_attribute/attributes/base.rb', line 4
def attribute
@attribute
end
|
#options ⇒ Object
Returns the value of attribute options.
4
5
6
|
# File 'lib/simple_attribute/attributes/base.rb', line 4
def options
@options
end
|
#record ⇒ Object
Returns the value of attribute record.
4
5
6
|
# File 'lib/simple_attribute/attributes/base.rb', line 4
def record
@record
end
|
#value ⇒ Object
Returns the value of attribute value.
4
5
6
|
# File 'lib/simple_attribute/attributes/base.rb', line 4
def value
@value
end
|
Instance Method Details
#attribute_name ⇒ Object
50
51
52
|
# File 'lib/simple_attribute/attributes/base.rb', line 50
def attribute_name
attribute.to_s.dasherize
end
|
#default_value ⇒ Object
28
29
30
|
# File 'lib/simple_attribute/attributes/base.rb', line 28
def default_value
@options.fetch :default_value, '—'
end
|
#defaults ⇒ Object
14
15
16
|
# File 'lib/simple_attribute/attributes/base.rb', line 14
def defaults
Hash(SimpleAttribute.config.send(:"#{renderer_name}")).symbolize_keys
end
|
#html_options ⇒ Object
58
59
60
|
# File 'lib/simple_attribute/attributes/base.rb', line 58
def html_options
options.fetch :html, {}
end
|
#label_method ⇒ Object
54
55
56
|
# File 'lib/simple_attribute/attributes/base.rb', line 54
def label_method
@options.fetch(:label, :to_s)
end
|
#render ⇒ Object
92
93
94
95
|
# File 'lib/simple_attribute/attributes/base.rb', line 92
def render
content = wrapper? ? render_wrapper : render_with_default
content.to_s.html_safe
end
|
#render_attribute ⇒ Object
76
77
78
|
# File 'lib/simple_attribute/attributes/base.rb', line 76
def render_attribute
value.try(label_method).to_s.html_safe
end
|
#render_default_value ⇒ Object
69
70
71
72
73
74
|
# File 'lib/simple_attribute/attributes/base.rb', line 69
def render_default_value
return if default_value.blank?
@options[:wrapper] = nil
content_tag :span, default_value, class: 'attribute-default-value'
end
|
#render_with_default ⇒ Object
80
81
82
83
84
85
86
|
# File 'lib/simple_attribute/attributes/base.rb', line 80
def render_with_default
if value?
render_attribute
else
render_default_value
end
end
|
#render_wrapper ⇒ Object
88
89
90
|
# File 'lib/simple_attribute/attributes/base.rb', line 88
def render_wrapper
content_tag :span, render_with_default.to_s.html_safe, wrapper_html
end
|
#renderer_name ⇒ Object
43
44
45
46
47
48
|
# File 'lib/simple_attribute/attributes/base.rb', line 43
def renderer_name
name = self.class.name.gsub('Attribute', '')
name = name.demodulize.underscore
name == 'base' ? 'string' : name.to_s.dasherize
end
|
18
19
20
21
22
23
24
25
26
|
# File 'lib/simple_attribute/attributes/base.rb', line 18
def value?
if value.is_a? ActiveRecord::Base
value.try(:id).present?
elsif value.is_a? String
strip_tags(value).present?
else
value.present?
end
end
|
#wrapper ⇒ Object
36
37
38
39
40
41
|
# File 'lib/simple_attribute/attributes/base.rb', line 36
def wrapper
wrapper = options.fetch :wrapper, nil
wrapper = SimpleAttribute.config.wrappers.try(:"#{wrapper}") unless wrapper.is_a? Hash
Hash(wrapper).symbolize_keys
end
|
32
33
34
|
# File 'lib/simple_attribute/attributes/base.rb', line 32
def wrapper?
options[:wrapper] != false
end
|
#wrapper_html ⇒ Object
62
63
64
65
66
67
|
# File 'lib/simple_attribute/attributes/base.rb', line 62
def wrapper_html
classes = ['attribute', attribute_name, renderer_name].uniq.join ' '
classes = "#{wrapper[:class]} #{classes}".strip
wrapper.merge(class: classes)
end
|