Sha256: bfcb95537179d7c22df9e770faa631c0fcdbdd8fa5d0bb656c877916672f9df4

Contents?: true

Size: 795 Bytes

Versions: 9

Compression:

Stored size: 795 Bytes

Contents

class ObserverGenerator < Rails::Generators::NamedBase
  desc "Adds method 'observe_me' to a Observer" 
    
  def self.source_root
    @source_root ||= File.expand_path("../templates", __FILE__)
  end

  def add_observer_method  
    if File.exist?(observer_file_name)
      inject_into_file(observer_file_name, observer_method_code, :after => after_txt) if after_txt
    else
      say "#{observer_file_name} does not exist. Please create it first before you can add a observer method to it!", :red
    end
  end

  protected

  def after_txt
    "ActiveRecord::Observer"
  end
                     
  def observer_file_name
    File.join(Rails.root, "app/models/#{file_name}_observer.rb")
  end

  def observer_method_code
    %Q{
  def observe_me
    "Observe me please!"
  end
}
  end
end 

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
generator-spec-0.5.0 lib/generators/observer/observer_generator.rb
generator-spec-0.4.8 lib/generators/observer/observer_generator.rb
generator-spec-0.4.7 lib/generators/observer/observer_generator.rb
generator-spec-0.4.5 lib/generators/observer/observer_generator.rb
generator-spec-0.4.4 lib/generators/observer/observer_generator.rb
generator-spec-0.4.3 lib/generators/observer/observer_generator.rb
generator-spec-0.4.2 lib/generators/observer/observer_generator.rb
generator-spec-0.4.1 lib/generators/observer/observer_generator.rb
generator-spec-0.4.0 lib/generators/observer/observer_generator.rb