class SimpleAttribute::Attributes::Base

Attributes

attribute[RW]
options[RW]
record[RW]
value[RW]

Public Class Methods

new(context, options) click to toggle source
# 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

Public Instance Methods

attribute_name() click to toggle source
# File lib/simple_attribute/attributes/base.rb, line 50
def attribute_name
  attribute.to_s.dasherize
end
default_value() click to toggle source
# File lib/simple_attribute/attributes/base.rb, line 28
def default_value
  @options.fetch :default_value, '—'
end
defaults() click to toggle source
# File lib/simple_attribute/attributes/base.rb, line 14
def defaults
  Hash(SimpleAttribute.config.send(:"#{renderer_name}")).symbolize_keys
end
html_options() click to toggle source
# File lib/simple_attribute/attributes/base.rb, line 58
def html_options
  options.fetch :html, {}
end
label_method() click to toggle source
# File lib/simple_attribute/attributes/base.rb, line 54
def label_method
  @options.fetch(:label, :to_s)
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# 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
render() click to toggle source
# 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() click to toggle source
# File lib/simple_attribute/attributes/base.rb, line 76
def render_attribute
  value.try(label_method).to_s.html_safe
end
render_default_value() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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?() click to toggle source
# 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() click to toggle source
# 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?() click to toggle source
# File lib/simple_attribute/attributes/base.rb, line 32
def wrapper?
  options[:wrapper] != false
end
wrapper_html() click to toggle source
# 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