1

I would like to create a custom Typecaster for the ActiveAttr Gem.

I have a Package class:

class Package

  include ActiveAttr::Model

  attribute :quantity, :type => Integer
  attribute :detail
  attribute :type
  attribute :order

  def shipping

  end
end

and I have Order class

class Order

  include ActiveAttr::Model

  attribute :id, :type => Integer

  def test

  end

end

In the Package class I want to use attribute :order, :type => OrderTypecaster because when I create a new package (p = Package.new(params['package']) I would like to set the Order id attribute automatically.

Is this possible?

I'm using Rails 3.2.13

Tks!

danilodeveloper
  • 3,840
  • 2
  • 36
  • 56

1 Answers1

1

I found a way to solve my problem without create a custom Typecaster. In Package class I wrote two simple methods:

  def order=(value)
    @order = Order.new(value)
  end

  def order
    @order ||= Order.new
  end

Now when I call Package.new(params['package']) the order id is automatically setted. I don't know if this is the best solution but works well, any better solution is welcome. :)

Tks guys!

danilodeveloper
  • 3,840
  • 2
  • 36
  • 56