Sha256: 6ac78f2668e090ed36b2e3b200484194294a406795347c9ddd7e34d88226ad9d

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module Dash
  class ResourceGenerator < Rails::Generators::NamedBase
    
    source_root File.expand_path('../templates', __FILE__)

    def add_route
      insert_into_file 'config/routes.rb', :after => /namespace :dash(.*)/ do
        "\n\n    resources :#{table_name}"
      end
    end
    
    def add_scope
      insert_into_file "app/models/#{singular_name}.rb", :after => /class #{singular_name.camelize}(.*)/ do
        "\n\n  scope :filter, lambda { |filter| where('', :filter => \"%\#{filter}%\") }"
      end
    end

    def add_menu
      insert_into_file 'app/views/layouts/dash.html.haml', :before => /(.*)#session(.*)/ do
        "#{' ' * 16}%li\n" +
        "#{' ' * 18}%a{ :href => url_for([:dash, :#{table_name}]), :title => #{singular_name.camelize}.model_name.human(:count => 2) }\n" +
        "#{' ' * 20}= #{singular_name.camelize}.model_name.human(:count => 2)\n"
      end
    end

    def create_templates
      template 'resource.erb', "app/controllers/dash/#{table_name}_controller.rb"
      template 'form.erb', "app/views/dash/#{table_name}/form.html.haml"
      template 'index.erb', "app/views/dash/#{table_name}/index.html.haml"
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_dash-0.0.4 lib/generators/dash/resource_generator.rb
rails_dash-0.0.3 lib/generators/dash/resource_generator.rb