私は花見を使用しています。/lib/supports/utils.rbにカスタムモジュールを作成しました。私はあるすべてのファイルを必要としています/libに/サポートで/libに、それは以下のようなものだ/ myappの:NoMethodError:Hanamiのカスタムモジュールを使用した文字列の未定義メソッド
require 'hanami/model'
require 'hanami/mailer'
Dir["#{__dir__}/myapp/**/*.rb"].each { |file| require_relative file }
Dir["#{__dir__}/supports/**/*.rb"].each { |file| require_relative file }
Hanami::Model.configure do
# and so on
/lib/supports/utils.rbでは、私がしている:
# using the gem 'slugify'
require 'slugify'
module MyModule
module Utils
module Slug
def slug_it(random = false)
if random
slugify + '-' + SecureRandom.hex(10).to_s
else
slugify
end
end
end
end
end
私はMyModuleというを含めることを試みた:: Utilsの::スラッグリポジトリ内が、それは常にNoMethodError返す: "string" の未定義のメソッド `slug_it」:文字列を 。例:
class EventRepository
include Hanami::Repository
include MyModule::Utils::Slug
def self.create_or_update(attrs)
found = find(attrs.id)
attrs = event_attributes(attrs)
if found
unless found.slug.include? attrs[:name].slug_it
attrs[:slug] = attrs[:name].slug_it(true)
end
found.update(attrs)
update found
else
attrs[:slug] = attrs[:name].slug_it(true)
create Event.new(attrs)
end
end
end
それは唯一私が/lib/supports/utils.rbの一番下に追加した場合に動作します:
class String
include MyModule::Utils::Slug
end
私はいつもinclude Hanami::Repository
のようなモジュールを含めたいですEventRepositoryで。
私は間違っていますか?
、@LukasEklundをありがとう!あなたの答えは本当に私を助けました。私はまた、Dir ["#{__ dir __}/myapp/**/*。rb"]とDir ["#{__ dir __}/supports/**/*。rb"]の間の要件の順序を変更する必要がありました。 EventRepositoryクラスのMyModuleをそれぞれ拡張します。 – marcodamaceno