質問

I have a class GoodsTransaction:

require Rails.root.join('lib','two_parties_transaction.rb')

class GoodsTransaction < Transaction

  include TwoPartiesTransaction

  def counterparty(org, loop = false)

    res = #Some logic
    res || super(org)
  end
end

which includes the TwoPartiesTransaction module:

module TwoPartiesTransaction
  extend ActiveSupport::Concern

  included do
    def counterparty(org)
       #Some logic
    end
  end
end

and running counterparty fails:

muichkine@ubuntu:~/src/white$ rails c
Faraday::Builder is now Faraday::RackBuilder.
Loading development environment (Rails 4.0.0)
2.0.0p247 :001 > tr = GoodsTransaction.new
=> #<GoodsTransaction _id: 5264e2eb75627540dd000000, e(end_date): nil, p(span): nil, s(start_date): nil, u(span_unit): nil, _type: "GoodsTransaction", a(autobook): nil, c(forced_close): nil, d(public_id): nil, k(autobook_period): nil, l(label): nil, n(autobook_number): nil, t(autobook_until): nil, contact_ids: nil, parent_id: nil, fc(creditor): nil, fd(debtor): nil> 
2.0.0p247 :002 > tr.counterparty(nil)
NoMethodError: super: no superclass method `counterparty' for   #<GoodsTransaction:0x000000090fe1d8>
from /home/muichkine/src/white/app/models/transactions/goods_transaction.rb:148:in `counterparty'

What am I doing that skips the super coutnerparty method?

役に立ちましたか?

解決

For including instance methods from the module into a class, you do not need to use ActiveSupport::Concern. You can just do

module TwoPartiesTransaction
  def counterparty(org)
     # ...
  end
end

You can still use ActiveSupport::Concern if you need it for other reasons, namely executing code at the class level (such as scope etc.) or adding class methods to the including class. For more detail on ActiveSupport::Concern, see the API documentation.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top