2016-05-31 7 views
2

私は花見を使用しています。/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で。

私は間違っていますか?

答えて

3

あなたはEventRepositoryMyModule::Utils::Slugを含む場合、含まれるモジュールで定義されたメソッドがStringEventRepositoryないのインスタンスで利用可能です。モジュールをそのまま使用する場合は、Stringに含める必要があります。あなたはグローバルクラスでそれを含めたくない場合は、

module MyModule 
    module Utils 
    module Slug 
     def slug_it(string, random = false) 
     if random 
      string.slugify + '-' + SecureRandom.hex(10).to_s 
     else 
      string.slugify 
     end 
     end 
    end 
    end 
end 

を行い、その後、あなたはそれのようsomethong試みることができるslugify

unless found.slug.include? slug_it(attrs[:name]) 
    attrs[:slug] = slug_it(attrs[:name], true) 
end 
+0

、@LukasEklundをありがとう!あなたの答えは本当に私を助けました。私はまた、Dir ["#{__ dir __}/myapp/**/*。rb"]とDir ["#{__ dir __}/supports/**/*。rb"]の間の要件の順序を変更する必要がありました。 EventRepositoryクラスのMyModuleをそれぞれ拡張します。 – marcodamaceno

0

にしたい文字列を渡すためにスラグの作成を修正することができます、また:

https://gist.github.com/rafaels88/5529b3863c699b1cd4d20265c32d4a21

# lib/your_app/ext/sluggable.rb 

module Sluggable 
    def self.included(base) 
    base.extend(ClassMethods) 
    end 

    module ClassMethods 
    def sluggable(field) 
     @field = field 
    end 

    def field 
     @field 
    end 
    end 

    def slug 
    send(self.class.field).slugify 
    end 
end 

# USAGE 
# 
# class MyEntity 
# include Hanami::Entity 
# include Sluggable 

# attributes :name, :slug, :other_field, :yet_another_field 
# sluggable :name 
# end 
# 
# Do not forget to create :slug field in database and mapping it to its entity 
0

anothえーそれを行う方法は次のとおりです。

require 'slugify' 

module MyModule 
    module Utils 
    module Slug 
     def self.slug_it(string, random = false) # Please note `self` and `string` 
     # ... 
     end 
    end 
    end 
end 

次に、あなたがこのようにそれを使用することができます:

MyModule::Utils::Slug.slug_it(attrs[:name], true) 
関連する問題