1

出于某种原因,我无法装饰特定模型,甚至无法通过控制台中的装饰器访问该模型的属性。

我不断收到Stack Too Deep不一致的错误;Stack Too Deep在出现错误之前我根本无法访问原始模型的属性这一事实之外的不一致。

这种行为真的很奇怪,因为test_method第一次尝试时有效,但我实际上无法访问原始模型的相同属性;我创造了大量的装饰器,从来没有遇到过这个问题。

装饰器相当简单:

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

该模型也相当简单:

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

如果自定义验证器可能是原因,它是基本级别的(尽管不能通过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 

然而,验证器在它应该的上下文中正常工作(新的和现有的记录),所以我怀疑这是问题所在......

4

1 回答 1

0

我认为原因是您在所有实例方法中都引用persona了实例变量而不是实例变量。PersonaDecorator 的实例@persona中没有这样的东西。persona

我的建议是将它们全部替换为实例变量, exceptself.decorate_personas和 removeattr_reader它实际上是指对象本身,可能是“堆栈级别太深”的直接原因

于 2013-09-27T19:00:33.830 回答