cirandas.net

ref: master

plugins/exchange/models/exchange_plugin/element.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class ExchangePlugin::Element < ActiveRecord::Base

  attr_accessible *self.column_names
  attr_accessible :proposal, :object, :object_np
  attr_accessible :object_attributes

  belongs_to :proposal, class_name: "ExchangePlugin::Proposal"
  has_one :exchange, through: :proposal

  belongs_to :object, polymorphic: true
  accepts_nested_attributes_for :object

  # np: non polymorphic version
  belongs_to :object_np, foreign_key: :object_id
  def object_np
    raise 'Dont use me'
  end

  validates_presence_of :proposal

  def build_object *args
    if self.object
      self.object
    else
      self.object_type.constantize.new *args
    end
  end

  # WORKAROUND bug in accepts_nested_attributes_for
  def object_attributes= attributes
    self.object.attributes = attributes
  end

  protected

end