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

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

#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



50
51
52
# File 'lib/simple_attribute/attributes/base.rb', line 50

def attribute_name
  attribute.to_s.dasherize
end

#default_valueObject



28
29
30
# File 'lib/simple_attribute/attributes/base.rb', line 28

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

#defaultsObject



14
15
16
# File 'lib/simple_attribute/attributes/base.rb', line 14

def defaults
  Hash(SimpleAttribute.config.send(:"#{renderer_name}")).symbolize_keys
end

#html_optionsObject



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

def html_options
  options.fetch :html, {}
end

#label_methodObject



54
55
56
# File 'lib/simple_attribute/attributes/base.rb', line 54

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

#renderObject



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_attributeObject



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_valueObject



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

#render_with_defaultObject



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_wrapperObject



88
89
90
# File 'lib/simple_attribute/attributes/base.rb', line 88

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

#renderer_nameObject



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

#value?Boolean

Returns:



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

#wrapperObject



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

#wrapper?Boolean

Returns:



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

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

#wrapper_htmlObject



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