Class: SimpleAttribute::Attributes::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_attribute/attributes/base.rb

Direct Known Subclasses

Association, Avatar, Boolean, Date, Enumeration, Image, Link, Money, Video

Instance Attribute Summary collapse

Instance Method Summary collapse

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, 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

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

#attributeObject

Returns the value of attribute attribute.



4
5
6
# File 'lib/simple_attribute/attributes/base.rb', line 4

def attribute
  @attribute
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/simple_attribute/attributes/base.rb', line 4

def options
  @options
end

#recordObject

Returns the value of attribute record.



4
5
6
# File 'lib/simple_attribute/attributes/base.rb', line 4

def record
  @record
end

#valueObject

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_nameObject

Attribute name



58
59
60
# File 'lib/simple_attribute/attributes/base.rb', line 58

def attribute_name
  "#{attribute}".dasherize
end

#default_valueObject

Get default value



32
33
34
# File 'lib/simple_attribute/attributes/base.rb', line 32

def default_value
  @options.fetch :default_value, ''
end

#defaultsObject

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_optionsObject

Attribute html options



68
69
70
# File 'lib/simple_attribute/attributes/base.rb', line 68

def html_options
  options.fetch :html, {}
end

#label_methodObject

Attribute label method



63
64
65
# File 'lib/simple_attribute/attributes/base.rb', line 63

def label_method
  @options.fetch(:label, :to_s)
end

#renderObject

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_attributeObject

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_valueObject

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
     :span, default_value, class: "attribute-default-value"
  end
end

#render_with_defaultObject

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_wrapperObject

Render wrapper



103
104
105
# File 'lib/simple_attribute/attributes/base.rb', line 103

def render_wrapper
   :span, render_with_default.to_s.html_safe, wrapper_html
end

#renderer_nameObject

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

Returns:



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
    strip_tags(value).present?
  else
    value.present?
  end
end

#wrapperObject

Get wrapper



42
43
44
45
46
47
# File 'lib/simple_attribute/attributes/base.rb', line 42

def wrapper
  wrapper = options.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

Returns:



37
38
39
# File 'lib/simple_attribute/attributes/base.rb', line 37

def wrapper?
  options[:wrapper] != false
end

#wrapper_htmlObject

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