Class: SimpleAttribute::Attributes::Base
- Inherits:
-
Object
- Object
- SimpleAttribute::Attributes::Base
- Defined in:
- lib/simple_attribute/attributes/base.rb
Direct Known Subclasses
Association, Avatar, Boolean, Date, Enumeration, Image, Link, Money, Video
Instance Attribute Summary collapse
-
#attribute ⇒ Object
Returns the value of attribute attribute.
-
#options ⇒ Object
Returns the value of attribute options.
-
#record ⇒ Object
Returns the value of attribute record.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#attribute_name ⇒ Object
Attribute name.
-
#default_value ⇒ Object
Get default value.
-
#defaults ⇒ Object
Get default options.
-
#html_options ⇒ Object
Attribute html options.
-
#initialize(context, options) ⇒ Base
constructor
Initialize base attribute.
-
#label_method ⇒ Object
Attribute label method.
-
#method_missing(method, *args, &block) ⇒ Object
Use view helpers if method is missing.
-
#render ⇒ Object
Render.
-
#render_attribute ⇒ Object
Render attribute.
-
#render_default_value ⇒ Object
Render default value.
-
#render_with_default ⇒ Object
Render attribute or default.
-
#render_wrapper ⇒ Object
Render wrapper.
-
#renderer_name ⇒ Object
Wrapper name.
-
#value? ⇒ Boolean
Check if has value.
-
#wrapper ⇒ Object
Get wrapper.
-
#wrapper? ⇒ Boolean
Check if needs wrapper.
-
#wrapper_html ⇒ Object
Wrapper html.
Constructor Details
#initialize(context, options) ⇒ Base
Initialize base attribute
7 8 9 10 11 12 13 |
# File 'lib/simple_attribute/attributes/base.rb', line 7 def initialize(context, ) @context = context @options = .reverse_merge defaults @record = .fetch :record @attribute = .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
Use view helpers if method is missing
114 115 116 |
# File 'lib/simple_attribute/attributes/base.rb', line 114 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 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
Attribute name
58 59 60 |
# File 'lib/simple_attribute/attributes/base.rb', line 58 def attribute_name "#{attribute}".dasherize end |
#default_value ⇒ Object
Get default value
32 33 34 |
# File 'lib/simple_attribute/attributes/base.rb', line 32 def default_value @options.fetch :default_value, '—' end |
#defaults ⇒ Object
Get default options
16 17 18 |
# File 'lib/simple_attribute/attributes/base.rb', line 16 def defaults Hash(SimpleAttribute.config.send(:"#{renderer_name}")).symbolize_keys end |
#html_options ⇒ Object
Attribute html options
68 69 70 |
# File 'lib/simple_attribute/attributes/base.rb', line 68 def .fetch :html, {} end |
#label_method ⇒ Object
Attribute label method
63 64 65 |
# File 'lib/simple_attribute/attributes/base.rb', line 63 def label_method @options.fetch(:label, :to_s) end |
#render ⇒ Object
Render
108 109 110 111 |
# File 'lib/simple_attribute/attributes/base.rb', line 108 def render content = wrapper? ? render_wrapper : render_with_default content.to_s.html_safe end |
#render_attribute ⇒ Object
Render attribute
89 90 91 |
# File 'lib/simple_attribute/attributes/base.rb', line 89 def render_attribute "#{value.try(label_method)}".html_safe end |
#render_default_value ⇒ Object
Render default value
81 82 83 84 85 86 |
# File 'lib/simple_attribute/attributes/base.rb', line 81 def render_default_value if default_value.present? @options[:wrapper] = nil content_tag :span, default_value, class: "attribute-default-value" end end |
#render_with_default ⇒ Object
Render attribute or default
94 95 96 97 98 99 100 |
# File 'lib/simple_attribute/attributes/base.rb', line 94 def render_with_default if value? render_attribute else render_default_value end end |
#render_wrapper ⇒ Object
Render wrapper
103 104 105 |
# File 'lib/simple_attribute/attributes/base.rb', line 103 def render_wrapper content_tag :span, render_with_default.to_s.html_safe, wrapper_html end |
#renderer_name ⇒ Object
Wrapper name
50 51 52 53 54 55 |
# File 'lib/simple_attribute/attributes/base.rb', line 50 def renderer_name name = self.class.name.gsub('Attribute', '') name = name.demodulize.underscore name == 'base' ? 'string' : "#{name}".dasherize end |
#value? ⇒ Boolean
Check if has value
21 22 23 24 25 26 27 28 29 |
# File 'lib/simple_attribute/attributes/base.rb', line 21 def value? if value.is_a? ActiveRecord::Base value.try(:id).present? elsif value.is_a? String (value).present? else value.present? end end |
#wrapper ⇒ Object
Get wrapper
42 43 44 45 46 47 |
# File 'lib/simple_attribute/attributes/base.rb', line 42 def wrapper wrapper = .fetch :wrapper, nil wrapper = SimpleAttribute.config.wrappers.try(:"#{wrapper}") unless wrapper.is_a? Hash Hash(wrapper).symbolize_keys end |
#wrapper? ⇒ Boolean
Check if needs wrapper
37 38 39 |
# File 'lib/simple_attribute/attributes/base.rb', line 37 def wrapper? [:wrapper] != false end |
#wrapper_html ⇒ Object
Wrapper html
73 74 75 76 77 78 |
# File 'lib/simple_attribute/attributes/base.rb', line 73 def wrapper_html classes = ['attribute', attribute_name, renderer_name].uniq.join ' ' classes = "#{wrapper[:class]} #{classes}".strip wrapper.merge(class: classes) end |