For some reason, I cannot decorate a particular model, not being able to even access that model’s attributes via the decorator in the console.
I continuously get Stack Too Deep
errors inconsistently; inconsistently outside of the fact I’m unable to access the original model’s attributes at all before getting a Stack Too Deep
error.
The behavior is really bizzarre because the test_method
works when attempted first, but I can’t actually access the same attributes of the original model; I’ve created tons of decorators and never had this problem.
The decorator is rather simple:
class PersonaDecorator
attr_reader :persona
include ActionView::Helpers
def initialize(persona)
@persona = persona
end
def self.decorate_personas(personas)
personas.map { |persona| new(persona) }
end
class << self
alias_method :build_collection, :decorate_personas
end
def respond_to_missing?(method, include_private=false)
persona.respond_to?(method) || super
end
def method_missing(method, *args, &blocks)
persona.send(method, *args, &block)
end
def age
"#{persona.age} years old"
end
def description
"#{persona.description}".html_safe
end
def test_method
content_tag(:div, "#{persona.description}")
end
def test_method2
content_tag(:div, "#{persona.creative_commons_attribution}")
end
def class_box
content_tag :div, class: 'large-12.columns' do
cc_class = case @persona.creative_commons_attribution_license
when 'Attribution-ShareAlike'
'cc_sa'
when 'Attribution-NoDerivs'
'cc_nd'
when 'Attribution-NonCommercial'
'cc_nonc'
when 'Attribution-NonCommercial-NoDerivs'
'cc_nonc_nd'
when 'Attribution-NonCommercial-ShareAlike'
'cc_nonc_sa'
end
content_tag(:figure, class: "large-3-columns creative_commmons_attribution_badge #{cc_class}")
content_tag :a, class: 'creative_commons_explanation' do
"#{persona.creative_commons_attribution}"
end
end
end
end
The model is also rather simple:
class Persona < ActiveRecord::Base
#virtual attributes
attr_accessor :approve_persona, :unapprove_persona, :full_name
#special configuration, properties, and actions
CREATIVE_COMMONS_ATTRIBUTION_LICENSES = %w(None Attribution-ShareAlike Attribution-NoDerivs Attribution-NonCommercial Attribution-NonCommercial-ShareAlike Attribution-NonCommercial-NoDerivs)
has_attached_file :avatar
has_attached_file :background_image
extend FriendlyId
friendly_id :slug_candidates, use: [:slugged, :history]
# call backs
before_save :perform_state_change
include NameConcern
# validations
validates :first_name, :last_name, presence: true
validates_attachment :avatar, content_type: { content_type: ["image/jpeg", "image/webp", "image/png"] }, size: { less_than: 5.megabyte }
validates :age, numericality: { greater_than: 0, integer: true }
validates :description, length: { minimum: 50 }
validates :byline, length: { maximum: 140 }
#validates :project, presence: true
validates_with CreativeCommonsValidator
#associations
belongs_to :project
has_many :influencers
has_many :interests
has_many :goals, as: :goalable
# state_machine
state_machine :state, initial: :pending do
state :approved
state :coming_soon
end
def full_name
"#{first_name} #{last_name}"
end
def slug_candidates
[
:full_name,
[:full_name, :age],
[:full_name, :occupation, :age]
]
end
def perform_state_change
self.state = 'approved' if approve_persona == '1'
self.state = 'pending' if unapprove_persona == '1'
end
end
In case the custom validator may be the cause, it’s a basic level one (though one that couldn’t be one-lined via a Proc
class CreativeCommonsValidator < ActiveModel::Validator
def validate(record)
if record.creative_commons_license != "None" || !record.creative_commons_license.blank?
if record.creative_commons_attribution.blank? || record.creative_commons_attribution_link.blank?
record.errors[:base] << "name and link to original owner of copyright work attached for this record incorrectly done."
end
end
end
end